diff --git a/package.json b/package.json index baf47aa..bec1af7 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "prettier": "^1.7.2", "rollup": "^0.50.0", "rxjs": "^5.0.2", + "shelljs": "^0.8.3", "ts-jest": "^22.4.1", "typescript": "^2.9.0" }, @@ -83,4 +84,4 @@ "/node_modules/" ] } -} \ No newline at end of file +} diff --git a/publish.js b/publish.js index 42ccad9..ccc6700 100755 --- a/publish.js +++ b/publish.js @@ -1,39 +1,86 @@ -#!/usr/bin/nscript -/* To run this script, nscript is needed: [sudo] npm install -g nscript +#!/usr/bin/env node /* Publish.js, publish a new version of the npm package as found in the current directory */ /* Run this file from the root of the repository */ -module.exports = function(shell, npm, git) { - var pkg = JSON.parse(shell.read("package.json")) + +const shell = require("shelljs") +const fs = require("fs") +const readline = require("readline") + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}) + +function run(command, options) { + const continueOnErrors = options && options.continueOnErrors + const ret = shell.exec(command, options) + if (!continueOnErrors && ret.code !== 0) { + shell.exit(1) + } + return ret +} + +function exit(code, msg) { + console.error(msg) + shell.exit(code) +} + +async function prompt(question, defaultValue) { + return new Promise(resolve => { + rl.question(`${question} [${defaultValue}]: `, answer => { + answer = answer && answer.trim() + resolve(answer ? answer : defaultValue) + }) + }) +} + +async function main() { + const pkg = JSON.parse(fs.readFileSync("package.json", "utf8")) // Bump version number - var nrs = pkg.version.split(".") + let nrs = pkg.version.split(".") nrs[2] = 1 + parseInt(nrs[2], 10) - var version = (pkg.version = shell.prompt( + const version = (pkg.version = await prompt( "Please specify the new package version of '" + pkg.name + "' (Ctrl^C to abort)", nrs.join(".") )) - if (!version.match(/^\d+\.\d+\.\d+$/)) shell.exit(1, "Invalid semantic version: " + version) + if (!version.match(/^\d+\.\d+\.\d+$/)) { + exit(1, "Invalid semantic version: " + version) + } - // Check registery data - if (npm.silent().test("info", pkg.name)) { + // Check registry data + const npmInfoRet = run(`npm info ${pkg.name} --json`, { + continueOnErrors: true, + silent: true + }) + if (npmInfoRet.code === 0) { //package is registered in npm? - var publishedPackageInfo = JSON.parse(npm.get("info", pkg.name, "--json")) + var publishedPackageInfo = JSON.parse(npmInfoRet.stdout) if ( publishedPackageInfo.versions == version || - publishedPackageInfo.versions.indexOf(version) != -1 - ) - shell.exit(2, "Version " + pkg.version + " is already published to npm") + publishedPackageInfo.versions.includes(version) + ) { + exit(2, "Version " + pkg.version + " is already published to npm") + } - shell.write("package.json", JSON.stringify(pkg, null, 2)) + fs.writeFileSync("package.json", JSON.stringify(pkg, null, 2), "utf8") + // build + run("npm run prepublishOnly") // Finally, commit and publish! - npm("run", "prepublishOnly") - npm("publish") - git("commit", "-am", "Published version " + version) - git("tag", version) + run("npm publish") + run(`git commit -am "Published version ${version}"`) + run(`git tag ${version}`) - git("push") - git("push", "--tags") + run("git push") + run("git push --tags") console.log("Published!") - } else shell.exit(1, pkg.name + " is not an existing npm package") + exit(0) + } else { + exit(1, pkg.name + " is not an existing npm package") + } } + +main().catch(e => { + throw e +}) diff --git a/yarn.lock b/yarn.lock index 1e4c717..2127fdb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5138,6 +5138,15 @@ shelljs@^0.7.5: interpret "^1.0.0" rechoir "^0.6.2" +shelljs@^0.8.3: + version "0.8.3" + resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.8.3.tgz#a7f3319520ebf09ee81275b2368adb286659b097" + integrity sha512-fc0BKlAWiLpwZljmOvAOTE/gXawtCoNrP5oaY7KIaQbbyHeQVg01pSEuEGvGh3HEdBU4baCD7wQBwADmM/7f7A== + dependencies: + glob "^7.0.0" + interpret "^1.0.0" + rechoir "^0.6.2" + shellwords@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"