1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
13 | return new (P || (P = Promise))(function (resolve, reject) {
|
14 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
15 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
16 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
17 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
18 | });
|
19 | };
|
20 | Object.defineProperty(exports, "__esModule", { value: true });
|
21 | const fsExtra = require("fs-extra");
|
22 | const path = require("path");
|
23 | const gen_ts_1 = require("./gen-ts");
|
24 | const commandLineArgs = require('command-line-args');
|
25 | const commandLineUsage = require('command-line-usage');
|
26 | const argDefs = [
|
27 | {
|
28 | name: 'help',
|
29 | type: Boolean,
|
30 | description: 'Print this help text.',
|
31 | },
|
32 | {
|
33 | name: 'version',
|
34 | type: Boolean,
|
35 | description: 'Print the installed version.',
|
36 | },
|
37 | {
|
38 | name: 'root',
|
39 | type: String,
|
40 | defaultValue: '.',
|
41 | description: 'Root directory of the package to analyze (default ".").',
|
42 | },
|
43 | {
|
44 | name: 'config',
|
45 | type: String,
|
46 | description: 'JSON configuration file (default "<root>/gen-tsd.json" if exists).',
|
47 | },
|
48 | {
|
49 | name: 'outDir',
|
50 | type: String,
|
51 | description: 'Type declarations output directory (default concatenated to stdout).',
|
52 | },
|
53 | ];
|
54 | function run(argv) {
|
55 | return __awaiter(this, void 0, void 0, function* () {
|
56 | const args = commandLineArgs(argDefs, { argv });
|
57 | if (args.help) {
|
58 | console.log(commandLineUsage([
|
59 | {
|
60 | header: `gen-typescript-declarations`,
|
61 | content: 'https://github.com/Polymer/gen-typescript-declarations',
|
62 | },
|
63 | {
|
64 | header: `Options`,
|
65 | optionList: argDefs,
|
66 | }
|
67 | ]));
|
68 | return;
|
69 | }
|
70 | if (args.version) {
|
71 | console.log(require('../package.json').version);
|
72 | return;
|
73 | }
|
74 | if (!args.config) {
|
75 | const p = path.join(args.root, 'gen-tsd.json');
|
76 | if (yield fsExtra.pathExists(p)) {
|
77 | args.config = p;
|
78 | }
|
79 | }
|
80 | let config = {};
|
81 | if (args.config) {
|
82 | console.info(`Loading config from "${args.config}".`);
|
83 | config = JSON.parse(yield fsExtra.readFile(args.config, 'utf8'));
|
84 | }
|
85 | const fileMap = yield gen_ts_1.generateDeclarations(args.root, config);
|
86 | if (args.outDir) {
|
87 | console.log('Writing type declarations to ' + path.resolve(args.outDir));
|
88 | yield writeFileMap(args.outDir, fileMap);
|
89 | }
|
90 | else {
|
91 | const concatenated = [...fileMap.values()].join('\n');
|
92 | process.stdout.write(concatenated);
|
93 | }
|
94 | });
|
95 | }
|
96 | function writeFileMap(rootDir, files) {
|
97 | return __awaiter(this, void 0, void 0, function* () {
|
98 | const promises = [];
|
99 | for (const [relPath, contents] of files) {
|
100 | const fullPath = path.join(rootDir, relPath);
|
101 | promises.push(fsExtra.outputFile(fullPath, contents));
|
102 | }
|
103 | yield Promise.all(promises);
|
104 | });
|
105 | }
|
106 | (() => __awaiter(this, void 0, void 0, function* () {
|
107 | try {
|
108 | yield run(process.argv);
|
109 | }
|
110 | catch (e) {
|
111 | console.error(e);
|
112 | process.exit(1);
|
113 | }
|
114 | }))();
|
115 |
|
\ | No newline at end of file |