-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-deno-deps.js
56 lines (48 loc) · 1.61 KB
/
update-deno-deps.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
import fs from "node:fs/promises";
import fss from "node:fs";
import path from "node:path";
import process from "node:process";
const cwd = process.cwd();
const denoNpmImportRe = /"npm:(.+?)@.*?"/g;
// iterate each template-deno-* directories
const directories = await fs.readdir(cwd);
for (const dir of directories) {
if (dir.startsWith("template-deno-")) {
const denoJsonPath = path.join(cwd, dir, "deno.json");
const denoJsonContent = await fs.readFile(denoJsonPath, "utf8");
let nodeVariantPkgJsonPath = path.join(
cwd,
dir.replace("-deno-", "-node-"),
"package.json",
);
if (!fss.existsSync(nodeVariantPkgJsonPath)) {
// fallback to this by default
nodeVariantPkgJsonPath = path.join(
cwd,
"template-node-base/package.json",
);
}
const nodeVariantPkgJson = JSON.parse(
await fs.readFile(nodeVariantPkgJsonPath, "utf8"),
);
// replace npm:* import with nodejs variant
const newDenoJsonContent = denoJsonContent.replace(
denoNpmImportRe,
(m, mod) => {
const versionSpecifier = nodeVariantPkgJson.dependencies?.[mod] ??
nodeVariantPkgJson.devDependencies?.[mod];
if (!versionSpecifier) {
// skip some deps for now
if (mod === "@deno/vite-plugin") return m;
console.error(
`No version specifier for ${mod} in ${nodeVariantPkgJsonPath} for ${dir}`,
);
return m;
}
return `"npm:${mod}@${versionSpecifier}"`;
},
);
await fs.writeFile(denoJsonPath, newDenoJsonContent);
console.log(`Updated ${dir}`);
}
}