UNPKG

@sap/cds-dk

Version:

Command line client and development toolkit for the SAP Cloud Application Programming Model

42 lines (35 loc) 1.22 kB
const cds = require('../../lib/cds') const { read, stat } = cds.utils const crypto = require('crypto') const { find } = require('../util/fs') /** * Calculate a checksum for a directory, excluding specified patterns. * @param {string} dirPath - The directory path to calculate checksum for. * @param {(file: string) => boolean} [filter] - the filter function to apply to the found files * @param {string} [hashFunction='sha256'] - The hash function to use (default is 'sha256'). * @returns {Promise<string>} - The checksum as a hex string. */ async function calcDirChecksum(dirPath, filter = [], hashFunction = 'sha256', optionalFiles = []) { const hash = crypto.createHash(hashFunction) let stats try { stats = await stat(dirPath) } catch { return hash.digest('hex') } if (stats.isFile()) { hash.update(await read(dirPath, "binary")) return hash.digest('hex') } const files = [...(await find(dirPath, { filter, ignoreSymlinks: true })), ...optionalFiles].sort() for (const file of files) { try { hash.update(file) hash.update(await read(file, "binary")) } catch { // ignore } } return hash.digest('hex') } module.exports = { calcDirChecksum }