Created
August 29, 2018 05:20
-
-
Save heineiuo/89acb69854ed87871e73c7b4377a4c3d to your computer and use it in GitHub Desktop.
use standardjs with pre-commit hook
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
const { exec } = require('shelljs') | |
const path = require('path') | |
const exit = () => { | |
console.log('JavaScript Standard Style errors were detected. Aborting commit.') | |
process.exit(1) | |
} | |
const gitdiff = exec(`git diff --name-only --cached --relative`) | |
if (gitdiff.stderr) { | |
exit() | |
} | |
const filtered = gitdiff.stdout.split(/\r?\n/).filter(item => { | |
if (item.length === 0) return false | |
return !!item.match(/^src.*\.jsx?$/) | |
}) | |
filtered.forEach(item => { | |
const standard = path.resolve(process.cwd(), './node_modules/.bin/standard') | |
const filepath = path.resolve(process.cwd(), item) | |
const result = exec(`${standard} ${filepath}`) | |
if (result.stderr) { | |
exit() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
配合husky和package.json使用:
package.json