Skip to content

Commit

Permalink
support resolving protobufjs as peer or local dependency everywhere i…
Browse files Browse the repository at this point in the history
…n the cli package
  • Loading branch information
taylorcode committed Aug 7, 2019
1 parent cfd2383 commit 7a30019
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 27 deletions.
11 changes: 6 additions & 5 deletions cli/pbjs.js
@@ -1,13 +1,14 @@
"use strict";
var path = require("path"),
fs = require("fs"),
pkg = require("./package.json"),
util = require("./util");

var protobuf = require(util.pathToProtobufJs),
minimist = require("minimist"),
chalk = require("chalk"),
glob = require("glob");
glob = require("glob"),
pkg = require("./package.json"),
util = require("./util"),
requireProtobufjs = require("./resolve-protobufjs");

var protobuf = requireProtobufjs();

var targets = util.requireAll("./targets");

Expand Down
19 changes: 19 additions & 0 deletions cli/resolve-protobufjs.js
@@ -0,0 +1,19 @@
"use strict";

exports.requireProtobufjs = function requireProtobufjs() {
try {
// local development, i.e. forked from github
require.resolve("..");
return require("..");
} catch (e) {
// installed as a peer dependency
try {
require.resolve("protobufjs");
return require("protobufjs");
} catch (e) {
// we should only ever hit this case when the developer installs protobufjs-cli
// but forgets to install protobufjs with npm/yarn.
throw new Error("protobufjs could not be resolved. Make sure that it is installed.");
}
}
}
3 changes: 2 additions & 1 deletion cli/targets/json-module.js
Expand Up @@ -3,7 +3,8 @@ module.exports = json_module;

var util = require("../util");

var protobuf = require("../..");
var requireProtobufjs = require("../resolve-protobufjs");
var protobuf = requireProtobufjs();

json_module.description = "JSON representation as a module";

Expand Down
3 changes: 2 additions & 1 deletion cli/targets/proto.js
Expand Up @@ -3,7 +3,8 @@ module.exports = proto_target;

proto_target.private = true;

var protobuf = require("../..");
var requireProtobufjs = require("../resolve-protobufjs");
var protobuf = requireProtobufjs();

var Namespace = protobuf.Namespace,
Enum = protobuf.Enum,
Expand Down
3 changes: 2 additions & 1 deletion cli/targets/proto2.js
@@ -1,7 +1,8 @@
"use strict";
module.exports = proto2_target;

var protobuf = require("../..");
var requireProtobufjs = require("../resolve-protobufjs");
var protobuf = requireProtobufjs();

proto2_target.description = "Protocol Buffers, Version 2";

Expand Down
3 changes: 2 additions & 1 deletion cli/targets/proto3.js
@@ -1,7 +1,8 @@
"use strict";
module.exports = proto3_target;

var protobuf = require("../..");
var requireProtobufjs = require("../resolve-protobufjs");
var protobuf = requireProtobufjs();

proto3_target.description = "Protocol Buffers, Version 3";

Expand Down
5 changes: 3 additions & 2 deletions cli/targets/static-module.js
Expand Up @@ -6,9 +6,10 @@ module.exports = static_module_target;
// - CommonJS modules depend on the minimal build for reduced package size with browserify.
// - AMD and global scope depend on the full library for now.

var util = require("../util");
var util = require("../util"),
requireProtobufjs = require("../resolve-protobufjs");

var protobuf = require("../..");
var protobuf = requireProtobufjs();

static_module_target.description = "Static code without reflection as a module";

Expand Down
12 changes: 7 additions & 5 deletions cli/targets/static.js
@@ -1,11 +1,13 @@
"use strict";
module.exports = static_target;

var protobuf = require("../.."),
UglifyJS = require("uglify-js"),
espree = require("espree"),
escodegen = require("escodegen"),
estraverse = require("estraverse");
var UglifyJS = require("uglify-js"),
espree = require("espree"),
escodegen = require("escodegen"),
estraverse = require("estraverse"),
requireProtobufjs = require("../resolve-protobufjs");

var protobuf = requireProtobufjs();

var Type = protobuf.Type,
Service = protobuf.Service,
Expand Down
14 changes: 3 additions & 11 deletions cli/util.js
@@ -1,17 +1,9 @@
"use strict";
var fs = require("fs"),
path = require("path");
path = require("path"),
requireProtobufjs = require("./resolve-protobufjs");

try {
// installed as a peer dependency
require.resolve("protobufjs");
exports.pathToProtobufJs = "protobufjs";
} catch (e) {
// local development, i.e. forked from github
exports.pathToProtobufJs = "..";
}

var protobuf = require(exports.pathToProtobufJs);
var protobuf = requireProtobufjs();

function basenameCompare(a, b) {
var aa = String(a).replace(/\.\w+$/, "").split(/(-?\d*\.?\d+)/g),
Expand Down

0 comments on commit 7a30019

Please sign in to comment.