tools/build-site.mjs (1529B)
1 #!/usr/bin/env node
2 // Assemble the deployable site into _site/: the static page from site/, the
3 // two server-side files under downloads/ with a SHA256SUMS file, and
4 // config.json pointing at the source repository. The deploy workflow and
5 // `npm run serve` both use this, so a local preview matches production.
6
7 import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
8 import { createHash } from 'node:crypto';
9
10 const ROOT = new URL('..', import.meta.url);
11 const OUT = new URL('_site/', ROOT);
12 const DOWNLOADS = [
13 ['check/php2shell-selfcheck.php', 'php2shell-selfcheck.php'],
14 ['scripts/php2shell-server-check.sh', 'php2shell-server-check.sh'],
15 ];
16
17 const server = process.env.GITHUB_SERVER_URL || 'https://github.com';
18 const repository = process.env.GITHUB_REPOSITORY || 'debuman/php2shell-scanner';
19
20 await rm(OUT, { recursive: true, force: true });
21 await cp(new URL('site/', ROOT), OUT, { recursive: true });
22 await mkdir(new URL('downloads/', OUT), { recursive: true });
23
24 const sums = [];
25 for (const [from, name] of DOWNLOADS) {
26 const data = await readFile(new URL(from, ROOT));
27 await writeFile(new URL(`downloads/${name}`, OUT), data);
28 sums.push(`${createHash('sha256').update(data).digest('hex')} ${name}`);
29 }
30 await writeFile(new URL('downloads/SHA256SUMS', OUT), sums.join('\n') + '\n');
31 await writeFile(new URL('config.json', OUT), JSON.stringify({ repository: `${server}/${repository}` }) + '\n');
32
33 console.log(`Built _site/ (${DOWNLOADS.length} downloads, repository ${server}/${repository})`);