1 | const Path = require('path');
|
2 | const importFrom = require('import-from');
|
3 | const resolvePkg = require('resolve-pkg');
|
4 | const Globby = require('globby');
|
5 | const semver = require('semver');
|
6 |
|
7 | module.exports = {
|
8 | utils: {getPackages},
|
9 | rules: {
|
10 | 'scope-enum': (ctx) =>
|
11 | getPackages(ctx).then((packages) => [2, 'always', packages]),
|
12 | },
|
13 | };
|
14 |
|
15 | function getPackages(context) {
|
16 | return Promise.resolve()
|
17 | .then(() => {
|
18 | const ctx = context || {};
|
19 | const cwd = ctx.cwd || process.cwd();
|
20 |
|
21 | const {workspaces} = require(Path.join(cwd, 'package.json'));
|
22 | if (Array.isArray(workspaces) && workspaces.length) {
|
23 |
|
24 | return Globby(
|
25 | workspaces.map((ws) => {
|
26 | return Path.posix.join(ws, 'package.json');
|
27 | }),
|
28 | {cwd, ignore: ['**/node_modules/**']}
|
29 | ).then((pJsons = []) => {
|
30 | return pJsons.map((pJson) => require(Path.join(cwd, pJson)));
|
31 | });
|
32 | }
|
33 |
|
34 | const lernaVersion = getLernaVersion(cwd);
|
35 | if (semver.lt(lernaVersion, '3.0.0')) {
|
36 | const Repository = importFrom(cwd, 'lerna/lib/Repository');
|
37 | const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');
|
38 |
|
39 | const repository = new Repository(cwd);
|
40 | return PackageUtilities.getPackages({
|
41 | packageConfigs: repository.packageConfigs,
|
42 | rootPath: cwd,
|
43 | });
|
44 | }
|
45 |
|
46 | const {getPackages} = importFrom(cwd, '@lerna/project');
|
47 | return getPackages(cwd);
|
48 | })
|
49 | .then((packages) => {
|
50 | return packages
|
51 | .map((pkg) => pkg.name)
|
52 | .filter(Boolean)
|
53 | .map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));
|
54 | });
|
55 | }
|
56 |
|
57 | function getLernaVersion(cwd) {
|
58 | return require(Path.join(resolvePkg('lerna', {cwd}), 'package.json')).version;
|
59 | }
|