Skip to content

Commit

Permalink
fix(publish): Include all packages during global major bump (#1391)
Browse files Browse the repository at this point in the history
Fixes #1383
  • Loading branch information
bchenSyd authored and evocateur committed May 3, 2018
1 parent 75b91bd commit 9cebed1
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,104 @@ packages/package-5/CHANGELOG.md
packages/package-5/package.json"
`;

exports[`PublishCommand fixed mode major bump should bump all packages: commit 1`] = `
"v2.0.0
HEAD -> master, tag: v2.0.0
diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2 +2 @@
- \\"version\\": \\"1.0.0\\"
+ \\"version\\": \\"2.0.0\\"
diff --git a/packages/package-1/package.json b/packages/package-1/package.json
index SHA..SHA 100644
--- a/packages/package-1/package.json
+++ b/packages/package-1/package.json
@@ -3 +3 @@
- \\"version\\": \\"1.0.0\\"
+ \\"version\\": \\"2.0.0\\"
diff --git a/packages/package-2/package.json b/packages/package-2/package.json
index SHA..SHA 100644
--- a/packages/package-2/package.json
+++ b/packages/package-2/package.json
@@ -3 +3 @@
- \\"version\\": \\"1.0.0\\",
+ \\"version\\": \\"2.0.0\\",
@@ -5 +5 @@
- \\"package-1\\": \\"^1.0.0\\"
+ \\"package-1\\": \\"^2.0.0\\"
diff --git a/packages/package-3/package.json b/packages/package-3/package.json
index SHA..SHA 100644
--- a/packages/package-3/package.json
+++ b/packages/package-3/package.json
@@ -3 +3 @@
- \\"version\\": \\"1.0.0\\",
+ \\"version\\": \\"2.0.0\\",
@@ -8 +8 @@
- \\"package-2\\": \\"^1.0.0\\"
+ \\"package-2\\": \\"^2.0.0\\"
diff --git a/packages/package-4/package.json b/packages/package-4/package.json
index SHA..SHA 100644
--- a/packages/package-4/package.json
+++ b/packages/package-4/package.json
@@ -3 +3 @@
- \\"version\\": \\"1.0.0\\",
+ \\"version\\": \\"2.0.0\\",
diff --git a/packages/package-5/package.json b/packages/package-5/package.json
index SHA..SHA 100644
--- a/packages/package-5/package.json
+++ b/packages/package-5/package.json
@@ -4 +4 @@
- \\"package-1\\": \\"^1.0.0\\"
+ \\"package-1\\": \\"^2.0.0\\"
@@ -7 +7 @@
- \\"package-3\\": \\"^1.0.0\\"
+ \\"package-3\\": \\"^2.0.0\\"
@@ -10 +10 @@
- \\"version\\": \\"1.0.0\\"
+ \\"version\\": \\"2.0.0\\""
`;

exports[`PublishCommand fixed mode major bump should bump all packages: updated packages 1`] = `
Object {
"package-1": "2.0.0",
"package-2": "2.0.0",
"package-3": "2.0.0",
"package-4": "2.0.0",
"package-5": "2.0.0",
}
`;

exports[`PublishCommand fixed mode minor bump should only bump changed: commit 1`] = `
"v1.0.1
HEAD -> master, tag: v1.0.1
diff --git a/lerna.json b/lerna.json
index SHA..SHA 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2 +2 @@
- \\"version\\": \\"1.0.0\\"
+ \\"version\\": \\"1.0.1\\"
diff --git a/packages/package-1/package.json b/packages/package-1/package.json
index SHA..SHA 100644
--- a/packages/package-1/package.json
+++ b/packages/package-1/package.json
@@ -3 +3 @@
- \\"version\\": \\"1.0.0\\"
+ \\"version\\": \\"1.0.1\\""
`;

exports[`PublishCommand fixed mode minor bump should only bump changed: updated packages 1`] = `
Object {
"package-1": "1.0.1",
}
`;

exports[`PublishCommand indepdendent mode with --cd-version should bump to prerelease versions with --cd-version prerelease (no --preid): commit 1`] = `
"Publish
Expand Down Expand Up @@ -1081,6 +1179,7 @@ Object {
"package-3": "2.0.0",
"package-4": "2.0.0",
"package-5": "2.0.0",
"package-6": "2.0.0",
"package-7": "2.0.0",
}
`;
50 changes: 49 additions & 1 deletion commands/publish/__tests__/publish-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ jest.mock("@lerna/conventional-commits");
jest.mock("@lerna/npm-dist-tag");
jest.mock("@lerna/npm-publish");
jest.mock("@lerna/run-lifecycle");
jest.mock("@lerna/collect-updates");
jest.mock("../lib/git-push");
jest.mock("../lib/is-behind-upstream");

Expand All @@ -17,10 +18,12 @@ const semver = require("semver");
// mocked or stubbed modules
const writePkg = require("write-pkg");
const ConventionalCommitUtilities = require("@lerna/conventional-commits");
const Package = require("@lerna/package");
const PromptUtilities = require("@lerna/prompt");
const npmDistTag = require("@lerna/npm-dist-tag");
const npmPublish = require("@lerna/npm-publish");
const runLifecycle = require("@lerna/run-lifecycle");
const collectUpdates = require("@lerna/collect-updates");
const gitPush = require("../lib/git-push");
const isBehindUpstream = require("../lib/is-behind-upstream");

Expand All @@ -37,6 +40,7 @@ const showCommit = require("@lerna-test/show-commit");
// file under test
const lernaPublish = require("@lerna-test/command-runner")(require("../command"));

const collectUpdatesActual = require.requireActual("@lerna/collect-updates");
// assertion helpers
const gitCommitMessage = cwd =>
execa("git", ["show", "--no-patch", "--pretty=%B"], { cwd }).then(result => result.stdout.trim());
Expand Down Expand Up @@ -70,6 +74,7 @@ describe("PublishCommand", () => {
// we've already tested these utilities elsewhere
npmPublish.mockResolvedValue();
runLifecycle.mockImplementation(() => Promise.resolve());
collectUpdates.mockImplementation(collectUpdatesActual);
npmDistTag.check.mockReturnValue(true);

PromptUtilities.select.mockResolvedValue("1.0.1");
Expand Down Expand Up @@ -115,6 +120,49 @@ describe("PublishCommand", () => {
});
});

/** =========================================================================
* FIXED
* ======================================================================= */

describe("fixed mode", () => {
it("minor bump should only bump changed", async () => {
const testDir = await initFixture("normal");
const package1 = new Package(
{
name: "package-1",
version: "1.0.0",
},
`${testDir}/packages/package-1`
);
collectUpdates.mockImplementationOnce(() => [{ pkg: package1, localDependencies: [] }]);

await lernaPublish(testDir)();

const patch = await showCommit(testDir);
expect(patch).toMatchSnapshot("commit");
expect(updatedPackageVersions(testDir)).toMatchSnapshot("updated packages");
});

it("major bump should bump all packages", async () => {
const testDir = await initFixture("normal");
PromptUtilities.select.mockResolvedValueOnce("2.0.0");
const package1 = new Package(
{
name: "package-1",
version: "1.0.0",
},
`${testDir}/packages/package-1`
);
collectUpdates.mockImplementationOnce(() => [{ pkg: package1, localDependencies: [] }]);

await lernaPublish(testDir)();

const patch = await showCommit(testDir);
expect(patch).toMatchSnapshot("commit");
expect(updatedPackageVersions(testDir)).toMatchSnapshot("updated packages");
});
});

/** =========================================================================
* INDEPENDENT
* ======================================================================= */
Expand Down Expand Up @@ -1122,7 +1170,7 @@ describe("PublishCommand", () => {
expect(updatedPackageJSON("package-5").dependencies).toMatchObject({
"package-4": "^2.0.0",
// FIXME: awkward... (_intentional_, for now)
"package-6": "^1.0.0",
"package-6": "^2.0.0",
});
// private packages do not need local version resolution
expect(updatedPackageJSON("package-7").dependencies).toMatchObject({
Expand Down
44 changes: 43 additions & 1 deletion commands/publish/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ function factory(argv) {
return new PublishCommand(argv);
}

function isBreakingChange(currentVersion, nextVersion) {
const releaseType = semver.diff(currentVersion, nextVersion);
switch (releaseType) { //eslint-disable-line
case "major":
return true;
case "minor":
return semver.lt(currentVersion, "1.0.0");
case "patch":
return semver.lt(currentVersion, "0.1.0");
case "premajor":
case "preminor":
case "prepatch":
case "prerelease":
return false;
}
}
class PublishCommand extends Command {
get defaultOptions() {
return Object.assign({}, super.defaultOptions, {
Expand Down Expand Up @@ -147,7 +163,33 @@ class PublishCommand extends Command {
const tasks = [
() => this.getVersionsForUpdates(),
versions => {
this.updatesVersions = versions;
if (
this.project.isIndependent() ||
versions.size === this.filteredPackages.size ||
this.options.canary
) {
// independent, force-publish=*, or canary, we don't bump all
this.updatesVersions = versions;
} else {
let hasBreakingChange;
for (const [name, bump] of versions) {
hasBreakingChange =
hasBreakingChange || isBreakingChange(this.packageGraph.get(name).version, bump);
}
if (hasBreakingChange) {
const packages =
this.filteredPackages.length === this.packageGraph.size
? this.packageGraph
: new Map(this.filteredPackages.map(({ name }) => [name, this.packageGraph.get(name)]));
this.updates = Array.from(packages.values());
this.updatesVersions = new Map();
this.updates.forEach(pkg => {
this.updatesVersions.set(pkg.name, this.globalVersion);
});
} else {
this.updatesVersions = versions;
}
}
},
() => this.confirmVersions(),
];
Expand Down
7 changes: 7 additions & 0 deletions integration/__snapshots__/lerna-publish.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ index SHA..SHA 100644
@@ -3 +3 @@
- "version": "1.0.0",
+ "version": "2.0.0",
diff --git a/packages/package-6/package.json b/packages/package-6/package.json
index SHA..SHA 100644
--- a/packages/package-6/package.json
+++ b/packages/package-6/package.json
@@ -3 +3 @@
- "version": "1.0.0",
+ "version": "2.0.0",
diff --git a/packages/package-7/package.json b/packages/package-7/package.json
index SHA..SHA 100644
--- a/packages/package-7/package.json
Expand Down

0 comments on commit 9cebed1

Please sign in to comment.