-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
76 lines (66 loc) · 2.06 KB
/
index.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
import { Array, pipe } from "effect";
import fs from "fs";
import path from "path";
const touch = async (filename: string) => {
const time = new Date();
await fs.promises.utimes(filename, time, time).catch(async function (err) {
if ("ENOENT" !== err.code) {
throw err;
}
let fh = await fs.promises.open(filename, "a");
await fh.close();
});
};
const repoNames = process.argv[2]?.split(",") ?? [];
const patches = (await import("./package.json", { assert: { type: "json" } }))
.default.pnpm.patchedDependencies;
const repos = repoNames.map((repo) => path.join(process.env.HOME!, "pj", repo));
const availablePatches = Object.entries(patches).map(([name, patch]) => {
return [
name.substring(1).includes("@")
? name.substring(0, name.substring(1).indexOf("@") + 1)
: name,
name,
patch,
];
});
await Promise.all(
repos.map(async (repo) => {
const pj = await import(path.join(repo, "package.json"), {
assert: { type: "json" },
}).then((_) => _.default);
const desiredPatches = pipe(
Object.entries(pj.pnpm.patchedDependencies).map(([name, patch]) => {
return name.substring(1).includes("@")
? name.substring(0, name.substring(1).indexOf("@") + 1)
: name;
}),
Array.filterMap((name) =>
Array.findFirst(
availablePatches,
([desiredName]) => desiredName === name
)
)
);
const repoPatches = path.join(repo, "patches");
if (fs.existsSync(repoPatches)) {
fs.rmSync(repoPatches, { recursive: true });
}
fs.mkdirSync(repoPatches);
await touch(repoPatches + "/.gitkeep");
pj.pnpm.patchedDependencies = desiredPatches.reduce(
(acc, [_, desiredName, desiredPatch]) => {
acc[desiredName] = desiredPatch;
return acc;
},
{} as any
);
fs.writeFileSync(
path.join(repo, "package.json"),
JSON.stringify(pj, null, 2)
);
desiredPatches.forEach(([_, __, desiredPatch]) => {
fs.copyFileSync(desiredPatch, path.join(repo, desiredPatch));
});
})
);