-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathget-packages.js
72 lines (65 loc) · 1.76 KB
/
get-packages.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
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
/**
* Absolute path to packages directory.
*
* @type {string}
*/
const PACKAGES_DIR = path.resolve( __dirname, '../../packages' );
/**
* Returns true if the given base file name for a file within the packages
* directory is itself a directory.
*
* @param {string} file Packages directory file.
*
* @return {boolean} Whether file is a directory.
*/
function isDirectory( file ) {
return fs.lstatSync( path.resolve( PACKAGES_DIR, file ) ).isDirectory();
}
/**
* Returns true if the given packages has "module" field.
*
* @param {string} file Packages directory file.
*
* @return {boolean} Whether file is a directory.
*/
function hasModuleField( file ) {
let pkg;
try {
pkg = require( path.resolve( PACKAGES_DIR, file, 'package.json' ) );
} catch {
// If, for whatever reason, the package's `package.json` cannot be read,
// consider it as an invalid candidate. In most cases, this can happen
// when lingering directories are left in the working path when changing
// to an older branch where a package did not yet exist.
return false;
}
return !! pkg.module;
}
/**
* Filter predicate, returning true if the given base file name is to be
* included in the build.
*
* @param {string} pkg File base name to test.
*
* @return {boolean} Whether to include file in build.
*/
function filterPackages( pkg ) {
return [ isDirectory, hasModuleField ].every( ( check ) => check( pkg ) );
}
/**
* Returns the absolute path of all WordPress packages
*
* @return {Array} Package paths
*/
function getPackages() {
return fs
.readdirSync( PACKAGES_DIR )
.filter( filterPackages )
.map( ( file ) => path.resolve( PACKAGES_DIR, file ) );
}
module.exports = getPackages;