Skip to content
This repository was archived by the owner on Oct 24, 2021. It is now read-only.

Commit a1aa289

Browse files
committed
Add a binary to run the generator.
1 parent c263f05 commit a1aa289

File tree

5 files changed

+218
-2
lines changed

5 files changed

+218
-2
lines changed

bin/gen-typescript-declarations

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license
5+
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved. This
6+
* code may only be used under the BSD style license found at
7+
* http://polymer.github.io/LICENSE.txt The complete set of authors may be
8+
* found at http://polymer.github.io/AUTHORS.txt The complete set of
9+
* contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt Code
10+
* distributed by Google as part of the polymer project is also subject to an
11+
* additional IP rights grant found at http://polymer.github.io/PATENTS.txt
12+
*/
13+
14+
'use strict';
15+
16+
require('../lib/cli.js');
17+

package-lock.json

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
"description": "Generate TypeScript type declarations for Polymer components.",
55
"main": "lib/gen-ts.js",
66
"types": "lib/gen-ts.d.ts",
7+
"bin": "bin/gen-typescript-declarations",
78
"directories": {
89
"lib": "lib"
910
},
1011
"dependencies": {
12+
"command-line-args": "^4.0.7",
13+
"command-line-usage": "^4.0.1",
1114
"escodegen": "^1.9.0",
1215
"polymer-analyzer": "^2.3.0"
1316
},

src/cli.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved. This
4+
* code may only be used under the BSD style license found at
5+
* http://polymer.github.io/LICENSE.txt The complete set of authors may be
6+
* found at http://polymer.github.io/AUTHORS.txt The complete set of
7+
* contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt Code
8+
* distributed by Google as part of the polymer project is also subject to an
9+
* additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10+
*/
11+
12+
import * as fs from 'fs';
13+
import {generateDeclarations} from './gen-ts';
14+
15+
const commandLineArgs = require('command-line-args') as any;
16+
const commandLineUsage = require('command-line-usage') as any;
17+
18+
const argDefs = [
19+
{
20+
name: 'help',
21+
type: Boolean,
22+
description: 'Print this help text.',
23+
},
24+
{
25+
name: 'version',
26+
type: Boolean,
27+
description: 'Print the installed version.',
28+
},
29+
{
30+
name: 'root',
31+
type: String,
32+
defaultValue: '.',
33+
description: 'Root directory of the package to analyze (default ".").',
34+
},
35+
{
36+
name: 'out',
37+
type: String,
38+
description: 'Type declarations output file path (default stdout).',
39+
},
40+
];
41+
42+
async function run(argv: string[]) {
43+
const args = commandLineArgs(argDefs, {argv});
44+
45+
if (args.help) {
46+
console.log(commandLineUsage([
47+
{
48+
header: `gen-typescript-declarations`,
49+
content: 'https://github.com/Polymer/gen-typescript-declarations',
50+
},
51+
{
52+
header: `Options`,
53+
optionList: argDefs,
54+
}
55+
]));
56+
return;
57+
}
58+
59+
if (args.version) {
60+
console.log(require('../package.json').version);
61+
return;
62+
}
63+
64+
const declarations = await generateDeclarations(args.root);
65+
66+
if (args.out) {
67+
fs.writeFileSync(args.out, declarations);
68+
} else {
69+
process.stdout.write(declarations);
70+
}
71+
}
72+
73+
(async () => {
74+
try {
75+
await run(process.argv);
76+
} catch (e) {
77+
console.error(e);
78+
process.exit(1);
79+
}
80+
})();

src/gen-ts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ const header = `declare namespace Polymer {
2727
`;
2828
const footer = `}`;
2929

30-
export function generateDeclarations(): Promise<string> {
30+
export function generateDeclarations(root: string): Promise<string> {
3131
const analyzer = new Analyzer({
32-
urlLoader: new FSUrlLoader(),
32+
urlLoader: new FSUrlLoader(root),
3333
urlResolver: new PackageUrlResolver(),
3434
});
3535

0 commit comments

Comments
 (0)