forked from BIYUEHU/HimenoSena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
80 lines (69 loc) · 2.12 KB
/
build.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
77
78
79
80
const [PUBLIC_DIR, PHP_DIR, DEST_DIR] = /* Deno.args */ [
"public",
"php",
"build",
];
if (!PUBLIC_DIR || !DEST_DIR) {
console.error("Usage: build.ts <public_dir> <dest_dir>");
Deno.exit(1);
}
function safe(fn: () => unknown) {
try {
fn();
} catch {
// ignore
}
}
function copyFile(
dir: string,
target: string,
filter?: (name: string) => boolean,
) {
safe(() => Deno.mkdirSync(dir, { recursive: true }));
safe(() => Deno.mkdirSync(target, { recursive: true }));
for (const file of Deno.readDirSync(dir)) {
if (file.isFile) {
if (filter && !filter(file.name)) continue;
const source = `${dir}/${file.name}`;
const dest = `${target}/${file.name}`;
Deno.copyFileSync(source, dest);
console.log(`Copied ${source} to ${dest}`);
} else if (file.isDirectory) {
copyFile(`${dir}/${file.name}`, `${target}/${file.name}`);
}
}
}
safe(() => Deno.removeSync(DEST_DIR, { recursive: true }));
safe(() => Deno.mkdirSync(DEST_DIR, { recursive: true }));
copyFile(PUBLIC_DIR, DEST_DIR);
copyFile(PHP_DIR, DEST_DIR, (name) => name.endsWith(".php"));
await new Deno.Command(Deno.execPath(), {
args: ["bundle", "src/main.ts", `${DEST_DIR}/bundle.js`],
}).spawn().output();
const time = new Date().toLocaleString();
const htmlComment =
/* html */ `<!-- Project: https://github.com/biyuehu/HimenoSena -->
<!-- Build time: ${time} -->`;
const jsAndCssComment = /* js */ `/**
* @project https://github.com/biyuehu/HimenoSena
* @license BCU
* @build ${time}
*/`;
const indexHtml = Deno.readTextFileSync(`${DEST_DIR}/index.html`);
const indexPhp = Deno.readTextFileSync(`${DEST_DIR}/index.php`);
Deno.removeSync(`${DEST_DIR}/index.html`);
Deno.writeTextFileSync(
`${DEST_DIR}/index.php`,
`${indexPhp}\n${htmlComment}\n${indexHtml}`,
);
const bundleJs = Deno.readTextFileSync(`${DEST_DIR}/bundle.js`);
Deno.writeTextFileSync(
`${DEST_DIR}/bundle.js`,
`${jsAndCssComment}\n\n${bundleJs}`,
);
const stylesCss = Deno.readTextFileSync(`${DEST_DIR}/styles.css`);
Deno.writeTextFileSync(
`${DEST_DIR}/styles.css`,
`${jsAndCssComment}\n\n${stylesCss}`,
);
console.log("Build complete.");