Skip to content

Commit 648b760

Browse files
authored
fix: Run pbts jsdoc without a shell (#2160)
1 parent 71390e4 commit 648b760

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

cli/pbts.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,17 @@ exports.main = function(args, callback) {
9898
var basedir = path.join(__dirname, ".");
9999
var moduleName = argv.name || "null";
100100
var nodePath = process.execPath;
101-
var cmd = "\"" + nodePath + "\" \"" + require.resolve("jsdoc/jsdoc.js") + "\" -c \"" + path.join(basedir, "lib", "tsd-jsdoc.json") + "\" -q \"module=" + encodeURIComponent(moduleName) + "&comments=" + Boolean(argv.comments) + "\" " + files.map(function(file) { return "\"" + file + "\""; }).join(" ");
102-
var child = child_process.exec(cmd, {
101+
var jsdocArgs = [
102+
require.resolve("jsdoc/jsdoc.js"),
103+
"-c",
104+
path.join(basedir, "lib", "tsd-jsdoc.json"),
105+
"-q",
106+
"module=" + encodeURIComponent(moduleName) + "&comments=" + Boolean(argv.comments)
107+
].concat(files);
108+
var child = child_process.spawn(nodePath, jsdocArgs, {
103109
cwd: process.cwd(),
104110
argv0: "node",
105-
stdio: "pipe",
106-
maxBuffer: 1 << 26 // 67mb
111+
stdio: "pipe"
107112
});
108113
var out = [];
109114
var ended = false;

tests/cli.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var path = require("path");
55
var Module = require("module");
66
var protobuf = require("..");
77
var fs = require("fs");
8+
var EventEmitter = require("events").EventEmitter;
9+
var child_process = require("child_process");
810

911
function cliTest(test, testFunc) {
1012
// pbjs does not seem to work with Node v4, so skip this test if we're running on it
@@ -110,6 +112,38 @@ tape.test("pbjs generates correct ES6 static-module imports", function(test) {
110112
});
111113
});
112114

115+
tape.test("pbts passes jsdoc arguments without a shell", function(test) {
116+
var pbts = require("../cli/pbts");
117+
var originalSpawn = child_process.spawn;
118+
var file = "file with \"quotes\" `backticks` 'apostrophes' and ;.js";
119+
120+
test.plan(5);
121+
122+
child_process.spawn = function(cmd, args, options) {
123+
var child = new EventEmitter();
124+
child.stdout = new EventEmitter();
125+
child.stderr = { pipe: function() {} };
126+
127+
test.equal(cmd, process.execPath, "should execute node directly");
128+
test.ok(/jsdoc[\\/]jsdoc\.js$/.test(args[0]), "should execute jsdoc directly");
129+
test.equal(args[args.length - 1], file, "should pass file path as a single argument");
130+
test.equal(options.stdio, "pipe", "should pipe jsdoc output");
131+
132+
process.nextTick(function() {
133+
child.stdout.emit("data", "declare namespace test {}\n");
134+
child.stdout.emit("end");
135+
child.emit("close", 0);
136+
});
137+
138+
return child;
139+
};
140+
141+
pbts.main([file], function(err) {
142+
child_process.spawn = originalSpawn;
143+
test.error(err, "should generate definitions");
144+
});
145+
});
146+
113147
tape.test("without null-defaults, absent optional fields have zero values", function(test) {
114148
cliTest(test, function() {
115149
var root = protobuf.loadSync("tests/data/cli/null-defaults.proto");

0 commit comments

Comments
 (0)