diff --git a/lib/version.js b/lib/version.js index 4439f679b3b89..9c40646bf01b5 100644 --- a/lib/version.js +++ b/lib/version.js @@ -97,6 +97,8 @@ function persistVersion (newVersion, silent, data, localData, cb_) { if (!npm.config.get('allow-same-version') && data.version === newVersion) { return cb_(new Error('Version not changed, might want --allow-same-version')) } + var lifecycleDataPrevious = Object.assign({}, data) + lifecycleDataPrevious._id = data.name + '@' + data.version data.version = newVersion var lifecycleData = Object.create(data) lifecycleData._id = data.name + '@' + newVersion @@ -104,7 +106,7 @@ function persistVersion (newVersion, silent, data, localData, cb_) { var where = npm.prefix chain([ !localData.hasGit && [checkGit, localData], - [lifecycle, lifecycleData, 'preversion', where], + [lifecycle, lifecycleDataPrevious, 'preversion', where], [updatePackage, newVersion, silent], [lifecycle, lifecycleData, 'version', where], [commit, localData, newVersion], diff --git a/test/tap/version-lifecycle.js b/test/tap/version-lifecycle.js index e7a7793b44507..a586b19ab1cb7 100644 --- a/test/tap/version-lifecycle.js +++ b/test/tap/version-lifecycle.js @@ -124,20 +124,27 @@ test('npm version execution order', function (t) { t.ifError(err, 'version command complete') t.equal('0.0.0', readPackage('preversion').version, 'preversion') + t.equal('0.0.0', readNpmPackageVersion('preversion'), 'preversion') t.deepEqual(readStatus('preversion', t), { - 'preversion-package.json': 'A' + 'preversion-package.json': 'A', + 'preversion_npm_package_version.txt': 'A' }) t.equal('0.0.1', readPackage('version').version, 'version') + t.equal('0.0.1', readNpmPackageVersion('version'), 'version') t.deepEqual(readStatus('version', t), { 'package.json': 'M', 'preversion-package.json': 'A', - 'version-package.json': 'A' + 'preversion_npm_package_version.txt': 'A', + 'version-package.json': 'A', + 'version_npm_package_version.txt': 'A' }) t.equal('0.0.1', readPackage('postversion').version, 'postversion') + t.equal('0.0.1', readNpmPackageVersion('postversion'), 'postversion') t.deepEqual(readStatus('postversion', t), { - 'postversion-package.json': 'A' + 'postversion-package.json': 'A', + 'postversion_npm_package_version.txt': 'A' }) t.end() }) @@ -162,23 +169,26 @@ function setup () { function makeScript (lifecycle) { function contents (lifecycle) { var fs = require('fs') - var exec = require('child_process').exec - fs.createReadStream('package.json') - .pipe(fs.createWriteStream(lifecycle + '-package.json')) - .on('close', function () { - exec( - 'git add ' + lifecycle + '-package.json', - function () { - exec( - 'git status --porcelain', - function (err, stdout) { - if (err) throw err - fs.writeFileSync(lifecycle + '-git.txt', stdout) - } - ) - } - ) - }) + fs.writeFile(lifecycle + '_npm_package_version.txt', process.env.npm_package_version, function (err) { + if (err) throw err + var exec = require('child_process').exec + fs.createReadStream('package.json') + .pipe(fs.createWriteStream(lifecycle + '-package.json')) + .on('close', function () { + exec( + 'git add ' + lifecycle + '-package.json ' + lifecycle + '_npm_package_version.txt', + function () { + exec( + 'git status --porcelain', + function (err, stdout) { + if (err) throw err + fs.writeFileSync(lifecycle + '-git.txt', stdout) + } + ) + } + ) + }) + }) } var scriptPath = path.join(pkg, lifecycle + '.js') fs.writeFileSync( @@ -191,6 +201,10 @@ function readPackage (lifecycle) { return JSON.parse(fs.readFileSync(path.join(pkg, lifecycle + '-package.json'), 'utf-8')) } +function readNpmPackageVersion (lifecycle) { + return fs.readFileSync(path.join(pkg, lifecycle + '_npm_package_version.txt'), 'utf-8') +} + function readStatus (lifecycle, t) { var status = {} fs.readFileSync(path.join(pkg, lifecycle + '-git.txt'), 'utf-8')