-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetup.js
94 lines (88 loc) · 1.99 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const replace = require( 'replace-in-file' );
/**
* Create necessary combinations of given theme name.
* @param {array} names Theme name and slug from the prompt.
* @returns {string} names Prepared theme names set.
*/
function prepareThemeName( names ) {
names.underscoreLc = names.theme_slug.toLowerCase().replace( /[- \\/]/g, '_' );
names.underscoreUc = names.theme_name.replace( /[- \\/]/g, '_' );
return names;
}
/**
* Replace strings in theme files for new theme name.
* @param {array} names Prepared combinations of theme name.
*/
function renameTheme( names ) {
const options = {
files: [
'./**/*.php',
'./assets/build/*.json',
'./*.css',
'./*.json',
'./webpack.*.js',
'./tests/php/*.yml',
],
from: [
/'_s'/g,
/_s_/g,
/Text Domain: _s/g,
/package _s/g,
/ _s/g,
/_s-/g,
/--s--/g,
/__url__/g
],
to: [
`'${names.theme_slug}'`,
`${names.underscoreLc}_`,
`Text Domain: ${names.theme_slug}`,
`package ${names.underscoreUc}`,
` ${names.theme_name}`,
`${names.theme_slug}-`,
names.theme_slug,
names.theme_url
],
};
replace( options );
}
/*
* Setup CLI prompt for theme name and theme slug input.
*/
const inquirer = require( 'inquirer' ),
installType = [
{
type: 'input',
name: 'theme_name',
message: 'What is the theme name?',
default() {
return 'My Amazing Theme';
}
},
{
type: 'input',
name: 'theme_slug',
message: 'What is the theme slug?',
default( answers ) {
return answers.theme_name.toLowerCase().replace( /[ _\\/]/g, '-' );
},
validate( value ) {
if ( value.match( /^[-a-z0-9]*$/ ) ) {
return true;
}
return 'Theme slug should contain only lowercase letters and dashes';
}
},
{
type: 'input',
name: 'theme_url',
message: 'What is the site current url?',
default() {
return 'http://localhost';
}
}
];
inquirer.prompt( installType ).then( ( answers ) => {
const themeName = prepareThemeName( answers );
renameTheme( themeName );
} );