Skip to content

Commit

Permalink
CLI: Revised automatic setup of cli dependencies, see #618
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 4, 2017
1 parent f2ffa07 commit 44f6357
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ src/util/inquire/
src/util/path/
src/util/pool/
src/util/utf8/
pbjs.png
16 changes: 12 additions & 4 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
25 changes: 15 additions & 10 deletions cli/pbts.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 2 additions & 3 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 20 additions & 18 deletions cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 44f6357

Please sign in to comment.