|
| 1 | +#!/usr/bin/env node |
| 2 | +// @ts-check |
| 3 | + |
| 4 | +// This performs `npm pack` and retrieves the list of artifact files from the output. |
| 5 | +// |
| 6 | +// In local dev, invoke it with `-updateArtifactList` to perform a dry run of `npm pack` |
| 7 | +// and recreate `packages/artifacts.txt`. |
| 8 | +// The exes for all platforms will then be included in the list, even if not present locally. |
| 9 | +// |
| 10 | +// In CI, the scripts is invoked without options. It then performs `npm pack` for real, |
| 11 | +// recreates the artifact list and verifies that it has no changes compared to the committed state. |
| 12 | + |
| 13 | +/** |
| 14 | + * @typedef {{ |
| 15 | + * path: string, |
| 16 | + * size: number, |
| 17 | + * mode: number, |
| 18 | + * }} PackOutputFile |
| 19 | + * |
| 20 | + * @typedef {{ |
| 21 | + * files: PackOutputFile[], |
| 22 | + * entryCount: number, |
| 23 | + * bundled: unknown[], |
| 24 | + * }} PackOutputEntry |
| 25 | + * |
| 26 | + * @typedef {[PackOutputEntry]} PackOutput |
| 27 | + */ |
| 28 | + |
| 29 | +const { spawnSync, execSync } = require("child_process"); |
| 30 | +const path = require("path"); |
| 31 | +const fs = require("fs"); |
| 32 | + |
| 33 | +const mode = process.argv.includes("-updateArtifactList") |
| 34 | + ? "updateArtifactList" |
| 35 | + : "package"; |
| 36 | + |
| 37 | +const rootPath = path.join(__dirname, ".."); |
| 38 | +const fileListPath = path.join(rootPath, "packages", "artifacts.txt"); |
| 39 | + |
| 40 | +const output = spawnSync( |
| 41 | + "npm pack --json" + (mode === "updateArtifactList" ? " --dry-run" : ""), |
| 42 | + { |
| 43 | + cwd: rootPath, |
| 44 | + encoding: "utf8", |
| 45 | + shell: true, |
| 46 | + }, |
| 47 | +).stdout; |
| 48 | + |
| 49 | +/** @type {PackOutput} */ |
| 50 | +const parsedOutput = JSON.parse(output); |
| 51 | +let filePaths = parsedOutput[0].files.map(file => file.path); |
| 52 | + |
| 53 | +if (mode === "updateArtifactList") { |
| 54 | + filePaths = Array.from(new Set(filePaths.concat(getFilesAddedByCI()))); |
| 55 | +} |
| 56 | + |
| 57 | +filePaths.sort(); |
| 58 | +fs.writeFileSync(fileListPath, filePaths.join("\n")); |
| 59 | + |
| 60 | +if (mode === "package") { |
| 61 | + execSync(`git diff --exit-code ${fileListPath}`, { stdio: "inherit" }); |
| 62 | +} |
| 63 | + |
| 64 | +function getFilesAddedByCI() { |
| 65 | + const platforms = ["darwin", "darwinarm64", "linux", "linuxarm64", "win32"]; |
| 66 | + const exes = [ |
| 67 | + "bsb_helper.exe", |
| 68 | + "bsc.exe", |
| 69 | + "ninja.exe", |
| 70 | + "rescript.exe", |
| 71 | + "rewatch.exe", |
| 72 | + ]; |
| 73 | + |
| 74 | + const files = ["ninja.COPYING"]; |
| 75 | + |
| 76 | + for (let platform of platforms) { |
| 77 | + for (let exe of exes) { |
| 78 | + files.push(`${platform}/${exe}`); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return files; |
| 83 | +} |
0 commit comments