Use pre-push hook instead of pre-commit

This commit is contained in:
Bartek Fabiszewski 2020-04-11 21:31:55 +02:00
parent 4a66630a00
commit 569303651e
2 changed files with 9 additions and 29 deletions

View File

@ -17,32 +17,15 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
const fs = require('fs');
const crypto = require('crypto');
const exec = require('child_process').execSync;
const getBundleHash = () => {
const distDir = 'js/dist/';
const hash = crypto.createHash('sha1');
fs.readdirSync(distDir)
.filter(path => path.endsWith('.js'))
.sort()
.forEach(path => {
hash.update(fs.readFileSync(distDir + path));
});
return hash.digest('hex');
const sourcesModified = () => {
const lastBuildCommit = exec('git log -1 --pretty="format:%H" js/dist/*bundle*js*').toString();
const output = exec(`git diff --name-only ${lastBuildCommit} HEAD js/src`).toString();
return !!output && output.split('\n').length > 0;
};
const main = () => {
const bundleHash = getBundleHash();
try {
exec('npm run build');
if (getBundleHash() !== bundleHash) {
exec('git add js/dist/*');
}
} catch (e) {
console.log('Warning: build failed!');
}
};
main();
if (sourcesModified()) {
console.log('\nPlease update and commit distribution bundle first!\nYou may still push using --no-verify option.\n');
process.exitCode = 1;
}

View File

@ -88,10 +88,7 @@
],
"husky": {
"hooks": {
"pre-commit": "lint-staged"
"pre-push": "node .githooks/build-status.js"
}
},
"lint-staged": {
"js/src/**/*.js": "node .githooks/build-staged.js"
}
}