From e88d13ca7ee971451b57d056f747215f37dfd3d7 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Thu, 5 Jan 2017 01:16:37 +0100 Subject: [PATCH] CLI: Additional workarounds for on demand CLI dependencies, see #618 --- .gitignore | 1 + .npmignore | 1 + cli/package.json | 1 + cli/pbjs.js | 7 +------ cli/pbts.js | 8 +------- cli/util.js | 39 +++++++++++++++++++++++---------------- package.json | 3 ++- 7 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 cli/package.json diff --git a/.gitignore b/.gitignore index 5c2b97ebb..5724830ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.log npm-debug.* node_modules/ +cli/node_modules/ docs/ coverage/ sandbox/ diff --git a/.npmignore b/.npmignore index 38955e8f0..189548a12 100644 --- a/.npmignore +++ b/.npmignore @@ -3,6 +3,7 @@ *.log npm-debug.* node_modules/ +cli/node_modules/ docs/ coverage/ sandbox/ diff --git a/cli/package.json b/cli/package.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/cli/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/cli/pbjs.js b/cli/pbjs.js index 52cefba49..142e3efd2 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -4,12 +4,7 @@ var path = require("path"), pkg = require(path.join(__dirname, "..", "package.json")), util = require("./util"); -util.setup([ - "minimist", - "chalk", - "glob", - "uglify-js" -], pkg.devDependencies); +util.setup(); var protobuf = require(".."), minimist = require("minimist"), diff --git a/cli/pbts.js b/cli/pbts.js index 7c2bf5107..41c0fc0aa 100644 --- a/cli/pbts.js +++ b/cli/pbts.js @@ -5,13 +5,7 @@ var child_process = require("child_process"), pkg = require(path.join(__dirname, "..", "package.json")), util = require("./util"); -util.setup([ - "minimist", - "chalk", - "glob", - "tmp", - "jsdoc" -], pkg.devDependencies); +util.setup(); var minimist = require("minimist"), chalk = require("chalk"), diff --git a/cli/util.js b/cli/util.js index e6002a8f6..aa8d0b271 100644 --- a/cli/util.js +++ b/cli/util.js @@ -1,6 +1,8 @@ +"use strict"; var fs = require("fs"), path = require("path"), - child_process = require("child_process"); + child_process = require("child_process"), + Module = require("module"); var protobuf = require(".."); @@ -69,25 +71,30 @@ exports.inspect = function inspect(object, indent) { return sb.join("\n"); }; -exports.setup = function(modules, versions) { - for (var i = 0; i < modules.length;) { +exports.setup = function() { + // this one is important. without it, this folder won't be searched anymore. + try { fs.mkdirSync(path.join(__dirname, "node_modules")); } catch (e) {} + + // find out which modules are missing + var pkg = require(path.join(__dirname, "..", "package.json")); + var install = []; + pkg.cliDependencies.forEach(function(name) { try { - // do not feed the cache - require.resolve(path.join(modules[i], "package.json")); - modules.splice(i, 1); + require.resolve(name + "/package.json"); // jsdoc has no main file } catch (e) { - ++i; + var version = pkg.dependencies[name] || pkg.devDependencies[name]; + install.push(version ? name + "@" + version : name); } - } - 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, ".."), + if (!install.length) { + try { fs.rmdirSync(path.join(__dirname, "node_modules")); } catch (e) {} + return; + } + + // if any are missing, install them. this relies on an empty package.json in cli/. + process.stderr.write("installing CLI dependencies: " + install.join(", ") + "\n"); + child_process.execSync("npm --silent install " + install.join(" "), { + cwd: __dirname, stdio: "ignore" }); }; diff --git a/package.json b/package.json index fefc68bf3..7d0e2a359 100644 --- a/package.json +++ b/package.json @@ -88,5 +88,6 @@ "vinyl-source-stream": "^1.1.0", "zuul": "^3.11.1", "zuul-ngrok": "^4.0.0" - } + }, + "cliDependencies": [ "chalk", "glob", "jsdoc", "minimist", "tmp", "uglify-js" ] }