Skip to content

Instantly share code, notes, and snippets.

@heineiuo
Created August 29, 2018 05:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heineiuo/89acb69854ed87871e73c7b4377a4c3d to your computer and use it in GitHub Desktop.
Save heineiuo/89acb69854ed87871e73c7b4377a4c3d to your computer and use it in GitHub Desktop.
use standardjs with pre-commit hook
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()
}
})
@heineiuo
Copy link
Author

配合husky和package.json使用:

package.json

  "husky": {
    "skipCI": true,
    "hooks": {
      "pre-commit": "yarn lint"
    }
  },
  "standard": {
    "globals": [
      "fetch",
    ],
    "ignore": [
      "node_modules",
      "__tests__",
      "tests"
    ],
    "fix": true,
    "parser": "babel-eslint"
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment