-
Notifications
You must be signed in to change notification settings - Fork 2
/
postinstall.mjs
65 lines (61 loc) · 2.13 KB
/
postinstall.mjs
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
import { exec, spawnSync } from "node:child_process";
import { promisify } from "node:util";
import { parseDocument } from "yaml";
import { readFile, writeFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import semver from "semver";
let { stdout: pnpmVersion } = await promisify(exec)("pnpm --version", {
encoding: "utf-8",
});
pnpmVersion = pnpmVersion.trim();
const workflowURL = new URL("./.github/workflows/deploy.yml", import.meta.url);
const workflow = parseDocument(await readFile(workflowURL, "utf-8"));
/** @type {import('yaml').Node} */
const pnpmStep = workflow
.getIn(["jobs", "deploy", "steps"])
.items.find((step) => step.get("id") === "pnpm");
const requestedVersion = pnpmStep.getIn(["with", "version"]);
const red = "\x1b[31m";
const green = "\x1b[32m";
const yellow = "\x1b[32m";
const bold = "\x1b[1m";
const reset = "\x1b[0m";
if (pnpmVersion !== requestedVersion) {
if (process.argv.includes("--update")) {
pnpmStep.setIn(["with", "version"], pnpmVersion);
await writeFile(workflowURL, workflow.toString());
spawnSync(
"pnpm",
[
"exec",
"prettier",
"--write",
"--loglevel",
"warn",
fileURLToPath(workflowURL),
],
{ stdio: "inherit" }
);
const action = semver.gt(pnpmVersion, requestedVersion)
? `upgraded`
: `${yellow}downgraded${reset}${green}`;
console.log(
`${green}Successfully ${action} pnpm to v${pnpmVersion} in .github/workflows/deploy.yml${reset}`
);
} else {
console.error(
`${red}Expected pnpm v${pnpmVersion} but found v${requestedVersion} in .github/workflows/deploy.yml${reset}`
);
if (semver.lt(pnpmVersion, requestedVersion)) {
console.log(
`${yellow}*** This is a DOWNGRADE. Consider updating your local pnpm by running ${bold}pnpm update -g pnpm${reset}${yellow}! ***${reset}`
);
}
console.error(
` → Run ${bold}node postinstall.mjs --update${reset} to update the workflow`
);
process.exit(1);
}
} else if (process.argv.includes("--update")) {
console.log(`No need to update pnpm in .github/workflows/deploy.yml`);
}