diff options
author | James Talmage <james@talmage.io> | 2015-06-25 05:20:29 +0300 |
---|---|---|
committer | Rebecca Turner <me@re-becca.org> | 2015-07-01 12:41:34 +0300 |
commit | 21ce8fe7dfda78b5522963dc2caff080853da5e7 (patch) | |
tree | 09d59f60cdafde88de165516ba44c7b845c383be /lib | |
parent | 311db70fa3f0a6afd89ce24e5b02ff86dc1c8bb8 (diff) |
version: allow scripts to add files to the commit
Closes #8620
(cherry picked from commit a2106bf)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/version.js | 58 |
1 files changed, 38 insertions, 20 deletions
diff --git a/lib/version.js b/lib/version.js index c93e3b850..b33392488 100644 --- a/lib/version.js +++ b/lib/version.js @@ -52,34 +52,52 @@ function version (args, silent, cb_) { data.version = newVersion var lifecycleData = Object.create(data) lifecycleData._id = data.name + '@' + newVersion + var localData = {} var where = npm.prefix chain([ - [lifecycle, lifecycleData, 'preversion', where], - [version_, data, silent], - [lifecycle, lifecycleData, 'version', where], - [lifecycle, lifecycleData, 'postversion', where] ], - cb_) + [checkGit, localData], + [lifecycle, lifecycleData, 'preversion', where], + [updatePackage, newVersion, silent], + [lifecycle, lifecycleData, 'version', where], + [commit, localData, newVersion], + [lifecycle, lifecycleData, 'postversion', where] ], + cb_) }) } -function version_ (data, silent, cb_) { +function readPackage (cb) { + var packagePath = path.join(npm.localPrefix, 'package.json') + fs.readFile(packagePath, function (er, data) { + if (er) return cb(new Error(er)) + if (data) data = data.toString() + try { + data = JSON.parse(data) + } catch (e) { + er = e + data = null + } + cb(er, data) + }) +} + +function updatePackage (newVersion, silent, cb_) { function cb (er) { - if (!er && !silent) console.log('v' + data.version) + if (!er && !silent) console.log('v' + newVersion) cb_(er) } - checkGit(function (er, hasGit) { + readPackage(function (er, data) { if (er) return cb(new Error(er)) + data.version = newVersion + write(data, 'package.json', cb) + }) +} - write(data, 'package.json', function (er) { - if (er) return cb(new Error(er)) - - updateShrinkwrap(data.version, function (er, hasShrinkwrap) { - if (er || !hasGit) return cb(er) - commit(data.version, hasShrinkwrap, cb) - }) - }) +function commit (localData, newVersion, cb) { + updateShrinkwrap(newVersion, function (er, hasShrinkwrap) { + if (er || !localData.hasGit) return cb(er) + _commit(newVersion, hasShrinkwrap, cb) }) } @@ -121,7 +139,7 @@ function dump (data, cb) { cb() } -function checkGit (cb) { +function checkGit (localData, cb) { fs.stat(path.join(npm.localPrefix, '.git'), function (er, s) { var doGit = !er && s.isDirectory() && npm.config.get('git-tag-version') if (!doGit) { @@ -149,19 +167,19 @@ function checkGit (cb) { }).map(function (line) { return line.trim() }) - if (lines.length) { + if (lines.length && !npm.config.get('force')) { return cb(new Error( 'Git working directory not clean.\n' + lines.join('\n') )) } - + localData.hasGit = true cb(null, true) } ) }) } -function commit (version, hasShrinkwrap, cb) { +function _commit (version, hasShrinkwrap, cb) { var options = { env: process.env } var message = npm.config.get('message').replace(/%s/g, version) var sign = npm.config.get('sign-git-tag') |