-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathindex.js
59 lines (52 loc) · 1.6 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
const Path = require('path');
const importFrom = require('import-from');
const resolvePkg = require('resolve-pkg');
const Globby = require('globby');
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
return Globby(
workspaces.map((ws) => {
return Path.posix.join(ws, 'package.json');
}),
{cwd}
).then((pJsons = []) => {
return pJsons.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) {
return require(Path.join(resolvePkg('lerna', {cwd}), 'package.json')).version;
}