-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcheck.ts
357 lines (320 loc) · 10.9 KB
/
check.ts
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
import path from 'path';
import { execa } from 'execa';
import fse from 'fs-extra';
import { globby } from 'globby';
import pLimit from 'p-limit';
import * as resolve from 'resolve.exports';
import zod from 'zod';
import { createCommand } from '../command.js';
import { getBobConfig } from '../config.js';
import { getRootPackageJSON } from '../utils/get-root-package-json.js';
import { getWorkspacePackagePaths } from '../utils/get-workspace-package-paths.js';
import { getWorkspaces } from '../utils/get-workspaces.js';
import { presetFieldsDual } from './bootstrap.js';
const ExportsMapEntry = zod.object({
default: zod.string(),
types: zod.string(),
});
const ExportsMapModel = zod.record(
zod.union([
zod.string(),
zod.object({
require: zod.optional(ExportsMapEntry),
import: ExportsMapEntry,
default: ExportsMapEntry,
}),
]),
);
const EnginesModel = zod.record(zod.string(), zod.string());
const BinModel = zod.record(zod.string());
export const checkCommand = createCommand<{}, {}>(api => {
return {
command: 'check',
describe:
'Check whether all files in the exports map within the built package can be imported.',
builder(yargs) {
return yargs.options({});
},
async handler() {
const cwd = process.cwd();
const rootPackageJSON = await getRootPackageJSON();
const workspaces = await getWorkspaces(rootPackageJSON);
const isSinglePackage = workspaces === null;
let checkConfigs: Array<{
cwd: string;
packageJSON: Record<string, unknown>;
}> = [];
if (isSinglePackage) {
checkConfigs.push({
cwd,
packageJSON: rootPackageJSON,
});
} else {
const workspacesPaths = await getWorkspacePackagePaths(workspaces);
const limit = pLimit(20);
await Promise.all(
workspacesPaths.map(workspacePath =>
limit(async () => {
const packageJSONPath = path.join(workspacePath, 'package.json');
const packageJSON: Record<string, unknown> = await fse.readJSON(packageJSONPath);
checkConfigs.push({
cwd: workspacePath,
packageJSON,
});
}),
),
);
}
const limit = pLimit(20);
let didFail = false;
await Promise.allSettled(
checkConfigs.map(({ cwd, packageJSON }) =>
limit(async () => {
const config = getBobConfig(packageJSON);
if (config === false || config?.check === false) {
api.reporter.warn(`Skip check for '${packageJSON.name}'.`);
return;
}
const distPackageJSONPath = path.join(cwd, 'dist', 'package.json');
const distPackageJSON = await fse.readJSON(distPackageJSONPath);
try {
await checkExportsMapIntegrity({
cwd: path.join(cwd, 'dist'),
packageJSON: distPackageJSON,
skipExports: new Set<string>(config?.check?.skip ?? []),
dual: config?.commonjs ?? true,
});
await checkEngines({
packageJSON: distPackageJSON,
});
} catch (err) {
api.reporter.error(`Integrity check of '${packageJSON.name}' failed.`);
api.reporter.log(err);
didFail = true;
return;
}
api.reporter.success(`Checked integrity of '${packageJSON.name}'.`);
}),
),
);
if (didFail) {
throw new Error('One ore more integrity checks failed.');
}
},
};
});
async function checkExportsMapIntegrity(args: {
cwd: string;
packageJSON: {
name: string;
exports: any;
bin: unknown;
};
skipExports: Set<string>;
dual: boolean;
}) {
const exportsMapResult = ExportsMapModel.safeParse(args.packageJSON['exports']);
if (exportsMapResult.success === false) {
throw new Error(
"Missing exports map within the 'package.json'.\n" +
exportsMapResult.error.message +
'\nCorrect Example:\n' +
JSON.stringify(presetFieldsDual.exports, null, 2),
);
}
const exportsMap = exportsMapResult['data'];
const cjsSkipExports = new Set<string>();
const esmSkipExports = new Set<string>();
for (const definedExport of args.skipExports) {
if (args.dual) {
const cjsResult = resolve.resolve(args.packageJSON, definedExport, {
require: true,
})?.[0];
if (typeof cjsResult === 'string') {
cjsSkipExports.add(cjsResult);
}
}
const esmResult = resolve.resolve(args.packageJSON, definedExport)?.[0];
if (typeof esmResult === 'string') {
esmSkipExports.add(esmResult);
}
}
for (const key of Object.keys(exportsMap)) {
if (args.dual) {
const cjsResult = resolve.resolve(args.packageJSON, key, {
require: true,
})?.[0];
if (!cjsResult) {
throw new Error(
`Could not resolve CommonJS import '${key}' for '${args.packageJSON.name}'.`,
);
}
if (cjsResult.match(/.(js|cjs)$/)) {
const cjsFilePaths = await globby(cjsResult, {
cwd: args.cwd,
});
if (!cjsFilePaths.length) {
throw new Error(
`No files found matching the path '${cjsResult}' in '${key}' for '${args.packageJSON.name}'.`,
);
}
const limit = pLimit(20);
await Promise.all(
cjsFilePaths.map(file =>
limit(async () => {
if (cjsSkipExports.has(file)) {
return;
}
const result = await runRequireJSFileCommand({
path: file,
cwd: args.cwd,
});
if (result.exitCode !== 0) {
throw new Error(
`Require of file '${file}' failed.\n` +
`In case this file is expected to raise an error please add an export to the 'bob.check.skip' field in your 'package.json' file.\n` +
`Error:\n` +
result.stderr,
);
}
}),
),
);
} else {
// package.json or other files
// for now we just make sure they exists
await fse.stat(path.join(args.cwd, cjsResult));
}
}
const esmResult = resolve.resolve({ exports: exportsMap }, key)?.[0];
if (!esmResult) {
throw new Error(`Could not resolve export '${key}' in '${args.packageJSON.name}'.`);
}
if (esmResult.match(/.(js|mjs)$/)) {
const esmFilePaths = await globby(esmResult, {
cwd: args.cwd,
});
if (!esmFilePaths.length) {
throw new Error(
`No files found matching the path '${esmResult}' in '${key}' for '${args.packageJSON.name}'.`,
);
}
const limit = pLimit(20);
await Promise.all(
esmFilePaths.map(file =>
limit(async () => {
if (esmSkipExports.has(file)) {
return;
}
const result = await runImportJSFileCommand({
path: file,
cwd: args.cwd,
});
if (result.exitCode !== 0) {
throw new Error(`Import of file '${file}' failed with error:\n` + result.stderr);
}
}),
),
);
} else {
// package.json or other files
// for now we just make sure they exists
await fse.stat(path.join(args.cwd, esmResult));
}
}
const exportsRequirePath = resolve.resolve({ exports: exportsMap }, '.', { require: true })?.[0];
if (!exportsRequirePath || typeof exportsRequirePath !== 'string') {
throw new Error('Could not resolve default CommonJS entrypoint in a Module project.');
}
if (args.dual) {
const requireResult = await runRequireJSFileCommand({
path: exportsRequirePath,
cwd: args.cwd,
});
if (requireResult.exitCode !== 0) {
throw new Error(
`Require of file '${exportsRequirePath}' failed with error:\n` + requireResult.stderr,
);
}
} else {
const importResult = await runImportJSFileCommand({
path: exportsRequirePath,
cwd: args.cwd,
});
if (importResult.exitCode !== 0) {
throw new Error(
`Import of file '${exportsRequirePath}' failed with error:\n` + importResult.stderr,
);
}
}
const legacyImport = resolve.legacy(args.packageJSON);
if (!legacyImport || typeof legacyImport !== 'string') {
throw new Error('Could not resolve default ESM entrypoint.');
}
const legacyImportResult = await runImportJSFileCommand({
path: legacyImport,
cwd: args.cwd,
});
if (legacyImportResult.exitCode !== 0) {
throw new Error(
`Require of file '${exportsRequirePath}' failed with error:\n` + legacyImportResult.stderr,
);
}
if (args.packageJSON.bin) {
const result = BinModel.safeParse(args.packageJSON.bin);
if (result.success === false) {
throw new Error('Invalid format of bin field in package.json.\n' + result.error.message);
}
const cache = new Set<string>();
for (const filePath of Object.values(result.data)) {
if (cache.has(filePath)) {
continue;
}
cache.add(filePath);
const absoluteFilePath = path.join(args.cwd, filePath);
await fse.stat(absoluteFilePath).catch(() => {
throw new Error("Could not find binary file '" + absoluteFilePath + "'.");
});
await fse.access(path.join(args.cwd, filePath), fse.constants.X_OK).catch(() => {
throw new Error(
"Binary file '" +
absoluteFilePath +
"' is not executable.\n" +
`Please set the executable bit e.g. by running 'chmod +x "${absoluteFilePath}"'.`,
);
});
const contents = await fse.readFile(absoluteFilePath, 'utf-8');
if (!contents.startsWith('#!/usr/bin/env node\n')) {
throw new Error(
"Binary file '" +
absoluteFilePath +
"' does not have a shebang.\n Please add '#!/usr/bin/env node' to the beginning of the file.",
);
}
}
}
}
async function checkEngines(args: {
packageJSON: {
name: string;
engines: unknown;
};
}) {
const engines = EnginesModel.safeParse(args.packageJSON.engines);
if (engines.success === false || engines.data['node'] === undefined) {
throw new Error('Please specify the node engine version in your package.json.');
}
}
const timeout = `;setTimeout(() => { throw new Error("The Node.js process hangs. There is probably some side-effects. All exports should be free of side effects.") }, 500).unref()`;
function runRequireJSFileCommand(args: { cwd: string; path: string }) {
return execa('node', ['-e', `require('${args.path}')${timeout}`], {
cwd: args.cwd,
reject: false,
});
}
function runImportJSFileCommand(args: { cwd: string; path: string }) {
return execa('node', ['-e', `import('${args.path}').then(() => {${timeout}})`], {
cwd: args.cwd,
reject: false,
});
}