Skip to content

Commit

Permalink
feat: commit lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Mar 14, 2018
1 parent a67b18a commit 8f00caa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ before_script:
script:
- yarn lint
- yarn test
- yarn commitlint
- yarn coverage -- -u -p codecov coveralls

addons:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Your `package.json` should have the following entries.
"main": "src/index.js",
"scripts": {
"lint": "aegir lint",
"commitlint": "aegir commitlint",
"release": "aegir release",
"build": "aegir build",
"test": "aegir test",
Expand Down
55 changes: 55 additions & 0 deletions cmds/commitlint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

const lint = require('@commitlint/lint')
const read = require('@commitlint/read')
const load = require('@commitlint/load')
const util = require('util')

function exec (command) {
return new Promise((resolve, reject) => {
require('child_process').exec(command, (err, stdout, stderr) => {
if (err) {
return reject(err)
} else {
return resolve(stdout)
}
})
})
}

function runLinter () {
return exec('git rev-parse --abbrev-ref HEAD')
.then((branch) => exec(`git rev-list --left-right --count master...${branch}`))
.then((count) => count.split(/\s+/)[1])
.then((commits) => read({to: 'HEAD', from: `HEAD~${commits}`}))
.then((commits) => Promise.all([commits, load({ extends: ['@commitlint/config-conventional'] })]))
.then(([commits, opts]) => Promise.all(commits.map((commit) => lint(
commit,
opts.rules,
opts.parserPreset ? {parserOpts: opts.parserPreset.parserOpts} : {}
))))
.then(results => results.filter((r) => !r.valid))
.then(invalid => {
if (invalid.length) {
console.log(util.inspect(invalid, false, null))
return 1
} else {
return 0
}
})
.then(process.exit)
.catch((error) => {
console.log(error)
process.exit(1)
})
}

exec('git rev-parse --verify master')
.then(runLinter)
.catch(() => exec('git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*'))
.then(() => exec('git fetch'))
.then(lint)
.catch((error) => {
console.log(error)
process.exit(1)
})
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"watch": "cross-env AEGIR_TEST=hello node cli.js test -t node --watch",
"release": "npm run test && node cli.js release --no-build --no-test",
"release-minor": "npm run test && node cli.js release --no-build --no-test --type minor",
"release-major": "npm run test && node cli.js release --no-build --no-test --type major"
"release-major": "npm run test && node cli.js release --no-build --no-test --type major",
"commitlint": "node cli.js commitlint"
},
"keywords": [
"webpack",
Expand All @@ -29,6 +30,10 @@
"author": "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"license": "MIT",
"dependencies": {
"@commitlint/config-conventional": "^6.1.2",
"@commitlint/lint": "^6.1.2",
"@commitlint/load": "^6.1.2",
"@commitlint/read": "^6.1.2",
"async": "^2.6.0",
"browserify-zlib": "^0.2.0",
"chalk": "^2.3.0",
Expand Down

0 comments on commit 8f00caa

Please sign in to comment.