Skip to content

Commit 4750e31

Browse files
committed
pbts: Pipe tsd-jsdoc output (requires dcodeIO/tsd-jsdoc/master) and respect cwd, see #550
1 parent 19c269f commit 4750e31

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

cli/pbjs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ exports.main = function(args) {
8484

8585
// Search include paths when resolving imports
8686
root.resolvePath = function pbjsResolvePath(origin, target) {
87-
var filepath = protobuf.util.resolvePath(origin, target);
87+
var filepath = protobuf.util.path.resolve(origin, target);
8888
if (fs.existsSync(filepath))
8989
return filepath;
9090
for (var i = 0; i < paths.length; ++i) {
91-
var ifilepath = protobuf.util.resolvePath(paths[i] + "/", target);
91+
var ifilepath = protobuf.util.path.resolve(paths[i] + "/", target);
9292
if (fs.existsSync(ifilepath))
9393
return ifilepath;
9494
}

cli/pbts.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,27 @@ exports.main = function(args) {
4949
++i;
5050
}
5151

52-
// There is no proper API for jsdoc, so this executes the CLI and writes to types/types.d.ts
53-
var child = child_process.exec("node node_modules/jsdoc/jsdoc.js -c jsdoc.types.json " + files.join(' '), {
54-
cwd: path.join(__dirname, ".."),
52+
// There is no proper API for jsdoc, so this executes the CLI and pipes the output
53+
var basedir = path.join(__dirname, "..");
54+
var child = child_process.exec("node \"" + basedir + "/node_modules/jsdoc/jsdoc.js\" -c \"" + basedir + "/jsdoc.types.json\" " + files.map(function(file) { return '"' + file + '"'; }).join(' '), {
55+
cwd: process.cwd(),
5556
argv0: "node",
5657
stdio: "pipe"
5758
});
58-
child.stdout.pipe(process.stdout);
59+
var out = [];
60+
child.stdout.on("data", function(data) {
61+
out.push(data);
62+
});
5963
child.stderr.pipe(process.stderr);
6064
child.on("close", function(code) {
61-
if (code)
62-
throw Error("exited with " + code);
63-
64-
var dir = path.join(__dirname, "..", "types");
65-
var dts = fs.readFileSync(path.join(dir, "types.d.ts"), "utf8");
66-
fs.unlinkSync(path.join(dir, "types.d.ts"));
65+
if (code) {
66+
out = out.join('').replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
67+
process.stderr.write(out);
68+
process.exit(code);
69+
return;
70+
}
71+
72+
var dts = out.join('').replace(/{$/mg, "{\n").trim();
6773

6874
var header = [
6975
"// $> pbts " + process.argv.slice(2).join(' '),
@@ -74,7 +80,7 @@ exports.main = function(args) {
7480
// Remove declare statements and wrap everything in a module
7581
dts = dts.replace(/\bdeclare\s/g, "");
7682
dts = dts.replace(/^/mg, " ");
77-
dts = header.join('\n')+"\ndeclare module " + JSON.stringify(argv.name || "mymodule") + " {\n\n" + dts + "\n}\n";
83+
dts = header.join('\n')+"\ndeclare module " + JSON.stringify(argv.name || "mymodule") + " {\n\n" + dts + "\n\n}\n";
7884

7985
if (argv.out)
8086
fs.writeFileSync(argv.out, dts);

jsdoc.types.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"recurse" : true,
1818
"private" : false,
1919
"lenient" : true,
20-
"destination" : "./types",
20+
"destination" : "console",
2121
"template" : "./node_modules/tsd-jsdoc"
2222
}
2323
}

0 commit comments

Comments
 (0)