Skip to content

Commit

Permalink
CLI: Additional workarounds for on demand CLI dependencies, see #618
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 5, 2017
1 parent 44f6357 commit e88d13c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.log
npm-debug.*
node_modules/
cli/node_modules/
docs/
coverage/
sandbox/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.log
npm-debug.*
node_modules/
cli/node_modules/
docs/
coverage/
sandbox/
Expand Down
1 change: 1 addition & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 1 addition & 6 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
8 changes: 1 addition & 7 deletions cli/pbts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
39 changes: 23 additions & 16 deletions cli/util.js
Original file line number Diff line number Diff line change
@@ -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("..");

Expand Down Expand Up @@ -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"
});
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
}

0 comments on commit e88d13c

Please sign in to comment.