From 9191260cb0dc76ab95ddfc8f46dcf10c83202a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6nnemann?= Date: Mon, 2 Feb 2015 15:26:02 +0100 Subject: [PATCH] feat(pre): make hook "prepublish" compatible --- bin/semantic-release | 25 ++++++++++++++++++------- src/restart.js | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 src/restart.js diff --git a/bin/semantic-release b/bin/semantic-release index c772a9e6..13bc9c90 100755 --- a/bin/semantic-release +++ b/bin/semantic-release @@ -12,14 +12,18 @@ var argv = require('minimist')(process.argv.slice(2), { } }) +var npmArgv = process.env.npm_config_argv +npmArgv = npmArgv ? JSON.parse(npmArgv).cooked : [] + if (~argv._.indexOf('pre')) { - // The prepublish script is also executed when the package's dependencies are installed - // In this case we abort the command and do nothing. - var npmArgv = process.env.npm_config_argv - if (npmArgv) { - npmArgv = JSON.parse(npmArgv) - if (~npmArgv.cooked.indexOf('install')) process.exit(0) - } + var publish = false + + // see src/restart.js + if (~npmArgv.indexOf('--semantic-release-rerun')) process.exit(0) + // the `prepublish` hook is also executed when the package is installed + // in this case we abort the command and do nothing. + if (~npmArgv.indexOf('install')) process.exit(0) + if (~npmArgv.indexOf('publish')) publish = true console.log('Determining new version') @@ -35,6 +39,7 @@ if (~argv._.indexOf('pre')) { } console.log('Publishing v' + result) + if (publish) require('../src/restart')() }) } @@ -44,5 +49,11 @@ if (~argv._.indexOf('post')) { console.log('Something went wrong.') throw err } + + // see src/restart.js + if (~npmArgv.indexOf('--semantic-release-rerun')) { + console.log('Everything is alright :) npm will now print an error message that you can safely ignore.') + process.exit(1) + } }) } diff --git a/src/restart.js b/src/restart.js new file mode 100644 index 00000000..bb746f25 --- /dev/null +++ b/src/restart.js @@ -0,0 +1,16 @@ +'use strict' + +var spawn = require('child_process').spawn + +module.exports = function () { + // npm loads package.json data before running the `prepublish` hook + // changing the version on `prepublish` has no effect + // see https://github.com/npm/npm/issues/7118 + // to circumvent this behavior we are calling `npm publish` inside `prepublish` + // the package.json is then loaded again and the correct version will be published + + var child = spawn('npm', ['publish', '--semantic-release-rerun']) + + child.stdout.pipe(process.stdout) + child.stderr.pipe(process.stderr) +}