Skip to content

Commit 77b90eb

Browse files
committed
feat(cli): read sources from lerna.json if present
Closes #11
1 parent a103d37 commit 77b90eb

8 files changed

Lines changed: 73 additions & 21 deletions

File tree

src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ export const SEMVER_ORDER = [
4444

4545
const DEFAULT_INDENT = ' ';
4646
const DEFAULT_SEMVER_RANGE = RANGE_EXACT;
47-
const MONOREPO_PATTERN = './package.json';
48-
const PACKAGES_PATTERN = './packages/*/package.json';
47+
const MONOREPO_PATTERN = 'package.json';
48+
const PACKAGES_PATTERN = 'packages/*/package.json';
4949
const ALL_PATTERNS = [MONOREPO_PATTERN, PACKAGES_PATTERN];
5050

5151
export const FIX_MISMATCHES = {

src/fix-mismatches.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const run = async (program: CommanderApi) => {
2626
.option(OPTION_INDENT.spec, OPTION_INDENT.description)
2727
.parse(process.argv);
2828

29-
const pkgs = await getPackages(program);
29+
const pkgs = getPackages(program);
3030
const dependencyTypes = getDependencyTypes(program);
3131
const indent = getIndent(program);
3232
const mismatchedVersionsByName = getMismatchedVersionsByName(

src/format.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const run = async (program: CommanderApi) => {
6868
.option(OPTION_INDENT.spec, OPTION_INDENT.description)
6969
.parse(process.argv);
7070

71-
const pkgs = await getPackages(program);
71+
const pkgs = getPackages(program);
7272
const indent = getIndent(program);
7373

7474
await Promise.all(

src/lib/get-packages.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import mock = require('mock-fs');
2+
import { resolve } from 'path';
3+
import { getMockCommander } from '../../test/helpers';
4+
import { getPackages } from './get-packages';
5+
import { removeSync } from 'fs-extra';
6+
7+
describe('getPackages', () => {
8+
afterEach(() => {
9+
mock.restore();
10+
});
11+
12+
beforeEach(() => {
13+
mock({
14+
'cli/a/package.json': JSON.stringify({ name: 'cli-a' }),
15+
'cli/b/package.json': JSON.stringify({ name: 'cli-b' }),
16+
'lerna.json': JSON.stringify({ packages: ['lerna/*'] }),
17+
'lerna/a/package.json': JSON.stringify({ name: 'lerna-a' }),
18+
'lerna/b/package.json': JSON.stringify({ name: 'lerna-b' }),
19+
'packages/a/package.json': JSON.stringify({ name: 'packages-a' }),
20+
'packages/b/package.json': JSON.stringify({ name: 'packages-b' })
21+
});
22+
});
23+
24+
it('prefers CLI', () => {
25+
const program = getMockCommander(['cli/*/package.json']);
26+
expect(getPackages(program)).toEqual([
27+
{ data: { name: 'cli-a' }, path: 'cli/a/package.json' },
28+
{ data: { name: 'cli-b' }, path: 'cli/b/package.json' }
29+
]);
30+
});
31+
32+
it('falls back to lerna.json if present', () => {
33+
const program = getMockCommander([]);
34+
expect(getPackages(program)).toEqual([
35+
{ data: { name: 'lerna-a' }, path: 'lerna/a/package.json' },
36+
{ data: { name: 'lerna-b' }, path: 'lerna/b/package.json' }
37+
]);
38+
});
39+
40+
it('resorts to defaults', () => {
41+
const program = getMockCommander([]);
42+
removeSync('lerna.json');
43+
expect(getPackages(program)).toEqual([
44+
{ data: { name: 'packages-a' }, path: 'packages/a/package.json' },
45+
{ data: { name: 'packages-b' }, path: 'packages/b/package.json' }
46+
]);
47+
});
48+
});

src/lib/get-packages.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
import fs = require('fs-extra');
1+
import { readJsonSync } from 'fs-extra';
22
import globby = require('globby');
3+
import { join, resolve } from 'path';
34
import { OPTION_SOURCES } from '../constants';
45
import { CommanderApi, IManifestDescriptor } from '../typings';
56

6-
export const getSources = (program: CommanderApi): string[] =>
7-
program.source && program.source.length
8-
? program.source
9-
: OPTION_SOURCES.default;
7+
export const getSources = (program: CommanderApi): string[] => {
8+
if (program.source && program.source.length) {
9+
return program.source;
10+
}
11+
const lernaPath = resolve(process.cwd(), 'lerna.json');
12+
const lerna = readJsonSync(lernaPath, { throws: false });
13+
if (lerna && lerna.packages && lerna.packages.length) {
14+
return lerna.packages.map((glob: string) => join(glob, 'package.json'));
15+
}
16+
return OPTION_SOURCES.default;
17+
};
1018

11-
export const getPackages = async (
12-
program: CommanderApi
13-
): Promise<IManifestDescriptor[]> =>
14-
Promise.all(
15-
(await globby(getSources(program))).map(async (filePath) => ({
16-
data: await fs.readJSON(filePath),
17-
path: filePath
18-
}))
19-
);
19+
export const getPackages = (program: CommanderApi): IManifestDescriptor[] =>
20+
globby.sync(getSources(program)).map((filePath) => ({
21+
data: readJsonSync(filePath),
22+
path: filePath
23+
}));

src/list-mismatches.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const run = async (program: CommanderApi) => {
2121
.parse(process.argv);
2222

2323
const dependencyTypes = getDependencyTypes(program);
24-
const pkgs = await getPackages(program);
24+
const pkgs = getPackages(program);
2525
const mismatchedVersionsByName = getMismatchedVersionsByName(
2626
dependencyTypes,
2727
pkgs

src/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const run = async (program: CommanderApi) => {
2121
.parse(process.argv);
2222

2323
const dependencyTypes = getDependencyTypes(program);
24-
const pkgs = await getPackages(program);
24+
const pkgs = getPackages(program);
2525
const versionsByName = getVersionsByName(dependencyTypes, pkgs);
2626

2727
_.each(versionsByName, (versions, name) => {

src/set-semver-ranges.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const run = async (program: CommanderApi) => {
3333
const semverRange: string =
3434
program.semverRange || OPTION_SEMVER_RANGE.default;
3535

36-
const pkgs = await getPackages(program);
36+
const pkgs = getPackages(program);
3737
const dependencyTypes = getDependencyTypes(program);
3838
const indent = getIndent(program);
3939

0 commit comments

Comments
 (0)