Spaces:
Runtime error
Runtime error
File size: 1,676 Bytes
db5855f |
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 |
// @ts-check
import crypto from 'node:crypto';
import fs from 'node:fs';
import { join, resolve } from 'node:path';
const algorithm = 'sha256';
export const checksumFileName = `checksum.${algorithm}`;
const createSHAHash = () => crypto.createHash(algorithm);
/**
* @param {string} distPath
* @returns {Promise<void>}
*/
export async function createBuildChecksumFile(distPath) {
const paths = await getFilesInDirectory(distPath);
const fileChecksums = await Promise.all(paths.map(async (path) => await getFileChecksum(path)));
const buildChecksum = createSHAHash().update(fileChecksums.join('')).digest('hex');
await fs.promises.writeFile(join(distPath, checksumFileName), buildChecksum, { flag: 'w' });
}
/**
* @param {string} filePath
* @returns {Promise<string>}
*/
async function getFileChecksum(filePath) {
return new Promise((resolve, reject) => {
const hash = createSHAHash();
const stream = fs.createReadStream(filePath);
stream.on('error', (err) => reject(err));
stream.on('data', (chunk) => hash.update(chunk));
stream.on('end', () => resolve(hash.digest('hex')));
});
}
/**
* @param {string} directoryPath
* @returns {Promise<string[]>}
*/
async function getFilesInDirectory(directoryPath) {
const entries = await fs.promises.readdir(directoryPath, { withFileTypes: true });
const entriesPromises = entries.map(async (entry) => {
const entryPath = resolve(directoryPath, entry.name);
if (entry.isDirectory()) {
return await getFilesInDirectory(entryPath);
}
return entryPath;
});
return (await Promise.all(entriesPromises)).flat(20);
}
|