Skip to content

Commit

Permalink
feat(sys): add sys.generateFileHash() for more efficient file hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Oct 19, 2020
1 parent d584fdb commit d762c6d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/declarations/stencil-public-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,13 @@ export interface CompilerSystem {
*/
fetch?(input: string | any, init?: any): Promise<any>;
/**
* Generates a MD5 digest encoded as HEX
* Generates a sha1 digest encoded as HEX
*/
generateContentHash?(content: string | any, length?: number): Promise<string>;
/**
* Generates a sha1 digest encoded as HEX from a file path
*/
generateFileHash?(filePath: string | any, length?: number): Promise<string>;
/**
* Get the current directory.
*/
Expand Down
16 changes: 15 additions & 1 deletion src/sys/node/node-sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,26 @@ export function createNodeSys(c: { process?: any } = {}) {
},
generateContentHash(content, length) {
let hash = createHash('sha1').update(content).digest('hex').toLowerCase();

if (typeof length === 'number') {
hash = hash.substr(0, length);
}
return Promise.resolve(hash);
},
generateFileHash(filePath, length) {
return new Promise((resolve, reject) => {
const h = createHash('sha1');
fs.createReadStream(filePath)
.on('error', err => reject(err))
.on('data', data => h.update(data))
.on('end', () => {
let hash = h.digest('hex').toLowerCase();
if (typeof length === 'number') {
hash = hash.substr(0, length);
}
resolve(hash);
});
});
},
copy: nodeCopyTasks,
details: {
cpuModel: (Array.isArray(sysCpus) && sysCpus.length > 0 ? sysCpus[0] && sysCpus[0].model : '') || '',
Expand Down

0 comments on commit d762c6d

Please sign in to comment.