From 7dee5237a07e0fe912b692747febc7eaf29b3869 Mon Sep 17 00:00:00 2001 From: amorscher Date: Tue, 6 Dec 2022 10:17:00 +0100 Subject: [PATCH] fix(core): more detailed error message when version cannot be found (#3424) --- core/command/__tests__/command.test.js | 30 ++++++++++++++++++++++++++ core/command/index.js | 6 +++++- core/project/index.js | 2 ++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/core/command/__tests__/command.test.js b/core/command/__tests__/command.test.js index 063ca2ba9c9..38209a1109f 100644 --- a/core/command/__tests__/command.test.js +++ b/core/command/__tests__/command.test.js @@ -388,6 +388,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", + }) + ); + }); + it("throws ENOWORKSPACES when npm client is pnpm and useWorkspaces is not true", async () => { const cwd = await initFixture("pnpm"); diff --git a/core/command/index.js b/core/command/index.js index e74f4198305..399b883455f 100644 --- a/core/command/index.js +++ b/core/command/index.js @@ -240,10 +240,14 @@ class Command { throw new ValidationError("ENOPKG", "`package.json` does not exist, have you run `lerna init`?"); } - if (!this.project.version) { + 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("ENOVERSION", "Required property version does not exist in `lerna.json`"); + } + if (this.options.independent && !this.project.isIndependent()) { throw new ValidationError( "EVERSIONMODE", diff --git a/core/project/index.js b/core/project/index.js index 4448b7bb269..9d0e7ebe6e6 100644 --- a/core/project/index.js +++ b/core/project/index.js @@ -66,6 +66,7 @@ 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"), }; @@ -96,6 +97,7 @@ class Project { /** @type {ProjectConfig} */ this.config = loaded.config; + this.configNotFound = loaded.configNotFound; this.rootConfigLocation = loaded.filepath; this.rootPath = path.dirname(loaded.filepath);