-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
yarn.config.cjs
829 lines (742 loc) · 27.5 KB
/
yarn.config.cjs
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
// @ts-check
// This file is used to define, among other configuration, rules that Yarn will
// execute when you run `yarn constraints`. These rules primarily check the
// manifests of each package in the monorepo to ensure they follow a standard
// format, but also check the presence of certain files as well.
/** @type {import('@yarnpkg/types')} */
const { defineConfig } = require('@yarnpkg/types');
const { readFile } = require('fs/promises');
const { get } = require('lodash');
const { basename, resolve } = require('path');
const semver = require('semver');
const { inspect } = require('util');
/**
* These packages and ranges are allowed to mismatch expected consistency checks
* Only intended as temporary measures to faciliate upgrades and releases.
* This should trend towards empty.
*/
const ALLOWED_INCONSISTENT_DEPENDENCIES = {
// '@metamask/json-rpc-engine': ['^9.0.3'],
};
/**
* Aliases for the Yarn type definitions, to make the code more readable.
*
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Yarn} Yarn
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Workspace} Workspace
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Dependency} Dependency
* @typedef {import('@yarnpkg/types').Yarn.Constraints.DependencyType} DependencyType
*/
module.exports = defineConfig({
async constraints({ Yarn }) {
const rootWorkspace = Yarn.workspace({ cwd: '.' });
if (rootWorkspace === null) {
throw new Error('Could not find root workspace');
}
const repositoryUri = rootWorkspace.manifest.repository.url.replace(
/\.git$/u,
'',
);
for (const workspace of Yarn.workspaces()) {
const workspaceBasename = getWorkspaceBasename(workspace);
const isChildWorkspace = workspace.cwd !== '.';
const isPrivate =
'private' in workspace.manifest && workspace.manifest.private === true;
const dependenciesByIdentAndType = getDependenciesByIdentAndType(
Yarn.dependencies({ workspace }),
);
// All packages must have a name.
expectWorkspaceField(workspace, 'name');
if (isChildWorkspace) {
// All non-root packages must have a name that matches its directory
// (e.g., a package in a workspace directory called `foo` must be called
// `@metamask/foo`).
expectWorkspaceField(
workspace,
'name',
`@metamask/${workspaceBasename}`,
);
// All non-root packages must have a version.
expectWorkspaceField(workspace, 'version');
// All non-root packages must have a description that ends in a period.
expectWorkspaceDescription(workspace);
// All non-root packages must have the same set of NPM keywords.
expectWorkspaceField(workspace, 'keywords', ['MetaMask', 'Ethereum']);
// All non-root packages must have a homepage URL that includes its name.
expectWorkspaceField(
workspace,
'homepage',
`${repositoryUri}/tree/main/packages/${workspaceBasename}#readme`,
);
// All non-root packages must have a URL for reporting bugs that points
// to the Issues page for the repository.
expectWorkspaceField(workspace, 'bugs.url', `${repositoryUri}/issues`);
// All non-root packages must specify a Git repository within the
// MetaMask GitHub organization.
expectWorkspaceField(workspace, 'repository.type', 'git');
expectWorkspaceField(
workspace,
'repository.url',
`${repositoryUri}.git`,
);
// All non-root packages must have a license, defaulting to MIT.
await expectWorkspaceLicense(workspace);
// All non-root packages must not have side effects. (An exception is
// made for `@metamask/base-controller`).
if (workspace.ident !== '@metamask/base-controller') {
expectWorkspaceField(workspace, 'sideEffects', false);
}
// All non-root packages must set up ESM- and CommonJS-compatible
// exports correctly.
expectCorrectWorkspaceExports(workspace);
// All non-root packages must have the same "build" script.
expectWorkspaceField(
workspace,
'scripts.build',
'ts-bridge --project tsconfig.build.json --verbose --clean --no-references',
);
// All non-root packages must have the same "build:docs" script.
expectWorkspaceField(workspace, 'scripts.build:docs', 'typedoc');
if (isPrivate) {
// All private, non-root packages must not have a "publish:preview"
// script.
workspace.unset('scripts.publish:preview');
} else {
// All non-private, non-root packages must have the same
// "publish:preview" script.
expectWorkspaceField(
workspace,
'scripts.publish:preview',
'yarn npm publish --tag preview',
);
}
// No non-root packages may have a "prepack" script.
workspace.unset('scripts.prepack');
// All non-root package must have valid "changelog:update" and
// "changelog:validate" scripts.
expectCorrectWorkspaceChangelogScripts(workspace);
// All non-root packages must have a valid "since-latest-release" script.
expectWorkspaceField(
workspace,
'scripts.since-latest-release',
'../../scripts/since-latest-release.sh',
);
// All non-root packages must have the same "test" script.
expectWorkspaceField(
workspace,
'scripts.test',
'NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter',
);
// All non-root packages must have the same "test:clean" script.
expectWorkspaceField(
workspace,
'scripts.test:clean',
'NODE_OPTIONS=--experimental-vm-modules jest --clearCache',
);
// All non-root packages must have the same "test:verbose" script.
expectWorkspaceField(
workspace,
'scripts.test:verbose',
'NODE_OPTIONS=--experimental-vm-modules jest --verbose',
);
// All non-root packages must have the same "test:watch" script.
expectWorkspaceField(
workspace,
'scripts.test:watch',
'NODE_OPTIONS=--experimental-vm-modules jest --watch',
);
}
if (isChildWorkspace) {
// The list of files included in all non-root packages must only include
// files generated during the build process.
expectWorkspaceArrayField(workspace, 'files', 'dist/');
} else {
// The root package must specify an empty set of published files. (This
// is required in order to be able to import anything in
// development-only scripts, as otherwise the
// `node/no-unpublished-require` ESLint rule will disallow it.)
expectWorkspaceField(workspace, 'files', []);
}
// If one workspace package lists another workspace package within
// `dependencies` or `devDependencies`, the version used within the
// dependency range must match the current version of the dependency.
expectUpToDateWorkspaceDependenciesAndDevDependencies(Yarn, workspace);
// If one workspace package lists another workspace package within
// `peerDependencies`, the dependency range must satisfy the current
// version of that package.
expectUpToDateWorkspacePeerDependencies(Yarn, workspace);
// No dependency may be listed under both `dependencies` and
// `devDependencies`.
expectDependenciesNotInBothProdAndDev(
workspace,
dependenciesByIdentAndType,
);
// If one workspace package (A) lists another workspace package (B) in its
// `dependencies`, and B is a controller package, then we need to ensure
// that B is also listed in A's `peerDependencies` and that the version
// range satisfies the current version of B.
expectControllerDependenciesListedAsPeerDependencies(
Yarn,
workspace,
dependenciesByIdentAndType,
);
// The root workspace (and only the root workspace) must specify the Yarn
// version required for development.
if (isChildWorkspace) {
workspace.unset('packageManager');
} else {
expectWorkspaceField(workspace, 'packageManager', '[email protected]');
}
// All packages must specify a minimum Node.js version of 18.18.
expectWorkspaceField(workspace, 'engines.node', '^18.18 || >=20');
// All non-root public packages should be published to the NPM registry;
// all non-root private packages should not.
if (isPrivate) {
workspace.unset('publishConfig');
} else {
expectWorkspaceField(workspace, 'publishConfig.access', 'public');
expectWorkspaceField(
workspace,
'publishConfig.registry',
'https://registry.npmjs.org/',
);
}
if (isChildWorkspace) {
// All non-root packages must have a valid README.md file.
await expectReadme(workspace, workspaceBasename);
}
}
// All version ranges in `dependencies` and `devDependencies` for the same
// dependency across the monorepo must be the same.
expectConsistentDependenciesAndDevDependencies(Yarn);
},
});
/**
* Construct a nested map of dependencies. The inner layer categorizes
* instances of the same dependency by its location in the manifest; the outer
* layer categorizes the inner layer by the name of the dependency.
*
* @param {Dependency[]} dependencies - The list of dependencies to transform.
* @returns {Map<string, Map<DependencyType, Dependency>>} The resulting map.
*/
function getDependenciesByIdentAndType(dependencies) {
const dependenciesByIdentAndType = new Map();
for (const dependency of dependencies) {
const dependenciesForIdent = dependenciesByIdentAndType.get(
dependency.ident,
);
if (dependenciesForIdent === undefined) {
dependenciesByIdentAndType.set(
dependency.ident,
new Map([[dependency.type, dependency]]),
);
} else {
dependenciesForIdent.set(dependency.type, dependency);
}
}
return dependenciesByIdentAndType;
}
/**
* Construct a nested map of non-peer dependencies (`dependencies` and
* `devDependencies`). The inner layer categorizes instances of the same
* dependency by the version range specified; the outer layer categorizes the
* inner layer by the name of the dependency itself.
*
* @param {Dependency[]} dependencies - The list of dependencies to transform.
* @returns {Map<string, Map<string, Dependency[]>>} The resulting map.
*/
function getNonPeerDependenciesByIdent(dependencies) {
const nonPeerDependenciesByIdent = new Map();
for (const dependency of dependencies) {
if (dependency.type === 'peerDependencies') {
continue;
}
const dependencyRangesForIdent = nonPeerDependenciesByIdent.get(
dependency.ident,
);
if (dependencyRangesForIdent === undefined) {
nonPeerDependenciesByIdent.set(
dependency.ident,
new Map([[dependency.range, [dependency]]]),
);
} else {
const dependenciesForDependencyRange = dependencyRangesForIdent.get(
dependency.range,
);
if (dependenciesForDependencyRange === undefined) {
dependencyRangesForIdent.set(dependency.range, [dependency]);
} else {
dependenciesForDependencyRange.push(dependency);
}
}
}
return nonPeerDependenciesByIdent;
}
/**
* Get the basename of the workspace's directory. The workspace directory is
* expected to be in the form `<directory>/<package-name>`, and this function
* will extract `<package-name>`.
*
* @param {Workspace} workspace - The workspace.
* @returns {string} The name of the workspace.
*/
function getWorkspaceBasename(workspace) {
return basename(workspace.cwd);
}
/**
* Get the absolute path to a file within the workspace.
*
* @param {Workspace} workspace - The workspace.
* @param {string} path - The path to the file, relative to the workspace root.
* @returns {string} The absolute path to the file.
*/
function getWorkspacePath(workspace, path) {
return resolve(__dirname, workspace.cwd, path);
}
/**
* Get the contents of a file within the workspace. The file is expected to be
* encoded as UTF-8.
*
* @param {Workspace} workspace - The workspace.
* @param {string} path - The path to the file, relative to the workspace root.
* @returns {Promise<string>} The contents of the file.
*/
async function getWorkspaceFile(workspace, path) {
return await readFile(getWorkspacePath(workspace, path), 'utf8');
}
/**
* Attempts to access the given file to know whether the file exists.
*
* @param {Workspace} workspace - The workspace.
* @param {string} path - The path to the file, relative to the workspace root.
* @returns {Promise<boolean>} True if the file exists, false otherwise.
*/
async function workspaceFileExists(workspace, path) {
try {
await getWorkspaceFile(workspace, path);
} catch (error) {
if ('code' in error && error.code === 'ENOENT') {
return false;
}
throw error;
}
return true;
}
/**
* Expect that the workspace has the given field, and that it is a non-null
* value. If the field is not present, or is null, this will log an error, and
* cause the constraint to fail.
*
* If a value is provided, this will also verify that the field is equal to the
* given value.
*
* @param {Workspace} workspace - The workspace to check.
* @param {string} fieldName - The field to check.
* @param {unknown} [expectedValue] - The value to check.
*/
function expectWorkspaceField(workspace, fieldName, expectedValue = undefined) {
const fieldValue = get(workspace.manifest, fieldName);
if (expectedValue !== undefined && expectedValue !== null) {
workspace.set(fieldName, expectedValue);
} else if (expectedValue === null) {
workspace.unset(fieldName);
} else if (
expectedValue === undefined &&
(fieldValue === undefined || fieldValue === null)
) {
workspace.error(`Missing required field "${fieldName}".`);
}
}
/**
* Expect that the workspace has the given field, and that it is an array-like
* property containing the specified value. If the field is not present, is not
* an array, or does not contain the value, this will log an error, and cause
* the constraint to fail.
*
* @param {Workspace} workspace - The workspace to check.
* @param {string} fieldName - The field to check.
* @param {unknown} expectedValue - The value that should be contained in the array.
*/
function expectWorkspaceArrayField(
workspace,
fieldName,
expectedValue = undefined,
) {
let fieldValue = get(workspace.manifest, fieldName);
if (expectedValue) {
if (!Array.isArray(fieldValue)) {
fieldValue = [];
}
if (!fieldValue.includes(expectedValue)) {
fieldValue.push(expectedValue);
workspace.set(fieldName, fieldValue);
}
} else if (fieldValue === undefined || fieldValue === null) {
workspace.error(`Missing required field "${fieldName}".`);
}
}
/**
* Expect that the workspace has a description, and that it is a non-empty
* string. If the description is not present, or is null, this will log an
* error, and cause the constraint to fail.
*
* This will also verify that the description does not end with a period.
*
* @param {Workspace} workspace - The workspace to check.
*/
function expectWorkspaceDescription(workspace) {
expectWorkspaceField(workspace, 'description');
const { description } = workspace.manifest;
if (typeof description !== 'string') {
workspace.error(
`Expected description to be a string, but got ${typeof description}.`,
);
return;
}
if (description === '') {
workspace.error(`Expected description not to be an empty string.`);
return;
}
if (description.endsWith('.')) {
workspace.set('description', description.slice(0, -1));
}
}
/**
* Expect that the workspace has a license file, and that the `license` field is
* set. By default, this should be MIT, although some packages have pre-existing
* license that we cannot change.
*
* @param {Workspace} workspace - The workspace to check.
*/
async function expectWorkspaceLicense(workspace) {
if (
!(await workspaceFileExists(workspace, 'LICENSE')) &&
!(await workspaceFileExists(workspace, 'LICENCE'))
) {
workspace.error('Could not find LICENSE file');
}
if (
workspace.manifest.license === null ||
workspace.manifest.license === undefined
) {
expectWorkspaceField(workspace, 'license', 'MIT');
} else if (
[
'@metamask/json-rpc-engine',
'@metamask/json-rpc-middleware-stream',
'@metamask/permission-log-controller',
'@metamask/eth-json-rpc-provider',
].includes(workspace.manifest.name)
) {
expectWorkspaceField(workspace, 'license');
}
}
/**
* Expect that the workspace has exports set up correctly.
*
* @param {Workspace} workspace - The workspace to check.
*/
function expectCorrectWorkspaceExports(workspace) {
// All non-root packages must provide the location of the ESM-compatible
// JavaScript entrypoint and its matching type declaration file.
expectWorkspaceField(
workspace,
'exports["."].import.types',
'./dist/index.d.mts',
);
expectWorkspaceField(
workspace,
'exports["."].import.default',
'./dist/index.mjs',
);
// All non-root package must provide the location of the CommonJS-compatible
// entrypoint and its matching type declaration file.
expectWorkspaceField(
workspace,
'exports["."].require.types',
'./dist/index.d.cts',
);
expectWorkspaceField(
workspace,
'exports["."].require.default',
'./dist/index.cjs',
);
expectWorkspaceField(workspace, 'main', './dist/index.cjs');
expectWorkspaceField(workspace, 'types', './dist/index.d.cts');
// Types should not be set in the export object directly, but rather in the
// `import` and `require` subfields.
expectWorkspaceField(workspace, 'exports["."].types', null);
// All non-root packages must export a `package.json` file.
expectWorkspaceField(
workspace,
'exports["./package.json"]',
'./package.json',
);
}
/**
* Expect that the workspace has "changelog:update" and "changelog:validate"
* scripts, and that these package scripts call a common script by passing the
* name of the package as the first argument.
*
* @param {Workspace} workspace - The workspace to check.
*/
function expectCorrectWorkspaceChangelogScripts(workspace) {
/**
* @type {Record<string, { expectedStartString: string, script: string, match: RegExpMatchArray | null }>}
*/
const scripts = ['update', 'validate'].reduce((obj, variant) => {
const expectedStartString = `../../scripts/${variant}-changelog.sh ${workspace.manifest.name}`;
/** @type {string} */
const script = workspace.manifest.scripts[`changelog:${variant}`] ?? '';
const match = script.match(new RegExp(`^${expectedStartString}(.*)$`, 'u'));
return { ...obj, [variant]: { expectedStartString, script, match } };
}, {});
if (
scripts.update.match &&
scripts.validate.match &&
scripts.update.match[1] !== scripts.validate.match[1]
) {
workspace.error(
'Expected package\'s "changelog:validate" and "changelog:update" scripts to pass the same arguments to their underlying scripts',
);
}
for (const [
variant,
{ expectedStartString, script, match },
] of Object.entries(scripts)) {
expectWorkspaceField(workspace, `scripts.changelog:${variant}`);
if (script !== '' && !match) {
workspace.error(
`Expected package's "changelog:${variant}" script to be or start with "${expectedStartString}", but it was "${script}".`,
);
}
}
}
/**
* Expect that if the workspace package lists another workspace package within
* `dependencies` or `devDependencies`, the version used within the dependency
* range is exactly equal to the current version of the dependency (and the
* range uses the `^` modifier).
*
* @param {Yarn} Yarn - The Yarn "global".
* @param {Workspace} workspace - The workspace to check.
*/
function expectUpToDateWorkspaceDependenciesAndDevDependencies(
Yarn,
workspace,
) {
for (const dependency of Yarn.dependencies({ workspace })) {
const dependencyWorkspace = Yarn.workspace({ ident: dependency.ident });
if (
dependencyWorkspace !== null &&
dependency.type !== 'peerDependencies'
) {
const ignoredRanges = ALLOWED_INCONSISTENT_DEPENDENCIES[dependency.ident];
if (ignoredRanges?.includes(dependency.range)) {
continue;
}
dependency.update(`^${dependencyWorkspace.manifest.version}`);
}
}
}
/**
* Expect that if the workspace package lists another workspace package within
* `peerDependencies`, the dependency range satisfies the current version of
* that package.
*
* @param {Yarn} Yarn - The Yarn "global".
* @param {Workspace} workspace - The workspace to check.
*/
function expectUpToDateWorkspacePeerDependencies(Yarn, workspace) {
for (const dependency of Yarn.dependencies({ workspace })) {
const dependencyWorkspace = Yarn.workspace({ ident: dependency.ident });
if (
dependencyWorkspace !== null &&
dependency.type === 'peerDependencies'
) {
const dependencyWorkspaceVersion = new semver.SemVer(
dependencyWorkspace.manifest.version,
);
if (
!semver.satisfies(
dependencyWorkspace.manifest.version,
dependency.range,
)
) {
expectWorkspaceField(
workspace,
`peerDependencies["${dependency.ident}"]`,
`^${dependencyWorkspaceVersion.major}.0.0`,
);
}
}
}
}
/**
* Expect that a workspace package does not list a dependency in both
* `dependencies` and `devDependencies`.
*
* @param {Workspace} workspace - The workspace to check.
* @param {Map<string, Map<DependencyType, Dependency>>} dependenciesByIdentAndType - Map of
* dependency ident to dependency type and dependency.
*/
function expectDependenciesNotInBothProdAndDev(
workspace,
dependenciesByIdentAndType,
) {
for (const [
dependencyIdent,
dependencyInstancesByType,
] of dependenciesByIdentAndType.entries()) {
if (
dependencyInstancesByType.size > 1 &&
!dependencyInstancesByType.has('peerDependencies')
) {
workspace.error(
`\`${dependencyIdent}\` cannot be listed in both \`dependencies\` and \`devDependencies\``,
);
}
}
}
/**
* Expect that if the workspace package lists another workspace package in its
* dependencies, and it is a controller package, that the controller package is
* listed in the workspace's `peerDependencies` and the version range satisfies
* the current version of the controller package.
*
* The expectation in this case is that the client will instantiate B in order
* to pass it into A. Therefore, it needs to list not only A as a dependency,
* but also B. Additionally, the version of B that the client is using with A
* needs to match the version that A itself is expecting internally.
*
* Note that this constraint does not apply for packages that seem to represent
* instantiable controllers but actually represent abstract classes.
*
* @param {Yarn} Yarn - The Yarn "global".
* @param {Workspace} workspace - The workspace to check.
* @param {Map<string, Map<DependencyType, Dependency>>} dependenciesByIdentAndType - Map of
* dependency ident to dependency type and dependency.
*/
function expectControllerDependenciesListedAsPeerDependencies(
Yarn,
workspace,
dependenciesByIdentAndType,
) {
for (const [
dependencyIdent,
dependencyInstancesByType,
] of dependenciesByIdentAndType.entries()) {
if (!dependencyInstancesByType.has('dependencies')) {
continue;
}
const dependencyWorkspace = Yarn.workspace({ ident: dependencyIdent });
if (
dependencyWorkspace !== null &&
dependencyIdent.endsWith('-controller') &&
dependencyIdent !== '@metamask/base-controller' &&
dependencyIdent !== '@metamask/polling-controller' &&
!dependencyInstancesByType.has('peerDependencies')
) {
const dependencyWorkspaceVersion = new semver.SemVer(
dependencyWorkspace.manifest.version,
);
expectWorkspaceField(
workspace,
`peerDependencies["${dependencyIdent}"]`,
`^${dependencyWorkspaceVersion.major}.0.0`,
);
}
}
}
/**
* Filter out dependency ranges which are not to be considered in `expectConsistentDependenciesAndDevDependencies`.
*
* @param {string} dependencyIdent - The dependency being filtered for
* @param {Map<string, Dependency>} dependenciesByRange - Dependencies by range
* @returns {Map<string, Dependency>} The resulting map.
*/
function getInconsistentDependenciesAndDevDependencies(
dependencyIdent,
dependenciesByRange,
) {
const ignoredRanges = ALLOWED_INCONSISTENT_DEPENDENCIES[dependencyIdent];
if (!ignoredRanges) {
return dependenciesByRange;
}
return new Map(
Object.entries(dependenciesByRange).filter(
([range]) => !ignoredRanges.includes(range),
),
);
}
/**
* Expect that all version ranges in `dependencies` and `devDependencies` for
* the same dependency across the entire monorepo are the same. As it is
* impossible to compare NPM version ranges, let the user decide if there are
* conflicts. (`peerDependencies` is a special case, and we handle that
* particularly for workspace packages elsewhere.)
*
* @param {Yarn} Yarn - The Yarn "global".
*/
function expectConsistentDependenciesAndDevDependencies(Yarn) {
const nonPeerDependenciesByIdent = getNonPeerDependenciesByIdent(
Yarn.dependencies(),
);
for (const [
dependencyIdent,
dependenciesByRange,
] of nonPeerDependenciesByIdent.entries()) {
if (dependenciesByRange.size <= 1) {
continue;
}
const dependenciesToConsider =
getInconsistentDependenciesAndDevDependencies(
dependencyIdent,
dependenciesByRange,
);
const dependencyRanges = [...dependenciesToConsider.keys()].sort();
for (const dependencies of dependenciesToConsider.values()) {
for (const dependency of dependencies) {
dependency.error(
`Expected version range for ${dependencyIdent} (in ${
dependency.type
}) to be consistent across monorepo. Pick one: ${inspect(
dependencyRanges,
)}`,
);
}
}
}
}
/**
* Expect that the workspace has a README.md file, and that it is a non-empty
* string. The README.md is expected to:
*
* - Not contain template instructions (unless the workspace is the module
* template itself).
* - Match the version of Node.js specified in the `.nvmrc` file.
*
* @param {Workspace} workspace - The workspace to check.
* @param {string} workspaceBasename - The name of the workspace.
* @returns {Promise<void>}
*/
async function expectReadme(workspace, workspaceBasename) {
const readme = await getWorkspaceFile(workspace, 'README.md');
if (
workspaceBasename !== 'metamask-module-template' &&
readme.includes('## Template Instructions')
) {
workspace.error(
'The README.md contains template instructions. These instructions should be removed.',
);
}
if (!readme.includes(`yarn add @metamask/${workspaceBasename}`)) {
workspace.error(
`The README.md does not contain an example of how to install the package using Yarn (\`yarn add @metamask/${workspaceBasename}\`). Please add an example.`,
);
}
if (!readme.includes(`npm install @metamask/${workspaceBasename}`)) {
workspace.error(
`The README.md does not contain an example of how to install the package using npm (\`npm install @metamask/${workspaceBasename}\`). Please add an example.`,
);
}
}