Skip to content

Commit

Permalink
pbts: Pipe tsd-jsdoc output (requires dcodeIO/tsd-jsdoc/master) and r…
Browse files Browse the repository at this point in the history
…espect cwd, see #550
  • Loading branch information
dcodeIO committed Dec 14, 2016
1 parent 19c269f commit 4750e31
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cli/pbjs.js
Expand Up @@ -84,11 +84,11 @@ exports.main = function(args) {

// Search include paths when resolving imports
root.resolvePath = function pbjsResolvePath(origin, target) {
var filepath = protobuf.util.resolvePath(origin, target);
var filepath = protobuf.util.path.resolve(origin, target);
if (fs.existsSync(filepath))
return filepath;
for (var i = 0; i < paths.length; ++i) {
var ifilepath = protobuf.util.resolvePath(paths[i] + "/", target);
var ifilepath = protobuf.util.path.resolve(paths[i] + "/", target);
if (fs.existsSync(ifilepath))
return ifilepath;
}
Expand Down
28 changes: 17 additions & 11 deletions cli/pbts.js
Expand Up @@ -49,21 +49,27 @@ exports.main = function(args) {
++i;
}

// There is no proper API for jsdoc, so this executes the CLI and writes to types/types.d.ts
var child = child_process.exec("node node_modules/jsdoc/jsdoc.js -c jsdoc.types.json " + files.join(' '), {
cwd: path.join(__dirname, ".."),
// There is no proper API for jsdoc, so this executes the CLI and pipes the output
var basedir = path.join(__dirname, "..");
var child = child_process.exec("node \"" + basedir + "/node_modules/jsdoc/jsdoc.js\" -c \"" + basedir + "/jsdoc.types.json\" " + files.map(function(file) { return '"' + file + '"'; }).join(' '), {
cwd: process.cwd(),
argv0: "node",
stdio: "pipe"
});
child.stdout.pipe(process.stdout);
var out = [];
child.stdout.on("data", function(data) {
out.push(data);
});
child.stderr.pipe(process.stderr);
child.on("close", function(code) {
if (code)
throw Error("exited with " + code);

var dir = path.join(__dirname, "..", "types");
var dts = fs.readFileSync(path.join(dir, "types.d.ts"), "utf8");
fs.unlinkSync(path.join(dir, "types.d.ts"));
if (code) {
out = out.join('').replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
process.stderr.write(out);
process.exit(code);
return;
}

var dts = out.join('').replace(/{$/mg, "{\n").trim();

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

if (argv.out)
fs.writeFileSync(argv.out, dts);
Expand Down
2 changes: 1 addition & 1 deletion jsdoc.types.json
Expand Up @@ -17,7 +17,7 @@
"recurse" : true,
"private" : false,
"lenient" : true,
"destination" : "./types",
"destination" : "console",
"template" : "./node_modules/tsd-jsdoc"
}
}

0 comments on commit 4750e31

Please sign in to comment.