-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
135 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.idea/ | ||
.vscode/ | ||
.cache-loader/ | ||
node_modules/ | ||
build | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
|
||
const crypto = require('crypto'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
const getSources = (paths = [], sourceMethod = function() {}, exclude = []) => { | ||
const getSource = (somePath = '') => { | ||
try { | ||
if (fs.lstatSync(somePath).isDirectory()) { | ||
if ( | ||
exclude.includes(somePath) || | ||
exclude.find(excluded => somePath.startsWith(excluded)) | ||
) { | ||
return somePath; | ||
} | ||
const fileList = fs.readdirSync(somePath); | ||
return getSources( | ||
fileList.map(file => path.join(somePath, file)), | ||
sourceMethod | ||
); | ||
} else { | ||
return sourceMethod(somePath); | ||
} | ||
} catch (ignored) { | ||
return somePath; | ||
} | ||
}; | ||
let result = ''; | ||
for (let i = paths.length - 1; i >= 0; i--) { | ||
result += getSource(paths[i]); | ||
} | ||
return result; | ||
}; | ||
|
||
const getSourceMethod = key => { | ||
const cases = { | ||
content: filePath => fs.readFileSync(filePath, 'utf-8'), | ||
mtime: filePath => `${filePath}_${fs.statSync(filePath).mtime}`, | ||
}; | ||
|
||
const found = cases[key]; | ||
|
||
if (found) { | ||
return found; | ||
} | ||
|
||
console.error( | ||
'Error from createHashFromPaths:\n\n', | ||
'Source method is not recognized, possible values are:\n\n', | ||
Object.keys(cases).map(e => '`' + e + '`').join(', '), | ||
'\n' | ||
); | ||
process.exit(1); | ||
}; | ||
|
||
const createHashFromPaths = ({ paths, exclude, method = 'mtime' }) => { | ||
const hash = crypto.createHash('md5'); | ||
hash.update(paths.join('')); | ||
if (Array.isArray(paths)) { | ||
const sourceMethod = getSourceMethod(method); | ||
const sources = getSources(paths, sourceMethod, exclude); | ||
hash.update(sources); | ||
} | ||
|
||
return hash.digest('hex'); | ||
}; | ||
|
||
module.exports = createHashFromPaths; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters