Created
June 1, 2017 07:48
-
-
Save arijusg/2dd0dbec7349e685359cc7c2207bedd0 to your computer and use it in GitHub Desktop.
Pre commit hook - the right way
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
{ | |
"name": "app", | |
"version": "0.0.0", | |
"license": "MIT", | |
"scripts": { | |
"ng": "ng", | |
"start": "ng serve", | |
"build": "ng build", | |
"build-prod": "rm -rf dist && ng build --prod --output-hashing none --vendor-chunk true --extract-css false", | |
"test": "ng test --single-run", | |
"lint": "ng lint", | |
"e2e": "ng e2e", | |
"pre-commit": "ts-node scripts/precommit.ts" | |
}, | |
"private": true, | |
"dependencies": { }, | |
"devDependencies": { | |
"pre-commit": "^1.2.2" | |
}, | |
"pre-commit": [ | |
"pre-commit" | |
] | |
} |
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
import { spawn } from 'child_process'; | |
class PrecommitHook { | |
private async stashNotStagedFiles(): Promise<void> { | |
return new Promise<void>((resolve) => { | |
console.log("***-------------------- stashing not staged files --------------------***"); | |
let s = spawn('git', ['stash', '-u', '--keep-index'], { stdio: 'inherit' }); | |
s.on('close', (code) => { | |
resolve(); | |
}); | |
}); | |
} | |
private tsLint() { | |
return new Promise<void>((resolve) => { | |
console.log("***-------------------- lint --------------------***"); | |
let s = spawn('npm', ['run', 'lint'], { stdio: 'inherit' }); | |
s.on('close', (code) => { | |
resolve(); | |
}); | |
}); | |
} | |
private gitResetHard() { | |
return new Promise<void>((resolve) => { | |
console.log("***-------------------- git hard reset --------------------***"); | |
let s = spawn('git', ['reset', '--hard'], { stdio: 'inherit' }); | |
s.on('close', (code) => { | |
resolve(); | |
}); | |
}); | |
} | |
private runTests() { | |
return new Promise<void>((resolve) => { | |
console.log("***-------------------- test --------------------***"); | |
let s = spawn('npm', ['run', 'test'], { stdio: 'inherit' }); | |
s.on('close', (code) => { | |
resolve(); | |
}); | |
}); | |
} | |
private popNotStagesFiles() { | |
return new Promise<void>((resolve) => { | |
console.log("***-------------------- pop not staged files --------------------***"); | |
let s = spawn('git', ['stash', 'pop', '-q'], { stdio: 'inherit' }); | |
s.on('close', (code) => { | |
resolve(); | |
}); | |
}); | |
} | |
public async run() { | |
try { | |
await this.stashNotStagedFiles(); | |
await this.tsLint(); | |
await this.runTests(); | |
} | |
finally { | |
await this.gitResetHard(); | |
await this.popNotStagesFiles(); | |
} | |
} | |
} | |
const hook = new PrecommitHook(); | |
hook.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment