Skip to content

Commit

Permalink
fix(core): include root package.json when reading yarn & lerna config
Browse files Browse the repository at this point in the history
When no --source options are provided and lerna.json or yarn workspaces
config are used to locate package.json files by default, the root
package.json file was previously not included.
  • Loading branch information
JamieMason committed Feb 16, 2020
1 parent beee8ad commit a7875cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/commands/lib/get-wrappers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('getWrappers', () => {

beforeEach(() => {
mock({
'package.json': JSON.stringify({ name: 'root' }),
'cli/a/package.json': JSON.stringify({ name: 'cli-a' }),
'cli/b/package.json': JSON.stringify({ name: 'cli-b' }),
'lerna.json': JSON.stringify({ packages: ['lerna/*'] }),
Expand All @@ -23,7 +24,10 @@ describe('getWrappers', () => {
});
});

const getShape = (name: string, filePath: string): SourceWrapper => ({ contents: { name }, filePath });
const getShape = (name: string, filePath: string): SourceWrapper => ({
contents: { name },
filePath: `${process.cwd()}/${filePath}`,
});

it('prefers CLI', () => {
const program = { source: ['cli/*/package.json'] };
Expand All @@ -36,6 +40,7 @@ describe('getWrappers', () => {
it('falls back to lerna.json if present', () => {
const program = { source: [] };
expect(getWrappers(program)).toEqual([
getShape('root', 'package.json'),
getShape('lerna-a', 'lerna/a/package.json'),
getShape('lerna-b', 'lerna/b/package.json'),
]);
Expand All @@ -45,6 +50,7 @@ describe('getWrappers', () => {
const program = { source: [] };
removeSync('lerna.json');
expect(getWrappers(program)).toEqual([
getShape('root', 'package.json'),
getShape('packages-a', 'packages/a/package.json'),
getShape('packages-b', 'packages/b/package.json'),
]);
Expand All @@ -66,6 +72,7 @@ describe('getWrappers', () => {
it('should resolve correctly', () => {
const program = { source: [] };
expect(getWrappers(program)).toEqual([
{ contents: { workspaces: ['packages/*'] }, filePath: `${process.cwd()}/package.json` },
getShape('packages-a', 'packages/a/package.json'),
getShape('packages-b', 'packages/b/package.json'),
]);
Expand Down
6 changes: 4 additions & 2 deletions src/commands/lib/get-wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ const getPatternsFromConfig = (fileName: string, propName: string): string[] | n
const filePath = resolve(process.cwd(), fileName);
const config = readJsonSync(filePath, { throws: false });
const isNonEmptyArray = config && config[propName] && config[propName].length > 0;
return isNonEmptyArray ? config[propName].map((dirPath: string) => join(dirPath, 'package.json')) : null;
return isNonEmptyArray
? [process.cwd()].concat(config[propName]).map((dirPath: string) => join(dirPath, 'package.json'))
: null;
};

const hasCliPatterns = (program: Options) => program.source && program.source.length > 0;
const getCliPatterns = (program: Options) => program.source;
const getYarnPatterns = () => getPatternsFromConfig('package.json', 'workspaces');
const getLernaPatterns = () => getPatternsFromConfig('lerna.json', 'packages');
const getDefaultPatterns = () => ALL_PATTERNS;
const resolvePattern = (pattern: string) => sync(pattern);
const resolvePattern = (pattern: string) => sync(pattern, { absolute: true });
const reduceFlatArray = (all: string[], next: string[]) => all.concat(next);
const createWrapper = (filePath: string) => ({ contents: readJsonSync(filePath), filePath });

Expand Down

0 comments on commit a7875cb

Please sign in to comment.