build: hash and create ".sha256sum" for binary built (#157)

This commit is contained in:
Jesse Chan 2021-04-07 22:09:21 +08:00 committed by GitHub
parent f79a54c2c8
commit 52a1754e42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -166,6 +166,26 @@ async function compile(nodeVersion: string, targetArch: string) {
return compileOnUnix(nodeVersion, targetArch);
}
async function hash(filePath: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
const resultHash = crypto.createHash('sha256');
const input = fs.createReadStream(filePath);
input.on('error', (e) => {
reject(e);
});
input.on('readable', () => {
const data = input.read();
if (data) {
resultHash.update(data);
} else {
resolve(resultHash.digest('hex'));
}
});
});
}
export default async function build(
nodeVersion: string,
targetArch: string,
@ -177,9 +197,16 @@ export default async function build(
await gitClone(nodeVersion);
await gitResetHard(nodeVersion);
await applyPatches(nodeVersion);
const output = await compile(nodeVersion, targetArch);
const outputHash = await hash(output);
await fs.mkdirp(path.dirname(local));
await fs.copy(output, local);
await fs.promises.writeFile(
`${local}.sha256sum`,
`${outputHash} ${path.basename(local)}
`
);
await fs.remove(buildPath);
}