|
| 1 | +const Path = require('path'); |
1 | 2 | const importFrom = require('import-from');
|
| 3 | +const resolvePkg = require('resolve-pkg'); |
| 4 | +const semver = require('semver'); |
2 | 5 |
|
3 | 6 | module.exports = {
|
4 | 7 | utils: {getPackages},
|
5 | 8 | rules: {
|
6 |
| - 'scope-enum': async ctx => [2, 'always', await getPackages(ctx)] |
| 9 | + 'scope-enum': ctx => |
| 10 | + getPackages(ctx).then(packages => [2, 'always', packages]) |
7 | 11 | }
|
8 | 12 | };
|
9 | 13 |
|
10 |
| -async function getPackages(context) { |
11 |
| - const ctx = context || {}; |
12 |
| - const cwd = ctx.cwd || process.cwd(); |
| 14 | +function getPackages(context) { |
| 15 | + return Promise.resolve() |
| 16 | + .then(() => { |
| 17 | + const ctx = context || {}; |
| 18 | + const cwd = ctx.cwd || process.cwd(); |
| 19 | + const lernaVersion = getLernaVersion(cwd); |
13 | 20 |
|
14 |
| - const Project = importFrom(cwd, '@lerna/project'); |
| 21 | + if (semver.lt(lernaVersion, '3.0.0')) { |
| 22 | + const Repository = importFrom(cwd, 'lerna/lib/Repository'); |
| 23 | + const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities'); |
15 | 24 |
|
16 |
| - const project = new Project(cwd); |
17 |
| - const packages = await project.getPackages(); |
| 25 | + const repository = new Repository(cwd); |
| 26 | + return PackageUtilities.getPackages({ |
| 27 | + packageConfigs: repository.packageConfigs, |
| 28 | + rootPath: cwd |
| 29 | + }); |
| 30 | + } |
18 | 31 |
|
19 |
| - return packages |
20 |
| - .map(pkg => pkg.name) |
21 |
| - .map(name => (name.charAt(0) === '@' ? name.split('/')[1] : name)); |
| 32 | + const Project = importFrom(cwd, '@lerna/project'); |
| 33 | + const project = new Project(cwd); |
| 34 | + return project.getPackages(); |
| 35 | + }) |
| 36 | + .then(packages => { |
| 37 | + return packages |
| 38 | + .map(pkg => pkg.name) |
| 39 | + .map(name => (name.charAt(0) === '@' ? name.split('/')[1] : name)); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +function getLernaVersion(cwd) { |
| 44 | + return require(Path.join(resolvePkg('lerna', {cwd}), 'package.json')).version; |
22 | 45 | }
|
0 commit comments