-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostinstall.js
131 lines (122 loc) · 5.47 KB
/
postinstall.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
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
console.info("-".repeat(73));
import fs from "node:fs";
import path from "node:path";
import { readPackageJSON, writePackageJSON, resolvePackageJSON } from "pkg-types";
import { startGroup, endGroup } from "@actions/core";
import IS_IN_GITHUB_ACTIONS from "./modules/IS_IN_GITHUB_ACTIONS.js";
import git from "./modules/git.js";
import upstreamExist from "./modules/getUpstream.js";
const IS_DRY_RUN = process.argv.includes("--dry-run");
/**
* @type { import("../package-lock.json") }
*/
const packageLockJSON = JSON.parse(await fs.promises.readFile("./package-lock.json", "utf8"));
/**
* @type { import("../package.json") }
*/
const packageJSON = await readPackageJSON();
let globalChanged = false;
console.info("Start to update the package.json of packages/");
const unusedDependencies = new Set(Object.keys(packageJSON.dependencies));
/**
* @type { Record<string, { pkgPath, pkgJSONPath: string, pkgJSON: Awaited<ReturnType<readPackageJSON>> }> }
*/
const packagesList = Object.fromEntries(await Promise.all((await fs.promises.readdir("./packages")).map(async (pkg) => {
const pkgPath = path.resolve("./packages", pkg);
return [
pkg,
{
pkgJSONPath: await resolvePackageJSON(pkgPath),
pkgJSON: await readPackageJSON(await resolvePackageJSON(pkgPath)),
},
];
})));
for (const [pkg, { pkgJSONPath, pkgJSON }] of Object.entries(packagesList)) {
console.info(`[${pkg}]`, "Start to update package.json");
let pkgChanged = false;
for (const property of ["dependencies", "devDependencies"]) {
if (!Reflect.has(pkgJSON, property)) {
continue;
}
for (const [dependencyName, dependencyVersionString] of Object.entries(pkgJSON[property])) {
unusedDependencies.delete(dependencyName);
/**
* @type { { version: string } | undefined }
*/
const lockInfo = packageLockJSON.packages[`node_modules/${dependencyName}`];
if (typeof lockInfo?.version === "string") {
const targetVersion = `^${lockInfo.version}`;
if (dependencyVersionString !== `^${lockInfo.version}`) {
console.warn(`[${pkg}]`, `[${property}]`, `Version mismatch for ${dependencyName}: ${dependencyVersionString} vs ${targetVersion} by dependency`);
pkgChanged = true;
pkgJSON[property][dependencyName] = targetVersion;
}
} else {
const localPackageName = dependencyName.replace(/^@[^/]+\//, "");
if (Reflect.has(packagesList, localPackageName)) {
const targetVersion = `^${packagesList[localPackageName].pkgJSON.version}`;
if (dependencyVersionString !== targetVersion) {
console.warn(`[${pkg}]`, `[${property}]`, `Version mismatch for ${dependencyName}: ${dependencyVersionString} vs ${targetVersion} by local package`);
pkgChanged = true;
pkgJSON[property][dependencyName] = targetVersion;
}
}
}
}
}
if (pkgJSON.engines.node !== packageJSON.engines.node) {
console.warn(`[${pkg}]`, `Node version mismatch: ${pkgJSON.engines.node} vs ${packageJSON.engines.node}`);
pkgChanged = true;
pkgJSON.engines.node = packageJSON.engines.node;
}
if (pkgChanged) {
globalChanged = true;
console.info(`[${pkg}]`, "There are some changes in package.json, write it back");
if (!IS_DRY_RUN) {
await writePackageJSON(pkgJSONPath, pkgJSON);
}
} else {
console.info(`[${pkg}]`, "There is no change in package.json");
}
}
console.info("Parsing the scripts to exclude used dependencies");
for (const file of ["./eslint.config.js", ...(await fs.promises.readdir("./scripts", { withFileTypes: true, recursive: true })).filter((dirent) => dirent.isFile()).map((dirent) => path.resolve(dirent.path, dirent.name))]) {
const content = `\n${await fs.promises.readFile(file, { encoding: "utf-8" })}`;
const matches = content.match(/(?<=\nimport)[^\n]+? from "[^\n"]+";?\n/g);
if (!matches) {
continue;
}
for (const match of matches) {
const dependencyName = match.match(/(?<= from ")[^"]+/)?.[0];
if (dependencyName && !/^node:|^\./.test(dependencyName)) {
unusedDependencies.delete(dependencyName);
}
}
}
if (unusedDependencies.size > 0) {
process.emitWarning(`There are some unused dependencies: ${[...unusedDependencies].join(", ")}`);
} else {
console.info("There is no unused dependency");
}
if (!globalChanged) {
console.info("There is no change in package.json.");
} else if (!IS_IN_GITHUB_ACTIONS) {
console.info("Not running in GitHub Actions, skip committing.");
} else if (!upstreamExist) {
console.info("Running in GitHub Actions, but the upstream does not exist, skip committing.");
} else if (process.env.GITHUB_REF !== "refs/heads/master") {
console.info("Running in GitHub Actions, but the current ref is not a branch, skip committing.");
} else {
console.info("Running in GitHub Actions, commit the changes.");
startGroup("Add result:");
console.info(await git.add("."));
endGroup();
startGroup("Commit result:");
console.info(await git.commit("chore: update dependencies"));
endGroup();
startGroup("Push result:");
console.info(await git.push());
endGroup();
}
console.info("Done.");
console.info("-".repeat(73));