Skip to content

Commit 5b32196

Browse files
authored
Have some fun with Yarn constraints (#438)
I recently learned about Yarn constraints (https://yarnpkg.com/features/constraints), so I've been yearning for a chance to try them.
1 parent 3365eda commit 5b32196

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"devDependencies": {
3131
"@code-chronicles/eslint-config": "workspace:*",
32+
"@yarnpkg/types": "4.0.0",
3233
"eslint": "9.11.0",
3334
"husky": "9.1.6",
3435
"lint-staged": "15.2.10",

yarn.config.cjs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
1-
module.exports = {
1+
const { defineConfig } = require("@yarnpkg/types");
2+
3+
module.exports = defineConfig({
24
async constraints({ Yarn }) {
5+
const rootWorkspace = Yarn.workspace({ cwd: "." });
6+
if (rootWorkspace == null) {
7+
Yarn.workspace().error("Didn't find a root workspace?!");
8+
return;
9+
}
10+
11+
// Use ESM everywhere.
312
for (const workspace of Yarn.workspaces()) {
413
workspace.set("type", "module");
514
}
15+
16+
// Set a consistent repository for all workspaces.
17+
if (rootWorkspace.manifest.repository == null) {
18+
rootWorkspace.error("Missing repository specification!");
19+
}
20+
for (const workspace of Yarn.workspaces()) {
21+
if (workspace.cwd !== ".") {
22+
workspace.set("repository.directory", workspace.cwd);
23+
workspace.set(
24+
"repository.type",
25+
rootWorkspace.manifest.repository.type,
26+
);
27+
workspace.set("repository.url", rootWorkspace.manifest.repository.url);
28+
} else {
29+
workspace.unset("repository.directory");
30+
}
31+
}
32+
33+
// Expect each workspace to either have a license or be private.
34+
for (const workspace of Yarn.workspaces()) {
35+
if (workspace.manifest.license == null && !workspace.manifest.private) {
36+
// Don't default since we want a human to make the call.
37+
workspace.error("Missing license for non-private workspace!");
38+
}
39+
}
40+
41+
// Expect each workspace to specify a version, except for the root.
42+
for (const workspace of Yarn.workspaces()) {
43+
if (workspace.cwd !== ".") {
44+
workspace.set("version", workspace.manifest.version ?? "0.0.1");
45+
} else {
46+
workspace.unset("version");
47+
}
48+
}
49+
50+
// Explicitly specify whether the workspace is private or not.
51+
for (const workspace of Yarn.workspaces()) {
52+
workspace.set("private", !!workspace.manifest.private);
53+
}
654
},
7-
};
55+
});

yarn.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)