#!/usr/bin/env node import { globSync } from 'glob' import fs from 'fs' import path from 'path' import crypto from 'crypto' import { EOL } from 'os' function sha256(filePath) { const hash = crypto.createHash('sha256') const stream = fs.createReadStream(filePath) return new Promise((resolve, reject) => { stream.on('data', (d) => hash.update(d)) stream.on('end', () => { resolve(hash.digest('hex')) }) stream.on('error', reject) }) } async function main() { const sha256TxtLines = [] const root = '/build/dist' const fileManifestPromises = globSync(`${root}/**/*`) .filter((path) => fs.lstatSync(path).isFile()) .map(async (filePath) => { const sum = await sha256(filePath) const rel = path.relative(root, filePath) sha256TxtLines.push([sum, rel]) return { sha256: sum, path: rel } }) const fileManifests = await Promise.all(fileManifestPromises) const asJSON = JSON.stringify({ files: fileManifests }, null, 2) fs.writeFileSync(path.join(root, 'SHA256.json'), asJSON) let asText = sha256TxtLines.map((line) => line.join(' ')).join(EOL) asText = sha256TxtLines.length === 1 ? asText + EOL : asText fs.writeFileSync( path.join(root, 'SHA256.txt'), asText, {encoding: 'utf8'} ) } main()