1 | #!/usr/bin/env node
|
2 | "use strict";
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | var path = require("path");
|
5 | var ts = require("typescript");
|
6 | var yargs = require("yargs");
|
7 | var load_config_file_1 = require("../config-file/load-config-file");
|
8 | var bundle_generator_1 = require("../bundle-generator");
|
9 | var check_diagnostics_errors_1 = require("../helpers/check-diagnostics-errors");
|
10 | var get_compiler_options_1 = require("../get-compiler-options");
|
11 | var fix_path_1 = require("../helpers/fix-path");
|
12 | var measure_time_1 = require("../helpers/measure-time");
|
13 | var logger_1 = require("../logger");
|
14 |
|
15 | function toStringsArray(data) {
|
16 | if (!Array.isArray(data)) {
|
17 | throw new Error(data + " is not a array");
|
18 | }
|
19 | return data.map(String);
|
20 | }
|
21 | function parseArgs() {
|
22 | return yargs
|
23 | .parserConfiguration({
|
24 | 'boolean-negation': false,
|
25 | 'camel-case-expansion': false,
|
26 | 'dot-notation': false,
|
27 | 'short-option-groups': false,
|
28 | })
|
29 | .usage('Usage: $0 [options] <file(s)>')
|
30 | .demandCommand(0)
|
31 | .option('out-file', {
|
32 | alias: 'o',
|
33 | type: 'string',
|
34 | description: 'File name of generated d.ts',
|
35 | })
|
36 | .option('verbose', {
|
37 | type: 'boolean',
|
38 | default: false,
|
39 | description: 'Enable verbose logging',
|
40 | })
|
41 | .option('silent', {
|
42 | type: 'boolean',
|
43 | default: false,
|
44 | description: 'Disable any logging except errors',
|
45 | })
|
46 | .option('no-check', {
|
47 | type: 'boolean',
|
48 | default: false,
|
49 | description: 'Skip validation of generated d.ts file',
|
50 | })
|
51 | .option('fail-on-class', {
|
52 | type: 'boolean',
|
53 | default: false,
|
54 | description: 'Fail if generated dts contains class declaration',
|
55 | })
|
56 | .option('external-inlines', {
|
57 | type: 'array',
|
58 | description: 'Array of package names from node_modules to inline typings from.\n' +
|
59 | 'Used types will be inlined into the output file',
|
60 | coerce: toStringsArray,
|
61 | })
|
62 | .option('external-imports', {
|
63 | type: 'array',
|
64 | description: 'Array of package names from node_modules to import typings from.\n' +
|
65 | 'Used types will be imported using "import { First, Second } from \'library-name\';".\n' +
|
66 | 'By default all libraries will be imported (except inlined libraries and libraries from @types)',
|
67 | coerce: toStringsArray,
|
68 | })
|
69 | .option('external-types', {
|
70 | type: 'array',
|
71 | description: 'Array of package names from @types to import typings from via the triple-slash reference directive.\n' +
|
72 | 'By default all packages are allowed and will be used according to their usages',
|
73 | coerce: toStringsArray,
|
74 | })
|
75 | .option('umd-module-name', {
|
76 | type: 'string',
|
77 | description: 'Name of the UMD module. If specified then `export as namespace ModuleName;` will be emitted',
|
78 | })
|
79 | .option('project', {
|
80 | type: 'string',
|
81 | description: 'Path to the tsconfig.json file that will be used for the compilation',
|
82 | })
|
83 | .option('sort', {
|
84 | type: 'boolean',
|
85 | default: false,
|
86 | description: 'Sort output nodes',
|
87 | })
|
88 | .option('inline-declare-global', {
|
89 | type: 'boolean',
|
90 | default: false,
|
91 | description: 'Enables inlining of `declare global` statements contained in files which should be inlined (all local files and packages from `--external-inlines`)',
|
92 | })
|
93 | .option('inline-declare-externals', {
|
94 | type: 'boolean',
|
95 | default: false,
|
96 | description: 'Enables inlining of `declare module` statements of the global modules (e.g. `declare module \'external-module\' {}`, but NOT `declare module \'./internal-module\' {}`) contained in files which should be inlined (all local files and packages from inlined libraries)',
|
97 | })
|
98 | .option('disable-symlinks-following', {
|
99 | type: 'boolean',
|
100 | default: false,
|
101 | description: '(EXPERIMENTAL) Disables resolving of symlinks to the original path. See https://github.com/timocov/dts-bundle-generator/issues/39 for more information',
|
102 | })
|
103 | .option('config', {
|
104 | type: 'string',
|
105 | description: 'File path to the generator config file',
|
106 | })
|
107 | .version()
|
108 | .strict()
|
109 | .example('$0 path/to/your/entry-file.ts', '')
|
110 | .example('$0 path/to/your/entry-file.ts path/to/your/entry-file-2.ts', '')
|
111 | .example('$0 --external-types jquery react -- entry-file.ts', '')
|
112 | .wrap(Math.min(100, yargs.terminalWidth()))
|
113 | .argv;
|
114 | }
|
115 | function generateOutFileName(inputFilePath) {
|
116 | var inputFileName = path.parse(inputFilePath).name;
|
117 | return fix_path_1.fixPath(path.join(inputFilePath, '..', inputFileName + '.d.ts'));
|
118 | }
|
119 |
|
120 | function main() {
|
121 | var args = parseArgs();
|
122 | if (args.silent && args.verbose) {
|
123 | throw new Error('Cannot use both silent and verbose options at the same time');
|
124 | }
|
125 | else if (args.verbose) {
|
126 | logger_1.enableVerbose();
|
127 | }
|
128 | else if (!args.silent) {
|
129 | logger_1.enableNormalLog();
|
130 | }
|
131 | var bundlerConfig;
|
132 | if (args.config !== undefined) {
|
133 | logger_1.verboseLog("Trying to load config from " + args.config + " file...");
|
134 | bundlerConfig = load_config_file_1.loadConfigFile(args.config);
|
135 | }
|
136 | else {
|
137 | if (args._.length < 1) {
|
138 | throw new Error('No input files specified');
|
139 | }
|
140 | if (args._.length > 1 && args['out-file']) {
|
141 | throw new Error('Cannot use outFile with multiple entries');
|
142 | }
|
143 | bundlerConfig = {
|
144 | entries: args._.map(function (path) {
|
145 | return {
|
146 | filePath: path,
|
147 | outFile: args['out-file'],
|
148 | noCheck: args['no-check'],
|
149 | libraries: {
|
150 | allowedTypesLibraries: args['external-types'],
|
151 | importedLibraries: args['external-imports'],
|
152 | inlinedLibraries: args['external-inlines'],
|
153 | },
|
154 | output: {
|
155 | inlineDeclareExternals: args['inline-declare-externals'],
|
156 | inlineDeclareGlobals: args['inline-declare-global'],
|
157 | umdModuleName: args['umd-module-name'],
|
158 | sortNodes: args.sort,
|
159 | },
|
160 | failOnClass: args['fail-on-class'],
|
161 | };
|
162 | }),
|
163 | compilationOptions: {
|
164 | preferredConfigPath: args.project,
|
165 | followSymlinks: !args['disable-symlinks-following'],
|
166 | },
|
167 | };
|
168 | }
|
169 | logger_1.verboseLog("Total entries count=" + bundlerConfig.entries.length);
|
170 | var generatedDts = bundle_generator_1.generateDtsBundle(bundlerConfig.entries, bundlerConfig.compilationOptions);
|
171 | var outFilesToCheck = [];
|
172 | for (var i = 0; i < bundlerConfig.entries.length; ++i) {
|
173 | var entry = bundlerConfig.entries[i];
|
174 | var outFile = entry.outFile !== undefined ? entry.outFile : generateOutFileName(entry.filePath);
|
175 | logger_1.normalLog("Writing " + entry.filePath + " -> " + outFile);
|
176 | ts.sys.writeFile(outFile, generatedDts[i]);
|
177 | if (!entry.noCheck) {
|
178 | outFilesToCheck.push(outFile);
|
179 | }
|
180 | }
|
181 | if (outFilesToCheck.length === 0) {
|
182 | logger_1.normalLog('File checking is skipped (due nothing to check)');
|
183 | return;
|
184 | }
|
185 | logger_1.normalLog('Checking generated files...');
|
186 | var preferredConfigFile = bundlerConfig.compilationOptions !== undefined ? bundlerConfig.compilationOptions.preferredConfigPath : undefined;
|
187 | var compilerOptions = get_compiler_options_1.getCompilerOptions(outFilesToCheck, preferredConfigFile);
|
188 | if (compilerOptions.skipLibCheck) {
|
189 | compilerOptions.skipLibCheck = false;
|
190 | logger_1.warnLog('Compiler option "skipLibCheck" is disabled to properly check generated output');
|
191 | }
|
192 | var program = ts.createProgram(outFilesToCheck, compilerOptions);
|
193 | check_diagnostics_errors_1.checkProgramDiagnosticsErrors(program);
|
194 | }
|
195 | try {
|
196 | var executionTime = measure_time_1.measureTime(main);
|
197 | logger_1.normalLog("Done in " + (executionTime / 1000).toFixed(2) + "s");
|
198 | }
|
199 | catch (ex) {
|
200 | logger_1.normalLog('');
|
201 | logger_1.errorLog("Error: " + ex.message);
|
202 | process.exit(1);
|
203 | }
|