Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,24 @@ Useful in [Continuous integration (CI)](https://en.wikipedia.org/wiki/Continuous
#### --cd-version

```sh
$ lerna publish --cd-version (patch | major | minor)
$ lerna publish --cd-version (patch | minor | major | prepatch | preminor | premajor | prerelease)
# uses the next semantic version(s) value and this skips `Select a new version for...` prompt
```

When run with this flag, `publish` will skip the version selection prompt (in independent mode) and use the next specified semantic version.
You must still use the `--yes` flag to avoid all prompts. This is useful when build systems need
to publish without command prompts. Works in both normal and independent modes.

#### --pre-id

```sh
$ lerna publish --cd-version (prepatch | preminor | premajor | prerelease) --pre-id beta
# prefixes the prerelease tag with given identifier
```

This flag works with connection with `--cd-version pre*` and allows to specify a meta suffix: `alpha`, `beta`, `rc`, or any other string (ex: `1.0.0` becomes `2.0.0-beta.0` after `lerna publish --cd-version premajor --preid beta`).
By default, the current meta-suffix will be used, if any.

#### --repo-version

```sh
Expand Down
34 changes: 29 additions & 5 deletions src/commands/PublishCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ export const command = "publish";

export const describe = "Publish packages in the current project.";

const cdVersionOptions = [
"major",
"minor",
"patch",
"premajor",
"preminor",
"prepatch",
"prerelease"
];
// 'major', 'minor', ... , or 'prerelease'
const cdVersionOptionsString =
`${cdVersionOptions.slice(0, -1).join(", ")}, or ${cdVersionOptions[cdVersionOptions.length - 1]}`;

export const builder = {
"canary": {
group: "Command Options:",
Expand All @@ -33,16 +46,23 @@ export const builder = {
},
"cd-version": {
group: "Command Options:",
describe: "Skip the version selection prompt and increment semver 'major', 'minor', or 'patch'.",
describe: `Skip the version selection prompt and increment semver ${cdVersionOptionsString}.`,
type: "string",
requiresArg: true,
coerce: (choice) => {
if (!["major", "minor", "patch"].some((inc) => choice === inc)) {
throw new Error(`--cd-version must be one of 'major', 'minor', or 'patch', got '${choice}'`);
if (!cdVersionOptions.some((inc) => choice === inc)) {
throw new Error(`--cd-version must be one of ${cdVersionOptionsString}, got '${choice}'`);
}
return choice;
},
},
"preid": {
group: "Command Options:",
describe:
"Set an identifier to be used to prefix premajor, preminor, prepatch or prerelease version increments.",
type: "string",
requiresArg: true,
},
"conventional-commits": {
group: "Command Options:",
describe: "Use angular conventional-commit format to determine version bump and generate CHANGELOG.",
Expand Down Expand Up @@ -249,14 +269,18 @@ export default class PublishCommand extends Command {
const versions = {};

this.updates.forEach((update) => {
versions[update.package.name] = semver.inc(update.package.version, this.options.cdVersion);
versions[update.package.name] = semver.inc(
update.package.version,
this.options.cdVersion,
this.options.preid
);
});

return callback(null, { versions });
}

// Otherwise bump the global version
const version = semver.inc(this.globalVersion, this.options.cdVersion);
const version = semver.inc(this.globalVersion, this.options.cdVersion, this.options.preid);
return callback(null, { version });
}

Expand Down
66 changes: 66 additions & 0 deletions test/PublishCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,34 @@ describe("PublishCommand", () => {
}
}));
});

it("should prefix pre* versions with pre-id when specified", (done) => {
const publishCommand = new PublishCommand([], {
cdVersion: "preminor",
preid: "alpha",
}, testDir);

publishCommand.runValidations();
publishCommand.runPreparations();

publishCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);

try {
if (pathExists.sync(path.join(testDir, "lerna-debug.log"))) {
// TODO: there has to be a better way to do this
throw new Error(fs.readFileSync(path.join(testDir, "lerna-debug.log"), "utf8"));
}

expect(PromptUtilities.select).not.toBeCalled();
expect(gitCommitMessage()).toBe("v1.1.0-alpha.0");

done();
} catch (ex) {
done.fail(ex);
}
}));
});
});

/** =========================================================================
Expand Down Expand Up @@ -907,6 +935,44 @@ describe("PublishCommand", () => {
}
}));
});

it("should prefix pre* versions with pre-id when specified", (done) => {
const publishCommand = new PublishCommand([], {
independent: true,
cdVersion: "prepatch",
preid: "beta"
}, testDir);

publishCommand.runValidations();
publishCommand.runPreparations();

publishCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);

try {
if (pathExists.sync(path.join(testDir, "lerna-debug.log"))) {
// TODO: there has to be a better way to do this
throw new Error(fs.readFileSync(path.join(testDir, "lerna-debug.log"), "utf8"));
}

expect(PromptUtilities.select).not.toBeCalled();
expect(gitCommitMessage()).toMatchSnapshot("[independent --cd-version] git commit message");
expect(updatedPackageJSON("package-2").dependencies).toMatchObject({
"package-1": "^1.0.1-beta.0",
});
expect(updatedPackageJSON("package-3").devDependencies).toMatchObject({
"package-2": "^2.0.1-beta.0",
});
expect(updatedPackageJSON("package-4").dependencies).toMatchObject({
"package-1": "^0.0.0",
});

done();
} catch (ex) {
done.fail(ex);
}
}));
});
});

/** =========================================================================
Expand Down
10 changes: 10 additions & 0 deletions test/__snapshots__/PublishCommand.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ exports[`[independent --cd-version] git commit message 1`] = `
- [email protected]"
`;

exports[`[independent --cd-version] git commit message 2`] = `
"Publish

- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]"
`;

exports[`[independent --conventional-commits] git adds changed files 1`] = `
Array [
"packages/package-1/CHANGELOG.md",
Expand Down