diff --git a/cli/pbjs.js b/cli/pbjs.js index 63b4a543d..e04900712 100644 --- a/cli/pbjs.js +++ b/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"); diff --git a/cli/resolve-protobufjs.js b/cli/resolve-protobufjs.js new file mode 100644 index 000000000..7f950a968 --- /dev/null +++ b/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."); + } + } +} diff --git a/cli/targets/json-module.js b/cli/targets/json-module.js index 5255cd99c..744f16451 100644 --- a/cli/targets/json-module.js +++ b/cli/targets/json-module.js @@ -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"; diff --git a/cli/targets/proto.js b/cli/targets/proto.js index d633f1688..64d48c8b9 100644 --- a/cli/targets/proto.js +++ b/cli/targets/proto.js @@ -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, diff --git a/cli/targets/proto2.js b/cli/targets/proto2.js index 09521e061..2e6380a56 100644 --- a/cli/targets/proto2.js +++ b/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"; diff --git a/cli/targets/proto3.js b/cli/targets/proto3.js index 661c916bd..8cf94dc13 100644 --- a/cli/targets/proto3.js +++ b/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"; diff --git a/cli/targets/static-module.js b/cli/targets/static-module.js index b56194768..edf8658a6 100644 --- a/cli/targets/static-module.js +++ b/cli/targets/static-module.js @@ -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"; diff --git a/cli/targets/static.js b/cli/targets/static.js index 0c938b0ab..9c6f7c735 100644 --- a/cli/targets/static.js +++ b/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, diff --git a/cli/util.js b/cli/util.js index 52202370c..89d41a91c 100644 --- a/cli/util.js +++ b/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),