Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): more detailed error message when version cannot be found #431

Merged
merged 1 commit into from Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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