-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
328 lines (314 loc) · 8.5 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const fs = require( 'fs' );
const crypto = require( 'crypto' );
const { glob } = require( 'glob' )
const { execSync, spawnSync } = require("child_process");
/**
* @typedef Path
* @type {object}
* @property {string} dir - Parent directory.
* @property {number} order - Order. Lower processed first.
* @property {string[]} path - Array of file path.
*/
/**
* Add path by order.
*
* @param {Path[]} paths
* @param {string} newPath
* @return {void}
*/
const addPath = ( paths, newPath ) => {
const pathParts = newPath.split( '/' ).filter( path => path.length >= 1 );
const length = pathParts.length;
const lastName = pathParts.pop();
const parentDir = pathParts.join( '/' );
let index = -1;
for ( let i = 0; i < paths.length; i++ ) {
if ( paths[ i ].dir === parentDir ) {
index = i;
break;
}
}
if ( 0 <= index ) {
// Already exists.
paths[ index ].path.push( [ parentDir, lastName ].join( '/' ) );
} else {
// Add new path.
paths.push( {
dir: parentDir, order: length, path: [ [ parentDir, lastName ].join( '/' ) ],
} );
}
paths.sort( ( a, b ) => {
if ( a.order === b.order ) {
return 0;
} else {
return a.order < b.order ? -1 : 1;
}
} );
}
/**
* Extract license header from JS file.
*
* @param {string} path
* @param {string} src
* @param {string} dest
* @return {boolean}
*/
const extractHeaderToLicense = ( path, src, dest ) => {
const target = path.replace( src, dest ) + '.LICENSE.txt';
const content = fs.readFileSync( path, 'utf8' );
if ( !content ) {
return false;
}
const match = content.match( /^(\/\*{1,2}!.*?\*\/)/ms );
if ( !match ) {
return false;
}
fs.writeFileSync( target, match[ 1 ] );
return true;
}
/**
* Check if wp-scripts is available.
*
* @returns {boolean}
*/
function isWordPressScriptsAvailable() {
const result = spawnSync("npm", ["list", "@wordpress/scripts"], { encoding: "utf-8" });
return result.stdout.match( /@wordpress\/scripts/ms );
}
/**
* Parse header to grab information.
*
* @param {object} object Object to assign.
* @param {string} fileContent Line string to parse.
* @param {string[]} deps Additional dependencies.
* @param {number} max_scan Maximum lines to scan.
* @return {object}
*/
function scanHeader( object, fileContent, deps, max_scan = 60 ) {
if ( !deps ) {
deps = [];
}
lines = fileContent.toString().split( "\n" );
lines.forEach( ( line, i ) => {
// If limit exceeded, stop scanning.
if ( i + 1 > max_scan ) {
return;
}
if ( ! line.match( /^[ *]*(wp|@)(deps|handle|version|footer|media|strategy|cssmedia)=?(.*)$/ ) ) {
// This is not header. Skip.
return;
}
const key = RegExp.$2.trim();
let value = RegExp.$3.trim();
switch ( key ) {
case 'version':
case 'handle':
case 'strategy':
object[ key ] = value;
break;
case 'media':
if ( ! object.media ) {
object.media = value;
}
break;
case 'cssmedia':
object.media = value;
break;
case 'footer':
object.footer = !( 'false' === value );
break;
case 'deps':
value.split( ',' ).map( ( dep ) => {
dep = dep.trim();
if ( 0 > deps.indexOf( dep ) ) {
deps.push( dep );
}
} );
break;
}
} );
object.deps = deps;
return object;
}
/**
* Grab dependencies from file.
*
* @param {String} file File path to scan.
* @param {String|Function} suffix Suffix for license.txt. If exists, it priors. Default, ".License.txt".
* @param {String} version Default version for files. If specified in header, it priors.
* @returns {object|null}
*/
function grabDeps( file, suffix = '', version = '0.0.0' ) {
const info = {
handle: file.split( '/' ).slice( -1 )[ 0 ].replace( /\.(js|jsx|css|scss)$/, '' ),
path: file,
ext: /\.js$/.test( file ) ? 'js' : 'css',
hash: '',
version,
deps: [],
footer: true,
media: '',
strategy: '',
};
if ( '' === suffix ) {
suffix = '.LICENSE.txt';
}
let fileToScan = file;
let licenseTxt = '';
switch ( typeof suffix ) {
case 'string':
if ( suffix ) {
licenseTxt = file + suffix;
}
break;
case 'function':
const generatedSuffix = suffix( file );
if ( generatedSuffix ) {
licenseTxt = generatedSuffix;
}
break;
}
// Create hash.
let hashOriginal = false;
if ( licenseTxt && fs.existsSync( licenseTxt ) ) {
fileToScan = licenseTxt;
hashOriginal = true;
}
// Search $file.assets.php file.
const deps = [];
if ( file.match( /\.js$/ ) ) {
const assetsFile = file.replace( /\.js$/, '.asset.php' );
if ( fs.existsSync( assetsFile ) ) {
// Scan PHP and get dependencies.
const assetsContent = fs.readFileSync( assetsFile, 'utf8' );
if ( assetsContent ) {
const match = assetsContent.match( /'dependencies' => array\(([^)]+)\)/ );
if ( match ) {
match[ 1 ].split( ',' ).forEach( ( dep ) => {
deps.push( dep.trim().replaceAll( "'", '' ) );
} );
}
}
}
}
const fileContent = fs.readFileSync( fileToScan, 'utf8' );
if ( fileContent ) {
// Add md5 hash string.
const md5hash = crypto.createHash( 'md5' );
if ( hashOriginal ) {
md5hash.update( fs.readFileSync( file ) );
} else {
md5hash.update( fileContent );
}
info.hash = md5hash.digest( 'hex' );
const scanned = scanHeader( info, fileContent, deps );
if ( ! scanned.media ) {
scanned.media = 'all';
}
return scanned;
} else {
return null;
}
}
/**
* Scan directory and extract dependencies.
*
* @param {string|string[]} dirs Directory file to scan.
* @param {String|Function} suffix Suffix for license file.
* @param {String} version Default version string.
* @returns {Array}
*/
function scanDir( dirs, suffix = '', version = '0.0.0' ) {
if ( 'string' === typeof dirs ) {
dirs = [ dirs ];
}
const result = [];
dirs.forEach( ( dir ) => {
const pattern = dir.replace( /\/$/, '' ) + '/**/*.*(css|js)';
const matches = glob.sync( pattern );
matches.forEach( ( file ) => {
result.push( grabDeps( file, suffix, version ) );
} );
} );
return result;
}
/**
* Dump dependencies in json file.
*
* @param {string|string[]} dirs Directory to scan.
* @param {String} dump File to dump.
* @param {String|Function} suffix Suffix for license file.
* @param {String} version Default version string.
*/
function dumpSetting( dirs, dump = './wp-dependencies.json', suffix = '', version = '0.0.0' ) {
const result = scanDir( dirs, suffix, version );
fs.writeFileSync( dump, JSON.stringify( result, null, "\t" ) );
}
/**
* Compile JS in directory.
*
* wp-scripts does not support nested js directory.
* This function compiles all js files in the directory and keep the directory structure.
*
* 1. Compile all ES6+ or JSX files in the directory.
* 2. Remove block directory.
* 3. Extract license header to license.txt.
*
* @param {string} srcDir Source directory.
* @param {string} destDir Target directory.
* @param {string[]} extensions Extensions to compile.
* @returns {Promise}
*/
function compileDirectory( srcDir, destDir, extensions = [ 'js', 'jsx' ] ) {
// Remove trailing slashes.
srcDir = srcDir.replace( /\/+$/, '' );
destDir = destDir.replace( /\/+$/, '' );
const globDir = extensions.map( ext => `${srcDir}/**/*.${ext}` );
if ( ! isWordPressScriptsAvailable() ) {
return Promise.reject( new Error( 'This function requires @wordpress/scripts.' ) );
}
return glob( globDir ).then( res => {
/** @type {Path[]} paths */
const paths = [];
res.forEach( ( path ) => {
addPath( paths, path );
} );
// Run build
const errors = [];
paths.forEach( ( p ) => {
try {
execSync( `wp-scripts build ${ p.path.join( ' ' ) } --output-path=${ p.dir.replace( srcDir, destDir ) }` );
} catch ( e ) {
console.log( e );
errors.push( p.path.join( ', ' ) );
}
} );
if ( errors.length ) {
throw new Error( `Failed to build: ${ errors.join( ', ' ) }` );
}
return paths;
} ).then( ( paths ) => {
// Remove all block json.
return glob( [ `${destDir}/**/blocks`, `${destDir}/**/*.asset.php` ] ).then( res => {
execSync( `rm -rf ${ res.join( ' ' ) }` );
return res.length;
} );
} ).then( () => {
// Put license.txt.
return glob( globDir ).then( res => {
const result = { total: res.length, extracted: 0};
res.map( ( path ) => {
result.total++;
if ( extractHeaderToLicense( path, srcDir, destDir ) ) {
result.extracted++;
}
} );
return result;
} );
} );
}
module.exports.scanHeader = scanHeader;
module.exports.grabDeps = grabDeps;
module.exports.scanDir = scanDir;
module.exports.dumpSetting = dumpSetting;
module.exports.compileDirectory = compileDirectory;