Skip to content

Commit 3783af7

Browse files
committed
Initial pbts CLI for generating TypeScript definitions, see #550
1 parent baea920 commit 3783af7

File tree

14 files changed

+135
-47
lines changed

14 files changed

+135
-47
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,20 @@ protobuf.load("bundle.json", function(err, root) {
336336
});
337337
```
338338

339+
#### Generating TypeScript definitions for static modules
340+
341+
Likewise, the `pbts` command line utility can be used to generate TypeScript definitions from `pbjs`-generated static modules.
342+
343+
```
344+
Generates TypeScript definitions from annotated JavaScript files.
345+
346+
-n, --name Specifies the module name.
347+
348+
-o, --out Saves to a file instead of writing to stdout.
349+
350+
usage: pbts [options] file1.js file2.js ...
351+
```
352+
339353
Building
340354
--------
341355

bin/pbts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
var path = require("path"),
3+
cli = require(path.join(__dirname, "..", "cli", "pbts.js"));
4+
var ret = cli.main(process.argv);
5+
if (typeof ret === 'number')
6+
process.exit(ret);

cli/pbjs.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ var minimist = util.require("minimist", pkg.devDependencies.minimist),
88
glob = util.require("glob", pkg.devDependencies.glob);
99

1010
var protobuf = require(".."),
11-
targets = util.requireAll("./targets"),
12-
pkg = require("../package.json");
11+
targets = util.requireAll("./targets");
1312

1413
exports.main = function(args) {
1514
var argv = minimist(args.slice(2), {

cli/pbts.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var path = require("path"),
2+
fs = require("fs"),
3+
pkg = require(path.join(__dirname, "..", "package.json")),
4+
util = require("./util");
5+
var child_process = require("child_process");
6+
7+
var minimist = util.require("minimist", pkg.devDependencies.minimist),
8+
chalk = util.require("chalk", pkg.devDependencies.chalk),
9+
glob = util.require("glob", pkg.devDependencies.glob);
10+
11+
var jsdoc = util.require("jsdoc/package.json", pkg.devDependencies.jsdoc),
12+
tsdjsdoc = util.require("tsd-jsdoc/package.json", pkg.devDependencies['tsd-jsdoc']);
13+
14+
var protobuf = require("..");
15+
16+
exports.main = function(args) {
17+
var argv = minimist(args.slice(2), {
18+
alias: {
19+
out : "o"
20+
},
21+
string: [ "out" ]
22+
});
23+
24+
var files = argv._;
25+
26+
if (!files.length) {
27+
console.log([
28+
"protobuf.js v" + pkg.version + " cli for TypeScript",
29+
"",
30+
"Generates TypeScript definitions from annotated JavaScript files.",
31+
"",
32+
" -n, --name Specifies the module name.",
33+
"",
34+
" -o, --out Saves to a file instead of writing to stdout.",
35+
"",
36+
"usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.js file2.js ..."
37+
].join("\n"));
38+
return 1;
39+
}
40+
41+
// Resolve glob expressions
42+
for (var i = 0; i < files.length;) {
43+
if (glob.hasMagic(files[i])) {
44+
var matches = glob.sync(files[i]);
45+
Array.prototype.splice.apply(files, [i, 1].concat(matches));
46+
i += matches.length;
47+
} else
48+
++i;
49+
}
50+
51+
// There is no proper API for jsdoc, so this executes the CLI and writes to types/types.d.ts
52+
var child = child_process.exec("node node_modules/jsdoc/jsdoc.js -c jsdoc.types.json " + files.join(' '), {
53+
cwd: path.join(__dirname, ".."),
54+
argv0: "node",
55+
stdio: "pipe"
56+
});
57+
child.stdout.pipe(process.stdout);
58+
child.stderr.pipe(process.stderr);
59+
child.on("close", function(code) {
60+
if (code)
61+
throw Error("exited with " + code);
62+
63+
var dir = path.join(__dirname, "..", "types");
64+
var dts = fs.readFileSync(path.join(dir, "types.d.ts"), "utf8");
65+
fs.unlinkSync(path.join(dir, "types.d.ts"));
66+
67+
var header = [
68+
"// pbts " + process.argv.slice(2).join(' '),
69+
"// Generated " + (new Date()).toUTCString().replace(/GMT/, "UTC"),
70+
""
71+
];
72+
73+
// Remove declare statements and wrap everything in a module
74+
dts = dts.replace(/\bdeclare\s/g, "");
75+
dts = dts.replace(/^/mg, " ");
76+
dts = header.join('\n')+"\ndeclare module " + JSON.stringify(argv.name || "mymodule") + " {\n\n" + dts + "\n}\n";
77+
78+
if (argv.out)
79+
fs.writeFileSync(argv.out, dts);
80+
else
81+
process.stdout.write(dts, "utf8");
82+
});
83+
84+
return undefined;
85+
};

cli/util.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ exports.inspect = function inspect(object, indent) {
7070
};
7171

7272
exports.require = function(name, version) {
73+
var sub = "";
74+
var p = name.indexOf("/");
75+
if (p > -1) {
76+
sub = name.substring(p);
77+
name = name.substring(0, p);
78+
}
7379
var cwd = path.join(__dirname, "..");
7480
var dir = path.join(cwd, "node_modules", name);
7581
try {
@@ -81,7 +87,7 @@ exports.require = function(name, version) {
8187
cwd: cwd
8288
});
8389
}
84-
return require(name);
90+
return require(name + sub);
8591
};
8692

8793
exports.wrap = function(name, OUTPUT, ROOT) {

dist/protobuf.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

0 Bytes
Binary file not shown.

jsdoc.types.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
"allowUnknownTags": false
44
},
55
"source": {
6-
"include": [
7-
"./src/"
8-
],
96
"exclude": [],
107
"includePattern": ".+\\.js(doc)?$",
118
"excludePattern": "(^|\\/|\\\\)_"

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
],
2121
"main": "src/index",
2222
"bin": {
23-
"pbjs": "bin/pbjs"
23+
"pbjs": "bin/pbjs",
24+
"pbts": "bin/pbts"
2425
},
2526
"types": "types/protobuf.js.d.ts",
2627
"scripts": {
@@ -32,7 +33,7 @@
3233
"prepublish": "node scripts/prepublish",
3334
"prof": "node bench/prof",
3435
"test": "tape tests/*.js | tap-spec",
35-
"types": "jsdoc -c jsdoc.types.json && node scripts/types.js && tsc types/test.ts --lib es2015 --noEmit",
36+
"types": "node bin/pbts --name protobufjs --out types/protobuf.js.d.ts ./src && tsc types/test.ts --lib es2015 --noEmit",
3637
"zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js",
3738
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js"
3839
},

0 commit comments

Comments
 (0)