Skip to content

Commit

Permalink
Add support for workspace:~ and workspace:^ (#585)
Browse files Browse the repository at this point in the history
* Add support for workspace version alias

* Tests for release-plan and workspace version alias

* Test prerelease bump with workspace:~

* Test apply-release-plan and workspace version aliases

* small tweaks

Co-authored-by: Mateusz Burzy艅ski <mateuszburzynski@gmail.com>
  • Loading branch information
javier-garcia-meteologica and Andarist committed Nov 8, 2021
1 parent 2b49c39 commit 74dda8c
Show file tree
Hide file tree
Showing 20 changed files with 253 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/stale-melons-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@changesets/apply-release-plan": patch
"@changesets/cli": patch
"@changesets/get-dependents-graph": patch
---

Add support for `workspace:^` and `workspace:~` dependency ranges.
3 changes: 3 additions & 0 deletions __fixtures__/workspace-alias-range-dep/.changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# We just want a file in here so git collects it

For this we have deliberately not included a config file, as we want to test the defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
11 changes: 11 additions & 0 deletions __fixtures__/workspace-alias-range-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"private": true,
"name": "workspace-alias-range-dep",
"description": "3 projects, one depending on the rest with `workspace:^` and `workspace:~` ranges",
"version": "1.0.0",
"bolt": {
"workspaces": [
"packages/*"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pkg-a",
"version": "1.0.0",
"dependencies": {
"pkg-b": "workspace:^",
"pkg-c": "workspace:~"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "pkg-b",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "pkg-c",
"version": "1.0.0"
}
3 changes: 3 additions & 0 deletions __fixtures__/workspace-version-alias-dep/.changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# We just want a file in here so git collects it

For this we have deliberately not included a config file, as we want to test the defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
11 changes: 11 additions & 0 deletions __fixtures__/workspace-version-alias-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"private": true,
"name": "simple-project",
"description": "Four projects with workspace version aliases",
"version": "1.0.0",
"bolt": {
"workspaces": [
"packages/*"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "pkg-a",
"version": "1.0.0",
"dependencies": {
"pkg-b": "workspace:*",
"pkg-c": "workspace:^",
"pkg-d": "workspace:~"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "pkg-b",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "pkg-c",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "pkg-d",
"version": "1.0.0"
}
65 changes: 65 additions & 0 deletions packages/apply-release-plan/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,71 @@ describe("apply release plan", () => {
}
});
});
it("should not update workspace version aliases", async () => {
const releasePlan = new FakeReleasePlan(
[
{
id: "some-id",
releases: [{ name: "pkg-b", type: "minor" }],
summary: "a very useful summary"
},
{
id: "some-id",
releases: [{ name: "pkg-c", type: "minor" }],
summary: "a very useful summary"
},
{
id: "some-id",
releases: [{ name: "pkg-d", type: "minor" }],
summary: "a very useful summary"
}
],
[
{
changesets: ["some-id"],
name: "pkg-b",
newVersion: "1.1.0",
oldVersion: "1.0.0",
type: "minor"
},
{
changesets: ["some-id"],
name: "pkg-c",
newVersion: "1.1.0",
oldVersion: "1.0.0",
type: "minor"
},
{
changesets: ["some-id"],
name: "pkg-d",
newVersion: "1.1.0",
oldVersion: "1.0.0",
type: "minor"
}
]
);
let { changedFiles } = await testSetup(
"workspace-version-alias-dep",
releasePlan.getReleasePlan(),
releasePlan.config
);
let pkgPath = changedFiles.find(a =>
a.endsWith(`pkg-a${path.sep}package.json`)
);

if (!pkgPath) throw new Error(`could not find an updated package json`);
let pkgJSON = await fs.readJSON(pkgPath);

expect(pkgJSON).toEqual({
name: "pkg-a",
version: "1.1.0",
dependencies: {
"pkg-b": "workspace:*",
"pkg-c": "workspace:^",
"pkg-d": "workspace:~"
}
});
});
it("should update workspace ranges only with bumpVersionsWithWorkspaceProtocolOnly", async () => {
const releasePlan = new FakeReleasePlan(
[
Expand Down
9 changes: 8 additions & 1 deletion packages/apply-release-plan/src/version-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ export default function versionPackage(
continue;
const usesWorkspaceRange = depCurrentVersion.startsWith("workspace:");
if (usesWorkspaceRange) {
depCurrentVersion = depCurrentVersion.substr(10);
const workspaceDepVersion = depCurrentVersion.replace(
/^workspace:/,
""
);
depCurrentVersion =
workspaceDepVersion === "^" || workspaceDepVersion === "~"
? "*"
: workspaceDepVersion;
} else if (bumpVersionsWithWorkspaceProtocolOnly === true) {
continue;
}
Expand Down
31 changes: 31 additions & 0 deletions packages/assemble-release-plan/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,37 @@ describe("assemble-release-plan", () => {
expect(releases[1].newVersion).toEqual("1.0.1");
expect(releases[1].changesets).toEqual([]);
});
it("should assemble the release plan with workspace:^ and workspace:~ dependents", () => {
setup.updateDependency("pkg-b", "pkg-a", "workspace:~");
setup.updateDependency("pkg-c", "pkg-a", "workspace:^");
setup.addChangeset({
id: "big-cats-delight",
releases: [{ name: "pkg-a", type: "major" }]
});

let { releases } = assembleReleasePlan(
setup.changesets,
setup.packages,
{
...defaultConfig,
bumpVersionsWithWorkspaceProtocolOnly: true
},
undefined
);

expect(releases.length).toEqual(3);

expect(releases[0].name).toEqual("pkg-a");
expect(releases[0].newVersion).toEqual("2.0.0");

expect(releases[1].name).toEqual("pkg-b");
expect(releases[1].newVersion).toEqual("1.0.1");
expect(releases[1].changesets).toEqual([]);

expect(releases[2].name).toEqual("pkg-c");
expect(releases[2].newVersion).toEqual("1.0.1");
expect(releases[2].changesets).toEqual([]);
});
it("should assemble release plan without dependent through dev dependency", () => {
setup.updateDevDependency("pkg-b", "pkg-a", "^1.0.0");
setup.addChangeset({
Expand Down
66 changes: 66 additions & 0 deletions packages/cli/src/commands/version/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,39 @@ describe("workspace range", () => {
}
]);
});

it("should bump dependant package when bumping a `workspace:^` dependency", async () => {
const cwd = f.copy("workspace-alias-range-dep");

await writeChangeset(
{
releases: [{ name: "pkg-b", type: "patch" }],
summary: "a very useful summary for the change"
},
cwd
);
await versionCommand(cwd, defaultOptions, modifiedDefaultConfig);

let packages = await getPackages(cwd);
expect(packages.packages.map(x => x.packageJson)).toEqual([
{
name: "pkg-a",
version: "1.0.1",
dependencies: {
"pkg-b": "workspace:^",
"pkg-c": "workspace:~"
}
},
{
name: "pkg-b",
version: "1.0.1"
},
{
name: "pkg-c",
version: "1.0.0"
}
]);
});
});

describe("same package in different dependency types", () => {
Expand Down Expand Up @@ -1227,4 +1260,37 @@ describe("pre", () => {
}
]);
});
it("should bump dependant of prerelease package when bumping a `workspace:~` dependency", async () => {
const cwd = f.copy("workspace-alias-range-dep");
await pre(cwd, { command: "enter", tag: "alpha" });

await writeChangeset(
{
releases: [{ name: "pkg-c", type: "patch" }],
summary: "a very useful summary for the change"
},
cwd
);
await versionCommand(cwd, defaultOptions, modifiedDefaultConfig);

let packages = await getPackages(cwd);
expect(packages.packages.map(x => x.packageJson)).toEqual([
{
name: "pkg-a",
version: "1.0.1-alpha.0",
dependencies: {
"pkg-b": "workspace:^",
"pkg-c": "workspace:~"
}
},
{
name: "pkg-b",
version: "1.0.0"
},
{
name: "pkg-c",
version: "1.0.1-alpha.0"
}
]);
});
});
12 changes: 9 additions & 3 deletions packages/get-dependents-graph/src/get-dependency-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const DEPENDENCY_TYPES = [
] as const;

const getAllDependencies = (config: PackageJSON) => {
const allDependencies = new Map();
const allDependencies = new Map<string, string>();

for (const type of DEPENDENCY_TYPES) {
const deps = config[type];
Expand Down Expand Up @@ -70,9 +70,15 @@ export default function getDependencyGraph(
if (!match) continue;

const expected = match.packageJson.version;
const usesWorkspaceRange = depVersion.startsWith("workspace:");

if (depVersion.startsWith("workspace:")) {
depVersion = depVersion.substr(10);
if (usesWorkspaceRange) {
depVersion = depVersion.replace(/^workspace:/, "");

if (depVersion === "*" || depVersion === "^" || depVersion === "~") {
dependencies.push(depName);
continue;
}
} else if (opts?.bumpVersionsWithWorkspaceProtocolOnly === true) {
continue;
}
Expand Down
2 changes: 0 additions & 2 deletions packages/git/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ async function getAllTags(cwd: string): Promise<Set<string>> {
.trim()
.split("\n");

console.log("gonna return set", new Set(tags));

return new Set(tags);
}

Expand Down

0 comments on commit 74dda8c

Please sign in to comment.