Place the following in .git/hooks/pre-commit
:
#!/bin/sh
node ./pre-commit.js
exit $?
Place the following in pre-commit.js
in the root of your repo:
const { exec } = require('child-process-promise');
const process = require('process');
async function getFilesToLint() {
const filesToLint = await exec('git diff HEAD --name-only').then(({stdout}) => {
const files = stdout.split('\n');
const filesToOmit = ['app.js'];
const filesToLint = files.filter(thisFile => {
return thisFile.endsWith('.js') && !filesToOmit.includes(thisFile);
});
return filesToLint;
});
return filesToLint;
}
async function lintFiles() {
const lintingOutput = [];
const filesToLint = await getFilesToLint();
return exec(`eslint ${filesToLint.join(' ')}`)
.then(({stdout}) => {
return {exitCode: 0, output: stdout};
})
.catch(({stdout}) => {
return {exitCode: 1, output:stdout};
});
}
async function main() {
const results = await lintFiles();
if (results.exitCode === 1 && results.output) {
console.log(results.output);
console.log('You have some linting errors. Please see the output above and fix the errors our ignore them and commit using XXXX');
}
process.exit(results.exitCode);
}
main();