-
Notifications
You must be signed in to change notification settings - Fork 455
/
Copy pathnpmPack.js
executable file
·85 lines (71 loc) · 2.14 KB
/
npmPack.js
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
#!/usr/bin/env node
// @ts-check
// This performs `npm pack` and retrieves the list of artifact files from the output.
//
// In local dev, invoke it with `-updateArtifactList` to perform a dry run of `npm pack`
// and recreate `packages/artifacts.txt`.
// The exes for all platforms will then be included in the list, even if not present locally.
//
// In CI, the scripts is invoked without options. It then performs `npm pack` for real,
// recreates the artifact list and verifies that it has no changes compared to the committed state.
/**
* @typedef {{
* path: string,
* size: number,
* mode: number,
* }} PackOutputFile
*
* @typedef {{
* files: PackOutputFile[],
* entryCount: number,
* bundled: unknown[],
* }} PackOutputEntry
*
* @typedef {[PackOutputEntry]} PackOutput
*/
const { spawnSync, execSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const mode = process.argv.includes("-updateArtifactList")
? "updateArtifactList"
: "package";
const rootPath = path.join(__dirname, "..");
const fileListPath = path.join(rootPath, "packages", "artifacts.txt");
const output = spawnSync(
"npm pack --json" + (mode === "updateArtifactList" ? " --dry-run" : ""),
{
cwd: rootPath,
encoding: "utf8",
shell: true,
},
).stdout;
/** @type {PackOutput} */
const parsedOutput = JSON.parse(output);
let filePaths = parsedOutput[0].files.map(file => file.path);
if (mode === "updateArtifactList") {
filePaths = Array.from(new Set(filePaths.concat(getFilesAddedByCI())));
}
filePaths.sort();
fs.writeFileSync(fileListPath, filePaths.join("\n"));
if (mode === "package") {
execSync(`git diff --exit-code ${fileListPath}`, { stdio: "inherit" });
}
function getFilesAddedByCI() {
const platforms = ["darwin", "darwinarm64", "linux", "linuxarm64", "win32"];
const exes = [
"bsb_helper.exe",
"bsc.exe",
"ninja.exe",
"rescript.exe",
"rescript-editor-analysis.exe",
"rescript-tools.exe",
"rewatch.exe",
];
const files = ["ninja.COPYING"];
for (let platform of platforms) {
for (let exe of exes) {
files.push(`${platform}/${exe}`);
}
}
return files;
}