Skip to content

Commit 1edda53

Browse files
author
Taylor McIntyre
committed
support resolving protobufjs as peer or local dependency everywhere in the cli package
1 parent 8f9eba3 commit 1edda53

File tree

9 files changed

+46
-27
lines changed

9 files changed

+46
-27
lines changed

cli/pbjs.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"use strict";
22
var path = require("path"),
33
fs = require("fs"),
4-
pkg = require("./package.json"),
5-
util = require("./util");
6-
7-
var protobuf = require(util.pathToProtobufJs),
84
minimist = require("minimist"),
95
chalk = require("chalk"),
10-
glob = require("glob");
6+
glob = require("glob"),
7+
pkg = require("./package.json"),
8+
util = require("./util"),
9+
requireProtobufjs = require("./resolve-protobufjs");
10+
11+
var protobuf = requireProtobufjs();
1112

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

cli/resolve-protobufjs.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
exports.requireProtobufjs = function requireProtobufjs() {
4+
try {
5+
// local development, i.e. forked from github
6+
require.resolve("..");
7+
return require("..");
8+
} catch (e) {
9+
// installed as a peer dependency
10+
try {
11+
require.resolve("protobufjs");
12+
return require("protobufjs");
13+
} catch (e) {
14+
// we should only ever hit this case when the developer installs protobufjs-cli
15+
// but forgets to install protobufjs with npm/yarn.
16+
throw new Error("protobufjs could not be resolved. Make sure that it is installed.");
17+
}
18+
}
19+
}

cli/targets/json-module.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module.exports = json_module;
33

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

6-
var protobuf = require("../..");
6+
var requireProtobufjs = require("../resolve-protobufjs");
7+
var protobuf = requireProtobufjs();
78

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

cli/targets/proto.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module.exports = proto_target;
33

44
proto_target.private = true;
55

6-
var protobuf = require("../..");
6+
var requireProtobufjs = require("../resolve-protobufjs");
7+
var protobuf = requireProtobufjs();
78

89
var Namespace = protobuf.Namespace,
910
Enum = protobuf.Enum,

cli/targets/proto2.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22
module.exports = proto2_target;
33

4-
var protobuf = require("../..");
4+
var requireProtobufjs = require("../resolve-protobufjs");
5+
var protobuf = requireProtobufjs();
56

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

cli/targets/proto3.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22
module.exports = proto3_target;
33

4-
var protobuf = require("../..");
4+
var requireProtobufjs = require("../resolve-protobufjs");
5+
var protobuf = requireProtobufjs();
56

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

cli/targets/static-module.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ module.exports = static_module_target;
66
// - CommonJS modules depend on the minimal build for reduced package size with browserify.
77
// - AMD and global scope depend on the full library for now.
88

9-
var util = require("../util");
9+
var util = require("../util"),
10+
requireProtobufjs = require("../resolve-protobufjs");
1011

11-
var protobuf = require("../..");
12+
var protobuf = requireProtobufjs();
1213

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

cli/targets/static.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"use strict";
22
module.exports = static_target;
33

4-
var protobuf = require("../.."),
5-
UglifyJS = require("uglify-js"),
6-
espree = require("espree"),
7-
escodegen = require("escodegen"),
8-
estraverse = require("estraverse");
4+
var UglifyJS = require("uglify-js"),
5+
espree = require("espree"),
6+
escodegen = require("escodegen"),
7+
estraverse = require("estraverse"),
8+
requireProtobufjs = require("../resolve-protobufjs");
9+
10+
var protobuf = requireProtobufjs();
911

1012
var Type = protobuf.Type,
1113
Service = protobuf.Service,

cli/util.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
"use strict";
22
var fs = require("fs"),
3-
path = require("path");
3+
path = require("path"),
4+
requireProtobufjs = require("./resolve-protobufjs");
45

5-
try {
6-
// installed as a peer dependency
7-
require.resolve("protobufjs");
8-
exports.pathToProtobufJs = "protobufjs";
9-
} catch (e) {
10-
// local development, i.e. forked from github
11-
exports.pathToProtobufJs = "..";
12-
}
13-
14-
var protobuf = require(exports.pathToProtobufJs);
6+
var protobuf = requireProtobufjs();
157

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

0 commit comments

Comments
 (0)