From e445effdcf3d6ea9da26ed679b792722d9a1ac07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6nnemann?= Date: Wed, 22 Apr 2015 17:15:10 +0200 Subject: [PATCH] feat(pre): add option to provide own commit message analyzer --- bin/semantic-release.js | 6 +++++- lib/{type/analyze.js => analyzer.js} | 14 +++++++++++++- lib/commits.js | 27 +++++++++++++++++++++++++++ lib/type/commits.js | 15 --------------- lib/type/index.js | 11 ----------- src/pre.js | 11 ++++++++--- 6 files changed, 53 insertions(+), 31 deletions(-) rename lib/{type/analyze.js => analyzer.js} (51%) create mode 100644 lib/commits.js delete mode 100644 lib/type/commits.js delete mode 100644 lib/type/index.js diff --git a/bin/semantic-release.js b/bin/semantic-release.js index c0d21dd0..ed8c411b 100755 --- a/bin/semantic-release.js +++ b/bin/semantic-release.js @@ -1,6 +1,8 @@ #!/usr/bin/env node 'use strict' +var readFile = require('fs').readFileSync + var abbrev = require('abbrev') var minimist = require('minimist') @@ -21,6 +23,8 @@ var argv = minimist(process.argv.slice(2), { } }) +var plugins = JSON.parse(readFile('./package.json')).release || {} + var npmArgv = process.env.npm_config_argv ? minimist(JSON.parse(process.env.npm_config_argv).cooked) : {_: []} @@ -46,7 +50,7 @@ if (~argv._.indexOf('pre')) { // require a correct setup during publish if (publish && !argv.debug && !require('../src/verify')(argv)) process.exit(1) - require('../src/pre')(argv, efh(function (result) { + require('../src/pre')(argv, plugins, efh(function (result) { if (!result) { console.log('Nothing changed. Not publishing.') process.exit(1) diff --git a/lib/type/analyze.js b/lib/analyzer.js similarity index 51% rename from lib/type/analyze.js rename to lib/analyzer.js index 966bb833..ff6d8f8f 100644 --- a/lib/type/analyze.js +++ b/lib/analyzer.js @@ -1,9 +1,21 @@ 'use strict' +var parseRawCommit = require('conventional-changelog/lib/git').parseRawCommit + module.exports = function (commits) { var type = null - commits.every(function (commit) { + commits + + .map(function (commit) { + return parseRawCommit(commit.hash + '\n' + commit.message) + }) + + .filter(function (commit) { + return !!commit + }) + + .every(function (commit) { if (commit.breaks.length) { type = 'major' return false diff --git a/lib/commits.js b/lib/commits.js new file mode 100644 index 00000000..326ed82e --- /dev/null +++ b/lib/commits.js @@ -0,0 +1,27 @@ +'use strict' + +var exec = require('child_process').exec + +var efh = require('./error').efh + +module.exports = function (from, cb) { + var range = (from ? from + '..' : '') + 'HEAD' + exec( + 'git log -E --format=%H==SPLIT==%s==END== ' + range, + efh(cb)(function (stdout) { + cb(null, String(stdout).split('==END==\n') + + .filter(function (raw) { + return !!raw.trim() + }) + + .map(function (raw) { + var data = raw.split('==SPLIT==') + return { + hash: data[0], + message: data[1] + } + })) + }) + ) +} diff --git a/lib/type/commits.js b/lib/type/commits.js deleted file mode 100644 index c82a8a67..00000000 --- a/lib/type/commits.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict' - -var git = require('conventional-changelog/lib/git') - -var efh = require('../error').efh - -module.exports = function (from, cb) { - git.getCommits({ - log: console.log.bind(console), - warn: console.warn.bind(console), - from: from - }, efh(cb)(function (commits) { - cb(null, commits) - })) -} diff --git a/lib/type/index.js b/lib/type/index.js deleted file mode 100644 index 2b970772..00000000 --- a/lib/type/index.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict' - -var analyze = require('./analyze') -var commits = require('./commits') -var efh = require('../error').efh - -module.exports = function (gitHead, cb) { - commits(gitHead, efh(cb)(function (commits) { - cb(null, analyze(commits)) - })) -} diff --git a/src/pre.js b/src/pre.js index 1a9f3f49..d1d7b8f8 100644 --- a/src/pre.js +++ b/src/pre.js @@ -4,16 +4,21 @@ var fs = require('fs') var semver = require('semver') -var type = require('../lib/type') +var getCommits = require('../lib/commits') var npmInfo = require('../lib/npm-info') var efh = require('../lib/error').efh -module.exports = function (options, cb) { +module.exports = function (options, plugins, cb) { var path = './package.json' var pkg = JSON.parse(fs.readFileSync(path)) + if (!pkg.name) return cb(new Error('Package must have a name')) + npmInfo(pkg.name, efh(cb)(function (res) { - type(res.gitHead, efh(cb)(function (type) { + getCommits(res.gitHead, efh(cb)(function (commits) { + var analyzer = require(plugins.analyzer || '../lib/analyzer') + var type = analyzer(commits) + if (!type) return cb(null, null) pkg.version = !res.version ? '1.0.0' : semver.inc(res.version, type) if (!options.debug) fs.writeFileSync(path, JSON.stringify(pkg, null, 2))