-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathyarn.config.cjs
More file actions
285 lines (245 loc) · 8.46 KB
/
yarn.config.cjs
File metadata and controls
285 lines (245 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Context} Context
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Dependency} Dependency
*/
/** @type {import('@yarnpkg/types')} */
const { defineConfig } = require(`@yarnpkg/types`);
const enforcedDevDependencies = {
prettier: { commands: ["prettier"], ident: "prettier" },
waitOn: { commands: ["wait-on"], ident: "wait-on" },
rimraf: { commands: ["rimraf"], ident: "rimraf" },
eslint: { commands: ["eslint"], ident: "eslint" },
typescript: { commands: ["tsc", "ts-node"], ident: "typescript" },
crossEnv: { commands: ["cross-env"], ident: "cross-env" },
};
const ignoredDependencies = [
"@sentry/webpack-plugin",
// Petrinaut uses Vite 8 and Storybook 10
"vite",
"@vitejs/plugin-react",
"storybook",
"@storybook/react-vite",
// Petrinaut uses multiple packages which are many versions behind in other workspaces
// To be un-ignored once H-5639 completed
"vitest",
"@dnd-kit/sortable",
"@babel/core",
];
const ignoredWorkspaces = [];
const allowedGitDependencies = [];
/**
* Whether to ignore a dependency for consistency checks (version alignment).
* Skips peerDependencies entirely (they may legitimately differ).
*
* @param {Dependency} dependency
*/
const shouldIgnoreDependencyForConsistency = (dependency) =>
ignoredDependencies.includes(dependency.ident) ||
ignoredWorkspaces.includes(dependency.workspace.ident) ||
dependency.type === "peerDependencies";
/**
* Whether to ignore a dependency for protocol enforcement.
* Does NOT skip peerDependencies — workspace peers must also use workspace:*.
*
* @param {Dependency} dependency
*/
const shouldIgnoreDependencyForProtocol = (dependency) =>
ignoredDependencies.includes(dependency.ident) ||
ignoredWorkspaces.includes(dependency.workspace.ident);
/**
* Enforces consistent dependency versions across all workspaces in the project.
*
* This rule ensures that all workspaces use the same version of a given dependency.
*
* @param {Context} context - The Yarn constraint context.
*/
function enforceConsistentDependenciesAcrossTheProject({ Yarn }) {
const workspaceIdents = new Set(
Yarn.workspaces().map((workspace) => workspace.ident),
);
for (const dependency of Yarn.dependencies()) {
if (shouldIgnoreDependencyForConsistency(dependency)) {
continue;
}
// Skip workspace dependencies; enforceProtocols handles them via workspace:*
if (workspaceIdents.has(dependency.ident)) {
continue;
}
for (const otherDependency of Yarn.dependencies({
ident: dependency.ident,
})) {
if (shouldIgnoreDependencyForConsistency(otherDependency)) {
continue;
}
dependency.update(otherDependency.range);
}
}
}
/**
* Enforces no dual-type dependencies across workspaces.
*
* This function ensures that a dependency is not listed in both "dependencies"
* and "devDependencies" for any workspace. If a dependency is found in both,
* it removes it from "dependencies", keeping it only in "devDependencies".
*
* @param {Context} context - The Yarn constraint context.
*/
function enforceNoDualTypeDependencies({ Yarn }) {
for (const devDependency of Yarn.dependencies({ type: "devDependencies" })) {
devDependency.workspace.unset(`dependency.${devDependency.ident}`);
}
}
/**
* Enforce that the package protocols are correct.
*
* @param {Context} context
*/
function enforceProtocols({ Yarn }) {
const workspaces = Yarn.workspaces();
for (const dependency of Yarn.dependencies()) {
if (shouldIgnoreDependencyForProtocol(dependency)) {
continue;
}
const workspaceDependency = workspaces.find(
(workspace) => workspace.ident === dependency.ident,
);
if (workspaceDependency) {
const isPublishable = dependency.workspace.manifest.private !== true;
const expectedRange =
isPublishable && dependency.type !== "devDependencies"
? "workspace:^"
: "workspace:*";
if (dependency.range !== expectedRange) {
dependency.update(expectedRange);
}
}
if (dependency.range.startsWith("file:")) {
// the file: protocol makes problems when used in conjunction with pnpm mode, portal is the equivalent protocol
dependency.update(dependency.range.replace("file:", "portal:"));
}
if (dependency.range.startsWith("link:")) {
dependency.error(
`The link protocol allows for non-packages to be linked and is not allowed, dependency: ${dependency.ident}`,
);
}
if (dependency.range.startsWith("exec:")) {
dependency.error(
`The exec protocol allows for arbitrary code execution and is not allowed, dependency: ${dependency.ident}`,
);
}
let shouldCheckIfValidGitDependency = false;
if (
dependency.range.startsWith("https://") ||
dependency.range.startsWith("http://")
) {
// always prefix with the git protocol
dependency.update(`git:${dependency.range}`);
shouldCheckIfValidGitDependency = true;
}
if (dependency.range.startsWith("ssh://")) {
// always prefix with the git protocol
dependency.update(`git:${dependency.range.replace(/^ssh:\/\//, "git:")}`);
shouldCheckIfValidGitDependency = true;
}
if (
(shouldCheckIfValidGitDependency ||
dependency.range.startsWith("git:")) &&
!allowedGitDependencies.includes(dependency.ident)
) {
dependency.error(
`arbitrary git dependencies are not allowed, dependency: ${dependency.ident}`,
);
}
// patches are only allowed if they are for an `npm:` dpeendenct
if (dependency.range.startsWith("patch:")) {
const dependencySpecification = dependency.range.match(/^patch:([^#]+)/);
if (!dependencySpecification) {
dependency.error(
`invalid patch protocol, dependency: ${dependency.ident}`,
);
continue;
}
// locator is on the right side
// splitRight at `@`
const segments = dependencySpecification[1].split("@");
const last = segments.pop();
// urldecode the last segment
const version = decodeURIComponent(last);
if (!version.startsWith("npm:")) {
dependency.error(
`patch protocol is only allowed for npm dependencies, dependency: ${dependency.ident}, patches: ${version}`,
);
}
}
}
}
/**
* Enforces proper declaration of dev dependencies.
*
* This rule checks if certain tools (like Prettier) are used in any workspace
* and ensures they're declared as dev dependencies in those workspaces.
*
* @param {Context} context - The Yarn constraint context.
*/
function enforceDevDependenciesAreProperlyDeclared({ Yarn }) {
const dependencies = Object.fromEntries(
Object.entries(enforcedDevDependencies).map(([key, { ident }]) => [
key,
Yarn.dependency({ ident }),
]),
);
for (const workspace of Yarn.workspaces()) {
/** @type {Record<string, string> | undefined} */
const scripts = workspace.manifest.scripts;
if (!scripts) {
continue;
}
const dependsOn = {
prettier: false,
waitOn: false,
rimraf: false,
eslint: false,
typescript: false,
crossEnv: false,
};
for (const script of Object.values(scripts)) {
for (const [key, { commands }] of Object.entries(
enforcedDevDependencies,
)) {
if (workspace.ident === "@local/eslint" && key === "eslint") {
continue;
}
const scriptSplit = script.split(" ");
if (commands.some((command) => scriptSplit.includes(command))) {
dependsOn[key] = true;
}
}
}
for (const [key, value] of Object.entries(dependsOn)) {
if (!value) {
if (!dependencies[key]) {
// dependency does not exist, so doesn't need to be enforced, as it doesn't exist
continue;
}
workspace.unset(`devDependencies.${dependencies[key].ident}`);
continue;
}
const dependency = dependencies[key];
if (dependency === null) {
workspace.error(
`missing devDependency ${key}, unable to automatically determine the version`,
);
continue;
}
workspace.set(`devDependencies.${dependency.ident}`, dependency.range);
}
}
}
module.exports = defineConfig({
async constraints(context) {
enforceConsistentDependenciesAcrossTheProject(context);
enforceNoDualTypeDependencies(context);
enforceProtocols(context);
enforceDevDependenciesAreProperlyDeclared(context);
},
});