UNPKG

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