Skip to content

Commit

Permalink
fix(core): more detailed error message when version cannot be found (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Dec 23, 2022
1 parent 246ac57 commit 7f3dffb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
30 changes: 30 additions & 0 deletions packages/core/src/__tests__/command.spec.ts
Expand Up @@ -384,6 +384,36 @@ describe('core-command', () => {
})
);
});

it('throws ENOVERSION when lerna.json is empty', async () => {
const cwd = await initFixture('basic');

const lernaConfigPath = path.join(cwd, 'lerna.json');
await fs.writeJson(lernaConfigPath, {});

await expect(testFactory({ cwd })).rejects.toThrow(
expect.objectContaining({
prefix: 'ENOVERSION',
})
);
});

it('throws ENOVERSION when no version property exists in lerna.json', async () => {
const cwd = await initFixture('basic');

const lernaConfigPath = path.join(cwd, 'lerna.json');
const lernaConfig = await fs.readJson(lernaConfigPath);
delete lernaConfig.version;
await fs.writeJson(lernaConfigPath, {
...lernaConfig,
});

await expect(testFactory({ cwd })).rejects.toThrow(
expect.objectContaining({
prefix: 'ENOVERSION',
})
);
});
});

describe('loglevel with verbose option true', () => {
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/command.ts
Expand Up @@ -285,11 +285,12 @@ export class Command<T extends AvailableCommandOption> {
);
}

if (this.project.configNotFound) {
throw new ValidationError('ENOLERNA', '`lerna.json` does not exist, have you run `lerna init`?');
}

if (!this.project.version) {
throw new ValidationError(
'ENOLERNA',
'No `lerna.json` file exist, please create one in the root of your project.'
);
throw new ValidationError('ENOVERSION', 'Required property version does not exist in `lerna.json`');
}

if ((this.options as InitCommandOption).independent && !this.project.isIndependent()) {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/project/project.ts
Expand Up @@ -22,6 +22,7 @@ import { ProjectConfig, RawManifest } from '../models';
*/
export class Project {
config: ProjectConfig;
configNotFound: boolean;
rootConfigLocation: string;
rootPath: string;
static PACKAGE_GLOB = 'packages/*';
Expand All @@ -40,6 +41,7 @@ export class Project {
// No need to distinguish between missing and empty,
// saves a lot of noisy guards elsewhere
config: {},
configNotFound: true,
// path.resolve(".", ...) starts from process.cwd()
filepath: path.resolve(cwd || '.', 'lerna.json'),
};
Expand All @@ -66,6 +68,7 @@ export class Project {
}

this.config = loaded?.config;
this.configNotFound = loaded?.configNotFound;
this.rootConfigLocation = loaded?.filepath ?? '';
this.rootPath = path.dirname(loaded?.filepath ?? '');

Expand Down

0 comments on commit 7f3dffb

Please sign in to comment.