-
Notifications
You must be signed in to change notification settings - Fork 913
/
index.js
67 lines (57 loc) · 1.82 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
const glob = require('glob');
const Path = require('path');
const importFrom = require('import-from');
const semver = require('semver');
module.exports = {
utils: {getPackages},
rules: {
'scope-enum': (ctx) =>
getPackages(ctx).then((packages) => [2, 'always', packages]),
},
};
function getPackages(context) {
return Promise.resolve()
.then(() => {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();
const {workspaces} = require(Path.join(cwd, 'package.json'));
if (Array.isArray(workspaces) && workspaces.length) {
// use yarn workspaces
const wsGlobs = workspaces.flatMap((ws) => {
const path = Path.posix.join(ws, 'package.json');
return glob.sync(path, {cwd, ignore: ['**/node_modules/**']});
});
return wsGlobs.map((pJson) => require(Path.join(cwd, pJson)));
}
const lernaVersion = getLernaVersion(cwd);
if (semver.lt(lernaVersion, '3.0.0')) {
const Repository = importFrom(cwd, 'lerna/lib/Repository');
const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');
const repository = new Repository(cwd);
return PackageUtilities.getPackages({
packageConfigs: repository.packageConfigs,
rootPath: cwd,
});
}
const {getPackages} = importFrom(cwd, '@lerna/project');
return getPackages(cwd);
})
.then((packages) => {
return packages
.map((pkg) => pkg.name)
.filter(Boolean)
.map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));
});
}
function getLernaVersion(cwd) {
const moduleEntrypoint = require.resolve('lerna', {
paths: [cwd],
});
const moduleDir = Path.join(
moduleEntrypoint.slice(0, moduleEntrypoint.lastIndexOf('node_modules')),
'node_modules',
'lerna'
);
const modulePackageJson = Path.join(moduleDir, 'package.json');
return require(modulePackageJson).version;
}