Skip to content

Commit

Permalink
[+] Technical - Add lint pre-commit-hook (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
slimee authored Mar 18, 2020
1 parent 207c7c6 commit 21778cf
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .eslint-bin/pre-commit-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const simpleGit = require('simple-git')(`${__dirname}/..`);
let listFilesModified = [];

function excludeNonCommitedFiles(file) {
return file.index !== 'D' // NOTICE: Deleted files
&& file.index !== ' ' // NOTICE: Files not staged for commit
&& file.index !== '?'; // NOTICE: Untracked files
}

function getFilesModified(callback) {
simpleGit.status((error, status) => {
if (error) {
console.error(error);
process.exit(-1);
}

listFilesModified = status.files
.filter(excludeNonCommitedFiles)
.map(file => {
if (file.index === 'R') {
return file.path.substring(file.path.indexOf(' -> ') + 4);
}
return file.path;
})
.filter(file => file.endsWith('.js'));

callback();
});
}

function runEslint(callback) {
if (listFilesModified.length === 0) {
return callback(0);
}

console.log(`[ESLint] Validating changed files:\n${listFilesModified.join('\n')}`);
const eslintPath = `${__dirname}/../node_modules/.bin/eslint`;
const spawn = require('child_process').spawn;
const cmd = spawn(eslintPath, listFilesModified, { stdio: 'inherit', shell: true });

cmd.on('exit',code => callback(code));
}

getFilesModified((error) => {
if (error) {
console.error(error);
process.exit(-2);
}

runEslint((code) => process.exit(code));
});
7 changes: 7 additions & 0 deletions .githooks/pre-commit/eslint
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
node .eslint-bin/pre-commit-hook.js

if [[ $? != 0 ]] ; then
echo "🚫 🚫 🚫 ESLint failed, git commit denied!"
exit 1
fi
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Environments - Add environments:update command.
- Authentication - Add Google authentication.
- Technical - Add tests on google authentication.
- Technical - Add lint pre-commit-hook.

### Changed
- Technical - Clean temporary tests files.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"bluebird": "3.5.2",
"chalk": "2.4.1",
"cli-table": "github:Automattic/cli-table#master",
"git-hooks": "1.1.10",
"inquirer": "6.2.0",
"joi": "14.3.1",
"jsonapi-serializer": "3.6.2",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,11 @@ get-value@^2.0.3, get-value@^2.0.6:
resolved "https://registry.yarnpkg.com/get-value/-/get-value-2.0.6.tgz#dc15ca1c672387ca76bd37ac0a395ba2042a2c28"
integrity sha1-3BXKHGcjh8p2vTesCjlbogQqLCg=

[email protected]:
version "1.1.10"
resolved "https://registry.yarnpkg.com/git-hooks/-/git-hooks-1.1.10.tgz#c3113a8506593ac703bffc3bc77badbe8c9e1536"
integrity sha512-23nto1qLsqJdY0daAUYqeKPyTvzefR2wZv1krgFRnPg0FxrmITCeWAIv/QfR+fAD7eZC7Rp5OSicJB4hDKyp1A==

glob-parent@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-3.1.0.tgz#9e6af6299d8d3bd2bd40430832bd113df906c5ae"
Expand Down

0 comments on commit 21778cf

Please sign in to comment.