From 44f6357557ab3d881310024342bcc1e0d336a20c Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Wed, 4 Jan 2017 22:24:49 +0100 Subject: [PATCH] CLI: Revised automatic setup of cli dependencies, see #618 --- .npmignore | 1 + cli/pbjs.js | 16 ++++++++++++---- cli/pbts.js | 25 +++++++++++++++---------- cli/targets/static.js | 5 ++--- cli/util.js | 38 ++++++++++++++++++++------------------ 5 files changed, 50 insertions(+), 35 deletions(-) diff --git a/.npmignore b/.npmignore index e6c50e0f4..38955e8f0 100644 --- a/.npmignore +++ b/.npmignore @@ -17,3 +17,4 @@ src/util/inquire/ src/util/path/ src/util/pool/ src/util/utf8/ +pbjs.png diff --git a/cli/pbjs.js b/cli/pbjs.js index c5ad4c342..52cefba49 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -1,14 +1,22 @@ +"use strict"; var path = require("path"), fs = require("fs"), pkg = require(path.join(__dirname, "..", "package.json")), util = require("./util"); -var minimist = util.require("minimist", pkg.devDependencies.minimist), - chalk = util.require("chalk", pkg.devDependencies.chalk), - glob = util.require("glob", pkg.devDependencies.glob); +util.setup([ + "minimist", + "chalk", + "glob", + "uglify-js" +], pkg.devDependencies); var protobuf = require(".."), - targets = util.requireAll("./targets"); + minimist = require("minimist"), + chalk = require("chalk"), + glob = require("glob"); + +var targets = util.requireAll("./targets"); /** * Runs pbjs programmatically. diff --git a/cli/pbts.js b/cli/pbts.js index a29074126..7c2bf5107 100644 --- a/cli/pbts.js +++ b/cli/pbts.js @@ -1,17 +1,22 @@ -var path = require("path"), +"use strict"; +var child_process = require("child_process"), + path = require("path"), fs = require("fs"), pkg = require(path.join(__dirname, "..", "package.json")), util = require("./util"); -var child_process = require("child_process"); -var minimist = util.require("minimist", pkg.devDependencies.minimist), - chalk = util.require("chalk", pkg.devDependencies.chalk), - glob = util.require("glob", pkg.devDependencies.glob), - tmp = util.require("tmp", pkg.devDependencies.tmp); - -var jsdoc = util.require("jsdoc/package.json", pkg.devDependencies.jsdoc); - -var protobuf = require(".."); +util.setup([ + "minimist", + "chalk", + "glob", + "tmp", + "jsdoc" +], pkg.devDependencies); + +var minimist = require("minimist"), + chalk = require("chalk"), + glob = require("glob"), + tmp = require("tmp"); /** * Runs pbts programmatically. diff --git a/cli/targets/static.js b/cli/targets/static.js index 7a4447130..313e31270 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.js @@ -2,9 +2,8 @@ module.exports = static_target; var protobuf = require("../.."), - cliUtil = require("../util"); - -var UglifyJS = cliUtil.require("uglify-js", require(require("path").join(__dirname, "..", "..", "package.json")).devDependencies["uglify-js"]); + cliUtil = require("../util"), + UglifyJS = require("uglify-js"); var Type = protobuf.Type, Service = protobuf.Service, diff --git a/cli/util.js b/cli/util.js index aeee7fe80..e6002a8f6 100644 --- a/cli/util.js +++ b/cli/util.js @@ -69,25 +69,27 @@ exports.inspect = function inspect(object, indent) { return sb.join("\n"); }; -exports.require = function(name, version) { - var sub = ""; - var p = name.indexOf("/"); - if (p > -1) { - sub = name.substring(p); - name = name.substring(0, p); +exports.setup = function(modules, versions) { + for (var i = 0; i < modules.length;) { + try { + // do not feed the cache + require.resolve(path.join(modules[i], "package.json")); + modules.splice(i, 1); + } catch (e) { + ++i; + } } - var cwd = path.join(__dirname, ".."); - var dir = path.join(cwd, "node_modules", name); - try { - // do not feed the cache - require.resolve(path.join(dir, "package.json")); - } catch (e) { - console.error("installing " + name + "@" + version + " ..."); - child_process.execSync("npm --silent install " + name + "@" + version, { - cwd: cwd - }); - } - return require(name + sub); + if (!modules.length) + return; + modules = modules.map(function(name) { + return name + "@" + versions[name]; + }); + var cmd = "npm --silent --only=prod install " + modules.join(" "); + process.stderr.write("setting up " + modules.join(", ") + " ...\n"); + child_process.execSync(cmd, { + cwd: path.join(__dirname, ".."), + stdio: "ignore" + }); }; exports.wrap = function(OUTPUT, options) {