-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.js
75 lines (59 loc) · 1.86 KB
/
template.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
import fs from "node:fs/promises";
import path from "node:path";
const template = path.join(import.meta.dirname, "template");
const read = (file) => fs.readFile(path.join(template, file), "utf8");
export async function buildBase(projectName) {
const files = {};
const baseFiles = [
".github/workflows/deploy.yml",
".prettierrc",
".prettierignore",
"eslint.config.mjs",
"build.mjs",
"repo.mjs",
"tsconfig.json"
];
for (const file of baseFiles) files[file] = await read(file);
const packageJSON = JSON.parse(await read("package.json"));
packageJSON.name = projectName;
const typesPackage = await fetch("https://registry.npmjs.com/@moonlight-mod/types", {
headers: {
"User-Agent": "@moonlight-mod/create-extension (+https://github.com/moonlight-mod/create-extension)"
}
}).then((r) => r.json());
packageJSON.dependencies["@moonlight-mod/types"] = "^" + typesPackage["dist-tags"]["latest"];
files["package.json"] = JSON.stringify(packageJSON, null, 2);
// npm is too smart about stripping files
files[".gitignore"] = `/dist
/repo
/node_modules
`;
return files;
}
export async function buildExt(extId, envPath) {
const files = {};
if (envPath) {
let str = await fs.readFile(envPath, "utf8");
str += `
declare module "@moonlight-mod/wp/${extId}_someLibrary" {
export * from "${extId}/webpackModules/someLibrary";
}
`;
files["env.d.ts"] = str;
} else {
files["env.d.ts"] = await read("env.d.ts").then((r) => r.replaceAll("sampleExtension", extId));
}
const extSpecific = [
"manifest.json",
"index.tsx",
"node.ts",
"webpackModules/entrypoint.ts",
"webpackModules/someLibrary.ts"
];
for (const file of extSpecific) {
files[`src/${extId}/${file}`] = await read(`src/sampleExtension/${file}`).then((r) =>
r.replaceAll("sampleExtension", extId)
);
}
return files;
}