diff --git a/bin/npm-publish.js b/bin/npm-publish.js index a663564..d42acf5 100755 --- a/bin/npm-publish.js +++ b/bin/npm-publish.js @@ -1,11 +1,37 @@ #!/usr/bin/env node +'use strict'; -var program = require('commander'); -var fluidPublish = require('fluid-publish'); -var path = require('path'); -var fs = require('fs'); -var lodash = require('lodash'); -var execSync = require("child_process").execSync; +let program = require('commander'); +let fluidPublish = require('fluid-publish'); +let path = require('path'); +let fs = require('fs'); +let execSync = require("child_process").execSync; + + +function isTag(tag) { + const tagList = execSync('git tag').toString().split('\n'); + if (tagList.indexOf(tag) !== -1) { + return true; + } + + return false; +} + +function isNpmVersion(packageName, version) { + const npmVersion = execSync(`npm view ${packageName} version`).toString(); + return npmVersion.trim() === version.trim(); +} + +function cleanErrorPublish({ packageName, hasTag, hasVersion, tag, version}) { + if (hasTag) { // Delete tag from GitHub + execSync(`git tag -d ${tag}`); + execSync(`git push origin :refs/tags/${tag}`); + } + + if (hasVersion) { // Delete version from npm + execSync(`npm unpublish ${packageName}@${version}`); + } +} program .version(require('../package.json').version) @@ -25,37 +51,51 @@ program.parse(process.argv); //override default date format and change timestamp of current revision to now time fluidPublish.convertToISO8601 = function(timestamp) { - var date = new Date().toISOString(); + const date = new Date().toISOString(); //remove {':', '-'} symbols from format because npm publish doesn't work return date.split(':').join('').split('-').join(''); }; if (program.release) { - var updatingVersionMessage = 'Updating to a version to '; - var currentPath = process.cwd(); - var packageFilename = path.join(currentPath, 'package.json'); - var version = require(packageFilename).version; + let updatingVersionMessage = 'Updating to a version to '; + let currentPath = process.cwd(); + let packageFilename = path.join(currentPath, 'package.json'); + let currentPackage = require(packageFilename); + let version = currentPackage.version; + let tag = `v${version}`; + let packageName = currentPackage.name; + let isSuccessful = true; - if (!program.test) { - execSync(`node ${path.resolve(__dirname, './update-changelog.js')}`, { stdio: 'inherit' }); - execSync('git push'); + try { + fluidPublish.standard(program.test, { + "pushVCTagCmd": "git push origin v${version}", + "vcTagCmd": "git tag -a v${version} -m \"Tagging the ${version} release\"" + }); + } catch (e) { + isSuccessful = false; + + if (!program.test) { + const hasTag = isTag(tag); + const hasVersion = isNpmVersion(packageName, version); + cleanErrorPublish({ packageName, hasTag, hasVersion, tag, version}); + + process.exit(1); + } } - fluidPublish.standard(program.test, { - "pushVCTagCmd": "git push origin v${version}", - "vcTagCmd": "git tag -a v${version} -m \"Tagging the ${version} release\"" - }); + if (!program.test && isSuccessful) { + execSync(`node ${path.resolve(__dirname, './update-changelog.js')}`, { stdio: 'inherit' }); + execSync('git push'); - if (!program.test) { - var vNumbers = version.split("."); - var lastNumber = parseInt(vNumbers[vNumbers.length - 1], 10); + let vNumbers = version.split("."); + let lastNumber = parseInt(vNumbers[vNumbers.length - 1], 10); vNumbers[vNumbers.length - 1] = lastNumber + 1; - var targetVersion = vNumbers.join('.'); + let targetVersion = vNumbers.join('.'); - var VERSION_REGEXP = new RegExp( + let VERSION_REGEXP = new RegExp( '([\'|\"]?version[\'|\"]?[ ]*:[ ]*[\'|\"]?)[\\d||A-a|.|-]*([\'|\"]?)', 'gi'); - var packageData = fs.readFileSync(packageFilename).toString(); + let packageData = fs.readFileSync(packageFilename).toString(); packageData = packageData.replace(VERSION_REGEXP, "\"version\": \"" + targetVersion + "\""); fs.writeFileSync(packageFilename, packageData);