37 lines
1.7 KiB
JavaScript
37 lines
1.7 KiB
JavaScript
import { readFileSync, writeFileSync, readdirSync } from 'node:fs';
|
|
import { createHash } from 'node:crypto';
|
|
import { join } from 'node:path';
|
|
|
|
const hash = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
const root = 'dist';
|
|
function collect(directory, prefix = '') {
|
|
const files = [];
|
|
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
const path = prefix + entry.name;
|
|
const fullPath = join(directory, entry.name);
|
|
if (entry.name.startsWith('.') || entry.isSymbolicLink()) throw new Error(`Unsafe release path: ${path}`);
|
|
if (entry.isDirectory()) files.push(...collect(fullPath, path + '/'));
|
|
else if (entry.isFile()) files.push([path, hash(readFileSync(fullPath))]);
|
|
else throw new Error(`Non-regular release path: ${path}`);
|
|
}
|
|
return files;
|
|
}
|
|
|
|
export function packageRelease(websiteSha, rootSha, pointer) {
|
|
const files = Object.fromEntries(collect(root).sort(([a], [b]) => a < b ? -1 : Number(a !== b)));
|
|
if (pointer && !files['examples/geometry/index.html']) throw new Error('Production requires the verified View example');
|
|
const identity = {
|
|
website_sha: websiteSha,
|
|
root_sha: rootSha,
|
|
mode: pointer ? 'release' : 'safe',
|
|
revision: pointer?.revision ?? null,
|
|
snapshot_sha256: pointer?.sha256 ?? null,
|
|
npm_lock_sha256: hash(readFileSync('package-lock.json')),
|
|
artifacts_sha256: hash(JSON.stringify(files)),
|
|
node_version: process.version,
|
|
};
|
|
writeFileSync(join(root, 'build.json'), JSON.stringify(identity) + '\n');
|
|
writeFileSync(join(root, 'files.sha256'), Object.entries(files).map(([file, digest]) => `${digest} ${file}`).join('\n') + '\n');
|
|
console.log(JSON.stringify({ ...identity, built_at: new Date().toISOString() }, null, 2));
|
|
}
|