Skip to content

Commit

Permalink
fix(version): Ensure --create-release environment variables are prese…
Browse files Browse the repository at this point in the history
…nt during initialization
  • Loading branch information
evocateur committed Feb 10, 2021
1 parent 63f18ba commit 2d0a97a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
14 changes: 14 additions & 0 deletions commands/version/__tests__/version-create-release.test.js
Expand Up @@ -50,6 +50,20 @@ describe.each([
expect(client.releases.size).toBe(0);
});

it("throws an error if environment variables are not present", async () => {
const cwd = await initFixture("normal");
const command = lernaVersion(cwd)("--create-release", type, "--conventional-commits");
const message = `Environment variables for ${type} are missing!`;

client.mockImplementationOnce(() => {
throw new Error(message);
});

await expect(command).rejects.toThrow(message);

expect(client.releases.size).toBe(0);
});

it("marks a version as a pre-release if it contains a valid part", async () => {
const cwd = await initFixture("normal");

Expand Down
13 changes: 7 additions & 6 deletions commands/version/index.js
Expand Up @@ -31,7 +31,7 @@ const { remoteBranchExists } = require("./lib/remote-branch-exists");
const { isBreakingChange } = require("./lib/is-breaking-change");
const { isAnythingCommitted } = require("./lib/is-anything-committed");
const { makePromptVersion } = require("./lib/prompt-version");
const { createRelease } = require("./lib/create-release");
const { createRelease, createReleaseClient } = require("./lib/create-release");
const { updateLockfileVersion } = require("./lib/update-lockfile-version");

module.exports = factory;
Expand Down Expand Up @@ -76,14 +76,15 @@ class VersionCommand extends Command {
this.pushToRemote = gitTagVersion && amend !== true && push;
// never automatically push to remote when amending a commit

this.createRelease = this.pushToRemote && this.options.createRelease;
this.releaseClient =
this.pushToRemote && this.options.createRelease && createReleaseClient(this.options.createRelease);
this.releaseNotes = [];

if (this.createRelease && this.options.conventionalCommits !== true) {
if (this.releaseClient && this.options.conventionalCommits !== true) {
throw new ValidationError("ERELEASE", "To create a release, you must enable --conventional-commits");
}

if (this.createRelease && this.options.changelog === false) {
if (this.releaseClient && this.options.changelog === false) {
throw new ValidationError("ERELEASE", "To create a release, you cannot pass --no-changelog");
}

Expand Down Expand Up @@ -274,11 +275,11 @@ class VersionCommand extends Command {
this.logger.info("execute", "Skipping git push");
}

if (this.createRelease) {
if (this.releaseClient) {
this.logger.info("execute", "Creating releases...");
tasks.push(() =>
createRelease(
this.options.createRelease,
this.releaseClient,
{ tags: this.tags, releaseNotes: this.releaseNotes },
{ gitRemote: this.options.gitRemote, execOpts: this.execOpts }
)
Expand Down
8 changes: 4 additions & 4 deletions commands/version/lib/create-release.js
Expand Up @@ -7,11 +7,12 @@ const { createGitHubClient, parseGitRepo } = require("@lerna/github-client");
const { ValidationError } = require("@lerna/validation-error");

module.exports.createRelease = createRelease;
module.exports.createReleaseClient = createReleaseClient;

/**
* @param {'github' | 'gitlab'} type
*/
function createClient(type) {
function createReleaseClient(type) {
switch (type) {
case "gitlab":
return createGitLabClient();
Expand All @@ -24,13 +25,12 @@ function createClient(type) {
}

/**
* @param {'github' | 'gitlab'} type
* @param {ReturnType<typeof createReleaseClient>} client
* @param {{ tags: string[]; releaseNotes: { name: string; notes: string; }[] }} commandProps
* @param {{ gitRemote: string; execOpts: import("@lerna/child-process").ExecOpts }} opts
*/
function createRelease(type, { tags, releaseNotes }, { gitRemote, execOpts }) {
function createRelease(client, { tags, releaseNotes }, { gitRemote, execOpts }) {
const repo = parseGitRepo(gitRemote, execOpts);
const client = createClient(type);

return Promise.all(
releaseNotes.map(({ notes, name }) => {
Expand Down

0 comments on commit 2d0a97a

Please sign in to comment.