-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathyarn.config.cjs
More file actions
399 lines (348 loc) · 11 KB
/
yarn.config.cjs
File metadata and controls
399 lines (348 loc) · 11 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* eslint-env node */
// @ts-check
const fs = require('node:fs')
const path = require('node:path')
const semver = require('semver')
/**
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Context} Context
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Workspace} Workspace
*/
/** @type {import('@yarnpkg/types')} */
const { defineConfig } = require(`@yarnpkg/types`)
/**
* Vite depends on esbuild. For consistency, we should always use the same
* version of esbuild as Vite.
*
* @param {Context} ctx
*/
async function enforceEsbuildMatchesVite({ Yarn }) {
// Find the first workspace that declares vite
let viteWorkspace = null
let viteVersion = null
for (const workspace of Yarn.workspaces()) {
const deps = workspace.manifest.dependencies || {}
const devDeps = workspace.manifest.devDependencies || {}
if (deps.vite || devDeps.vite) {
viteWorkspace = workspace
viteVersion = deps.vite || devDeps.vite
break
}
}
if (!viteWorkspace || !viteVersion) {
// No vite declared in any workspace; nothing to enforce
return
}
// Use Node's module resolution to find the actual vite package.json being used
// This works regardless of where Yarn placed vite in node_modules
let vitePackageJson = null
let vitePackageJsonPath = null
try {
vitePackageJsonPath = require.resolve('vite/package.json', {
paths: [path.join(process.cwd(), viteWorkspace.cwd)],
})
vitePackageJson = JSON.parse(fs.readFileSync(vitePackageJsonPath, 'utf-8'))
} catch (e) {
// Failed to resolve or read vite's package.json
}
if (!vitePackageJson) {
// Can't find vite's package.json, so we can't enforce this constraint
console.warn(
'Warning: Could not find vite package.json to enforce esbuild version constraint',
)
return
}
// Verify that the resolved vite version matches what was declared
let resolvedViteVersion = vitePackageJson.version
if (!semver.satisfies(resolvedViteVersion, viteVersion)) {
// Version mismatch - try fallback locations
const fallbackPaths = []
// Fallback 1: node_modules/vite/package.json (if not already checked)
const rootVitePath = path.join(
process.cwd(),
'node_modules',
'vite',
'package.json',
)
if (vitePackageJsonPath !== rootVitePath) {
fallbackPaths.push(rootVitePath)
}
// Fallback 2: node_modules/<workspace-name>/node_modules/vite/package.json
const workspaceName = viteWorkspace.manifest.name
if (workspaceName) {
const workspaceVitePath = path.join(
process.cwd(),
'node_modules',
workspaceName,
'node_modules',
'vite',
'package.json',
)
if (vitePackageJsonPath !== workspaceVitePath) {
fallbackPaths.push(workspaceVitePath)
}
}
// Try each fallback path
let foundMatch = false
for (const fallbackPath of fallbackPaths) {
if (fs.existsSync(fallbackPath)) {
try {
const fallbackPackageJson = JSON.parse(
fs.readFileSync(fallbackPath, 'utf-8'),
)
const fallbackVersion = fallbackPackageJson.version
if (semver.satisfies(fallbackVersion, viteVersion)) {
vitePackageJson = fallbackPackageJson
vitePackageJsonPath = fallbackPath
resolvedViteVersion = fallbackVersion
foundMatch = true
break
}
} catch (e) {
// Failed to read this fallback, continue to next
}
}
}
if (!foundMatch) {
console.warn(
`Warning: Resolved vite version (${resolvedViteVersion}) does not ` +
`satisfy declared version (${viteVersion})`,
)
}
}
// Get the esbuild version that vite depends on
const viteEsbuildVersion =
vitePackageJson.dependencies?.esbuild ||
vitePackageJson.devDependencies?.esbuild ||
vitePackageJson.optionalDependencies?.esbuild
if (!viteEsbuildVersion) {
// Vite doesn't declare esbuild dependency, nothing to enforce
return
}
// Check all workspaces that declare esbuild
const violations = []
for (const workspace of Yarn.workspaces()) {
const deps = workspace.manifest.dependencies || {}
const devDeps = workspace.manifest.devDependencies || {}
const peerDeps = workspace.manifest.peerDependencies || {}
const declaredEsbuild = deps.esbuild || devDeps.esbuild || peerDeps.esbuild
if (!declaredEsbuild) {
continue
}
// Check if the declared esbuild version satisfies vite's esbuild version
// range
// I wanted to use === here, but vite doesn't declare an exact version, and
// Cedar always does, so we use semver.satisfies instead.
const satisfies = semver.satisfies(declaredEsbuild, viteEsbuildVersion)
if (!satisfies) {
violations.push({
workspace: workspace.cwd,
declared: declaredEsbuild,
viteExpects: viteEsbuildVersion,
})
}
}
if (violations.length > 0) {
const lines = [
`esbuild / vite constraint failed: some workspaces declare an esbuild ` +
`version that doesn't satisfy what vite requires.`,
'',
`Vite (${vitePackageJson.version}) uses esbuild: ${viteEsbuildVersion}`,
'',
...violations.flatMap((v) => [
`- workspace: ${v.workspace}`,
` declared esbuild: ${v.declared}`,
` vite requires: ${v.viteExpects}`,
'',
]),
]
throw new Error(lines.join('\n'))
}
}
/**
* This rule will enforce that a workspace MUST depend on the same version of a
* dependency as the one used by the other workspaces.
*
* @param {Context} context
*/
function enforceConsistentDependenciesAcrossTheProject({ Yarn }) {
for (const dependency of Yarn.dependencies()) {
if (dependency.type === `peerDependencies`) {
continue
}
for (const otherDependency of Yarn.dependencies({
ident: dependency.ident,
})) {
if (otherDependency.type === `peerDependencies`) {
continue
}
if (
(dependency.type === `devDependencies` ||
otherDependency.type === `devDependencies`) &&
Yarn.workspace({ ident: otherDependency.ident })
) {
continue
}
dependency.update(otherDependency.range)
}
}
}
/**
* This rule will enforce that workspace dependencies use `workspace:*` as the
* dependency range instead of a specific version (like 0.7.1)
*
* @param {Context} context
*/
function enforceWorkspaceDependenciesWhenPossible({ Yarn }) {
for (const dependency of Yarn.dependencies()) {
if (!Yarn.workspace({ ident: dependency.ident })) {
continue
}
dependency.update(`workspace:*`)
}
}
/**
* This rule will enforce that a dependency doesn't appear in both
* `dependencies` and `devDependencies`.
*
* @param {Context} context
*/
function enforceNotProdAndDevDependencies({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
const dependencies = Yarn.dependencies({ workspace, type: 'dependencies' })
const devDependencies = Yarn.dependencies({
workspace,
type: 'devDependencies',
})
for (const dependency of dependencies) {
if (
devDependencies.find(
(devDependency) => devDependency.ident === dependency.ident,
)
) {
dependency.error(
`The dependency '${dependency.ident}' should not appear in both dependencies and devDependencies`,
)
}
}
}
}
/**
* This rule will enforce that any package built with babel (identified by the
* presence of a 'build:js' script in its `package.json`) must depend on the
* '@babel/runtime-corejs3' and 'core-js' packages.
*
* @param {Context} context
*/
function enforceBabelDependencies({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
const packageJson = workspace.manifest
if (!packageJson.scripts?.[`build:js`]) {
continue
}
const dependencies = Yarn.dependencies({
workspace,
type: 'dependencies',
})
const requiredDependencies = [`@babel/runtime-corejs3`, `core-js`]
for (const dependency of requiredDependencies) {
if (!dependencies.find((dep) => dep.ident === dependency)) {
workspace.error(
`The package '${workspace.cwd}' must depend on '${dependency}' to build with babel`,
)
}
}
}
}
/**
* This rule will enforce that the specified fields are present in the
* `package.json` of all workspaces.
*
* @param {Context} context
* @param {string[]} fields
*/
function enforceFieldsOnAllWorkspaces({ Yarn }, fields) {
for (const workspace of Yarn.workspaces()) {
// Skip the root workspace
if (workspace.cwd === '.') {
continue
}
for (const field of fields) {
if (!workspace.manifest[field]) {
workspace.error(
`The field '${field}' is required in the package.json of '${workspace.cwd}'`,
)
}
}
}
}
/**
* This rule will enforce that the specified fields are present in the
* `package.json` of all workspaces and that they have the expected value.
*
* @param {Context} context
* @param {Record<string, ((workspace: Workspace) => any) | string>} fields
*/
function enforceFieldsWithValuesOnAllWorkspaces({ Yarn }, fields) {
for (const workspace of Yarn.workspaces()) {
// Skip the root workspace
if (workspace.cwd === '.') {
continue
}
for (const [field, value] of Object.entries(fields)) {
workspace.set(
field,
typeof value === `function` ? value(workspace) : value,
)
}
}
}
module.exports = defineConfig({
constraints: async (ctx) => {
const branch = await gitBranch()
enforceConsistentDependenciesAcrossTheProject(ctx)
if (branch !== 'next' && !branch?.startsWith('release/')) {
enforceWorkspaceDependenciesWhenPossible(ctx)
}
enforceNotProdAndDevDependencies(ctx)
enforceBabelDependencies(ctx)
enforceFieldsOnAllWorkspaces(ctx, [
'name',
'version',
// 'description', // TODO(jgmw): Add description to all packages and uncomment this line
])
enforceFieldsWithValuesOnAllWorkspaces(ctx, {
license: 'MIT',
['repository.type']: 'git',
['repository.url']: 'git+https://github.com/cedarjs/cedar.git',
['repository.directory']: (workspace) => workspace.cwd,
})
await enforceEsbuildMatchesVite(ctx)
},
})
function gitBranch() {
function parseBranch(buf) {
const match = /ref: refs\/heads\/([^\n]+)/.exec(buf.toString())
return match ? match[1] : null
}
function findGitHead(startDir = process.cwd()) {
let currentDir = path.resolve(startDir)
let foundGitHeadPath
while (!foundGitHeadPath) {
const gitHeadPath = path.join(currentDir, '.git', 'HEAD')
if (fs.existsSync(gitHeadPath)) {
foundGitHeadPath = gitHeadPath
} else {
const parentDir = path.dirname(currentDir)
if (parentDir === currentDir) {
throw new Error('.git/HEAD does not exist')
}
currentDir = parentDir
}
}
return foundGitHeadPath
}
const promise = fs.promises
.readFile(findGitHead())
.then((buf) => parseBranch(buf))
return promise
}