From 8f00caaa3ea215637529515d85c1216c5b3e1530 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 12 Jan 2018 15:30:15 +0000 Subject: [PATCH] feat: commit lint --- .travis.yml | 1 + README.md | 1 + cmds/commitlint.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 +++++- 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 cmds/commitlint.js diff --git a/.travis.yml b/.travis.yml index bdfc959d7..95641b74b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ before_script: script: - yarn lint - yarn test + - yarn commitlint - yarn coverage -- -u -p codecov coveralls addons: diff --git a/README.md b/README.md index f817d9838..937eaadb9 100644 --- a/README.md +++ b/README.md @@ -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", diff --git a/cmds/commitlint.js b/cmds/commitlint.js new file mode 100644 index 000000000..5fadef8e5 --- /dev/null +++ b/cmds/commitlint.js @@ -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) + }) diff --git a/package.json b/package.json index c5a364b02..89aa34417 100644 --- a/package.json +++ b/package.json @@ -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", @@ -29,6 +30,10 @@ "author": "Friedel Ziegelmayer ", "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",