This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
generated from MetaMask/metamask-module-template
-
Notifications
You must be signed in to change notification settings - Fork 16
/
yarn.config.cjs
285 lines (250 loc) · 9.41 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
/** @type {import('@yarnpkg/types')} */
const { defineConfig } = require('@yarnpkg/types');
const { readFile } = require('fs/promises');
const { basename, resolve } = require('path');
/**
* Aliases for the Yarn type definitions, to make the code more readable.
*
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Workspace} Workspace
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Dependency} Dependency
*/
/**
* The base URL for the GitHub repository.
*
* @type {string}
*/
const BASE_URL = 'https://github.com/MetaMask/';
/**
* Get the name of the workspace. The workspace name is expected to be in the
* form `@metamask/workspace-name`, and this function will extract
* `workspace-name`.
*
* @param {Workspace} workspace - The workspace.
* @returns {string} The name of the workspace.
*/
function getWorkspaceName(workspace) {
return basename(workspace.ident);
}
/**
* 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');
}
/**
* 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} field - The field to check.
* @param {any} [value] - The value to check.
*/
function expectWorkspaceField(workspace, field, value) {
const fieldValue = workspace.manifest[field];
if (fieldValue === null) {
workspace.error(`Missing required field "${field}".`);
return;
}
if (value) {
workspace.set(field, value);
}
}
/**
* Expect that the workspace has a description, and that it is a non-null
* 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.endsWith('.')) {
workspace.set('description', description.slice(0, -1));
}
}
/**
* Expect that if a dependency is listed under "dependencies", it is not also
* listed under "devDependencies". If it is, this will log an error, and cause
* the constraint to fail.
*
* @param {Workspace} workspace - The workspace to check.
*/
function expectWorkspaceDependencies(workspace) {
workspace.pkg.dependencies.forEach((dependency) => {
// `workspace.pkg` does not have a `devDependencies` field, so we need to
// check the `manifest` instead.
const isDependency = Boolean(
workspace.manifest.dependencies?.[dependency.ident],
);
const isDevDependency = Boolean(
workspace.manifest.devDependencies?.[dependency.ident],
);
if (isDependency && isDevDependency) {
workspace.unset(`devDependencies.${dependency.ident}`);
}
});
}
/**
* 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} workspaceName - The name of the workspace.
* @returns {Promise<void>}
*/
async function expectReadme(workspace, workspaceName) {
const readme = await getWorkspaceFile(workspace, 'README.md');
if (
workspaceName !== '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/${workspaceName}`)) {
workspace.error(
`The README.md does not contain an example of how to install the package using Yarn (\`yarn add @metamask/${workspaceName}\`). Please add an example.`,
);
}
if (!readme.includes(`npm install @metamask/${workspaceName}`)) {
workspace.error(
`The README.md does not contain an example of how to install the package using npm (\`npm install @metamask/${workspaceName}\`). Please add an example.`,
);
}
}
/**
* Expect that the workspace has a pull_request_template.md file, and that it
* is a non-empty string. The pull_request_template.md is expected to:
*
* - Not contain an examples section (unless the workspace is the module
* template itself).
*
* @param {Workspace} workspace - The workspace to check.
* @param {string} workspaceName - The name of the workspace.
* @returns {Promise<void>}
*/
async function expectPullRequestTemplate(workspace, workspaceName) {
if (workspaceName === 'metamask-module-template') {
return;
}
const pullRequestTemplate = await getWorkspaceFile(
workspace,
'.github/PULL_REQUEST_TEMPLATE.md',
);
if (!pullRequestTemplate) {
workspace.error(
'The pull_request_template.md is missing. This should be added.',
);
}
if (pullRequestTemplate.includes('## Examples')) {
workspace.error(
'The pull_request_template.md contains an examples section. This section should be removed.',
);
}
}
/**
* Expect that the workspace has a valid `exports` field. The `exports` field
* is expected to:
*
* - Export a `types` entrypoint as the first export, or not at all.
*
* This is required for proper TypeScript support when using `Node16` (or later)
* module resolution.
*
* @param {Workspace} workspace - The workspace to check.
* @returns {void}
*/
function expectExports(workspace) {
const { exports: manifestExports } = workspace.manifest;
Object.entries(manifestExports)
.filter(([, exportValue]) => typeof exportValue !== 'string')
.forEach(([exportName, exportObject]) => {
const keys = Object.keys(exportObject);
if (keys.includes('types') && keys[0] !== 'types') {
workspace.error(
`The "types" export must be the first export in the "exports" field for the export "${exportName}".`,
);
}
});
}
module.exports = defineConfig({
async constraints({ Yarn }) {
const workspace = Yarn.workspace();
const workspaceName = getWorkspaceName(workspace);
const workspaceRepository = `${BASE_URL}${workspaceName}`;
// The package must have a name, version, description, and license.
expectWorkspaceField(workspace, 'name', `@metamask/${workspaceName}`);
expectWorkspaceField(workspace, 'version');
expectWorkspaceField(workspace, 'license');
expectWorkspaceDescription(workspace);
// The package must have a valid README.md file.
await expectReadme(workspace, workspaceName);
// The package must have a valid pull request template.
await expectPullRequestTemplate(workspace, workspaceName);
expectWorkspaceDependencies(workspace);
// The homepage of the package must match its name.
workspace.set('homepage', `${workspaceRepository}#readme`);
// The bugs URL of the package must point to the Issues page for the
// repository.
workspace.set('bugs.url', `${workspaceRepository}/issues`);
// The package must specify Git as the repository type, and match the URL of
// a repository within the MetaMask organization.
workspace.set('repository.type', 'git');
workspace.set('repository.url', `${workspaceRepository}.git`);
// The package must specify a minimum Node.js version of 16.
workspace.set('engines.node', '^18.18 || >=20');
// The package must specify a `types` entrypoint, and an `import`
// entrypoint.
workspace.set('types', './dist/types/index.d.ts');
workspace.set('exports["."].types', './dist/types/index.d.ts');
// The package must specify a `main` and `module` entrypoint, and a
// `require` and `import` entrypoint.
workspace.set('main', './dist/index.js');
workspace.set('exports["."].require', './dist/index.js');
workspace.set('module', './dist/index.mjs');
workspace.set('exports["."].import', './dist/index.mjs');
// The package must export a `package.json` file.
workspace.set('exports["./package.json"]', './package.json');
expectExports(workspace);
// The list of files included in the package must only include files
// generated during the build process.
workspace.set('files', ['dist']);
// The package is public, and should be published to the npm registry.
workspace.unset('private');
workspace.set('publishConfig.access', 'public');
workspace.set('publishConfig.registry', 'https://registry.npmjs.org/');
},
});