forked from dmtrmrv/wp-theme-rename-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·255 lines (213 loc) · 5.84 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#! /usr/bin/env node
// Dependencies.
var fs = require( 'fs' );
var path = require( 'path' );
// Process user arguments.
var args = process.argv.slice( 2 );
//Define variables.
var oldSlug,
oldName,
newSlug,
newName,
themeDir,
stylesheet;
// Excluded folders.
var folderExclude = [
'node_modules',
'.git',
'.sass-cache',
'.hg',
'.svn',
'.CVS',
'cache',
]
// Excluded files.
var fileExclude = []
// File extensions to work with.
var extensions = [
'.php',
'.html',
'.js',
'.json',
'.css',
'.scss',
'.sass',
'.txt',
'.md',
'.pot'
]
// Process arguments.
for ( var i = 0; i < args.length; i++ ) {
if ( args[ i ].indexOf( '-s' ) != -1 ) {
newSlug = args[ i ].replace( '-s=', '' );
} else if ( args[ i ].indexOf( '-n' ) != -1 ) {
newName = args[ i ].replace( '-n=', '' );
} else if ( args[ i ].indexOf( '-extensions' ) != -1 ) {
extensions = args[ i ].replace( '-extensions=', '' ).split( ',' );
} else if ( args[ i ].indexOf( '-folderExclude' ) != -1 ) {
folderExclude = args[ i ].replace( '-folderExclude=', '' ).split( ',' );
} else if ( args[ i ].indexOf( '-fileExclude' ) != -1 ) {
fileExclude = args[ i ].replace( '-fileExclude=', '' ).split( ',' );
}
}
// Return if no slug is provided.
if ( ! newSlug ) {
console.log( 'No slug provided!' );
return;
}
// Return in no name is provided.
if ( ! newName ) {
console.log( 'No name provided!' );
return;
}
// Read the themeDirectory.
themeDir = fs.readdirSync( '.' );
// If we have stylesheet, read it.
if ( typeof themeDir == 'object' && themeDir.indexOf( 'style.css' ) != -1 ) {
stylesheet = fs.readFileSync( 'style.css', 'UTF8' );
} else {
console.log( 'No style.css in the folder!' );
return
}
// If stylesheet is not empty read the theme slug.
if ( stylesheet && stylesheet.search( /Text Domain:.*$/m ) != -1 ) {
// Get the whole textdomain line.
oldSlug = stylesheet.match( /Text Domain:.*$/m );
// Trim everything except the slug.
oldSlug = oldSlug[0].replace( 'Text Domain:', '' ).trim();
} else {
console.log( 'Text domain is not defined!' );
return
}
// If stylesheet is not empty read the Theme Name.
if ( stylesheet && stylesheet.search( /Theme Name:.*$/m ) != -1 ) {
// Get the whole textdomain line.
oldName = stylesheet.match( /Theme Name:.*$/m );
// Trim everything except the Name.
oldName = oldName[0].replace( 'Theme Name:', '' ).trim();
} else {
console.log( 'Theme name is not defined!' );
return
}
// Check for a dot and and a slash at the beginning of the folder name and add it if necessary.
if ( folderExclude.length > 0 ) {
for ( var i = 0; i < folderExclude.length; i++ ) {
if ( folderExclude[i].indexOf( './' ) != 0 )
folderExclude[i] = './' + folderExclude[i];
};
}
// Check for a dot and and a slash at the beginning of the file name and add it if necessary.
if ( fileExclude.length > 0 ) {
for ( var i = 0; i < fileExclude.length; i++ ) {
if ( fileExclude[i].indexOf( './' ) != 0 )
fileExclude[i] = './' + fileExclude[i];
};
}
// Check for a dot at the beginning of the extension name and add it if necessary.
if ( extensions.length > 0 ) {
for ( var i = 0; i < extensions.length; i++ ) {
if ( extensions[i].indexOf( '.' ) != 0 )
extensions[i] = '.' + extensions[i];
};
}
// Replace tasks.
function replaceStrings( file ) {
// Get the content of the file.
var content = fs.readFileSync( file, 'UTF8' );
var old = content;
// Replace the prefixes.
content = content.replace(
new RegExp( oldSlug + '_', 'g' ),
newSlug + '_'
);
// Replace the handles.
content = content.replace(
new RegExp( oldSlug + '-', 'g' ),
newSlug + '-'
);
// Replace text domain.
content = content.replace(
new RegExp( '\'' + oldSlug + '\'', 'g' ),
'\'' + newSlug + '\''
);
// Replace Constants.
content = content.replace(
new RegExp( oldSlug.toUpperCase(), 'g' ),
newSlug.toUpperCase()
);
// Docblocks space before.
content = content.replace(
new RegExp( ' ' + oldName, 'g' ),
' ' + newName
);
// Docblocks space after.
content = content.replace(
new RegExp( oldName + ' ', 'g' ),
newName + ' '
);
// Docblocks space before lowercase.
content = content.replace(
new RegExp( ' ' + oldSlug, 'g' ),
' ' + newSlug
);
// Paths.
content = content.replace(
new RegExp( '/' + oldSlug, 'g' ),
'/' + newSlug
);
// Strings in single quotes lowercase.
content = content.replace(
new RegExp( '\'' + oldSlug + '\'', 'g' ),
'\'' + newSlug + '\''
);
// Strings in single quotes Uppercase.
content = content.replace(
new RegExp( '\'' + oldName + '\'', 'g' ),
'\'' + newName + '\''
);
// Strings in double quotes lowercase.
content = content.replace(
new RegExp( '\"' + oldSlug + '\"', 'g' ),
'\"' + newSlug + '\"'
);
// Strings in double quotes Uppercase.
content = content.replace(
new RegExp( '\"' + oldName + '\"', 'g' ),
'\"' + newName + '\"'
);
// Markdown headings.
content = content.replace(
new RegExp( '#' + oldName, 'g' ),
'#' + newName
);
// Write the file.
fs.writeFileSync( file, content, 'UTF8' );
}
/**
* Walk the folder recursively and call the string replacing function.
* @param {string} dir Directory to walk
*/
function walkDir( dir ) {
// Check if the directory name has a trailing slash and add it if necessary.
if ( dir[ dir.length-1 ] != '/' ) {
dir = dir.concat('/')
}
// Read all files and folders in the directory.
items = fs.readdirSync( dir );
// Loop through each of them.
items.forEach( function( item ) {
if ( fs.statSync( dir + item ).isDirectory() ) {
if ( folderExclude.indexOf( dir + item ) == -1 ) {
walkDir( dir + item + '/' );
}
} else if ( fs.statSync( dir + item ).isFile() ) {
if ( ( extensions.indexOf( path.extname( dir + item ) ) != -1 ) && ( fileExclude.indexOf( dir + item ) == -1 ) ) {
replaceStrings( dir + item );
}
}
} );
};
// Unleash the beast.
walkDir( '.' );
// Now start fresh with a new theme name!
console.log( 'All done!' );