UNPKG

4.18 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. This
5 * code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt The complete set of authors may be
7 * found at http://polymer.github.io/AUTHORS.txt The complete set of
8 * contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt Code
9 * distributed by Google as part of the polymer project is also subject to an
10 * additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 */
12var __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};
20Object.defineProperty(exports, "__esModule", { value: true });
21const fsExtra = require("fs-extra");
22const path = require("path");
23const gen_ts_1 = require("./gen-ts");
24const commandLineArgs = require('command-line-args');
25const commandLineUsage = require('command-line-usage');
26const 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];
54function 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}
96function 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//# sourceMappingURL=cli.js.map
\No newline at end of file