UNPKG

4.56 kBJavaScriptView Raw
1#! /usr/bin/env node
2"use strict";
3Object.defineProperty(exports, "__esModule", { value: true });
4var fs = require("fs");
5var globby = require("globby");
6var path = require("path");
7var fs_helpers_1 = require("./helpers/fs.helpers");
8var output_item_1 = require("./output-item");
9var tsgen_1 = require("./tsgen");
10var defaultConfigFileName = 'tsgen.json';
11(function () {
12 var configFilePath = getConfigFilePath();
13 var workingDirectory = path.dirname(configFilePath);
14 var config = require(configFilePath);
15 if (config.outDir === undefined) {
16 throw new Error('Invalid config: must specify an outDir.');
17 }
18 var sourcePaths = globby.sync(config.src, { cwd: workingDirectory })
19 .map(function (file) { return path.normalize(path.join(workingDirectory, file)); });
20 var outputItems = generateOutput(sourcePaths, config.options);
21 writeOutput(workingDirectory, config, outputItems);
22})();
23function getConfigFilePath() {
24 var configFileName = defaultConfigFileName;
25 if (process.argv.length > 2) {
26 configFileName = process.argv[2];
27 }
28 try {
29 var stats = fs.lstatSync(configFileName);
30 if (!stats.isFile()) {
31 throw new Error(configFileName + " is not a file.");
32 }
33 }
34 catch (_a) {
35 throw new Error(configFileName + " does not exist.");
36 }
37 return path.normalize(path.join(process.cwd(), configFileName));
38}
39function generateOutput(sourcePaths, options) {
40 var outputItems = [];
41 for (var _i = 0, sourcePaths_1 = sourcePaths; _i < sourcePaths_1.length; _i++) {
42 var filePath = sourcePaths_1[_i];
43 var name = path.basename(filePath, '.cs');
44 var csharp = fs.readFileSync(filePath).toString();
45 var typescript = tsgen_1.tsgen(csharp, options);
46 outputItems.push(new output_item_1.OutputItem(name, typescript));
47 }
48 return outputItems;
49}
50function writeOutput(workingDirectory, config, outputItems) {
51 var outputDirPath = path.normalize(path.join(workingDirectory, config.outDir));
52 var generatedFilePath = path.join(outputDirPath, 'generated.ts');
53 var importPaths = (config.imports || []).slice().map(function (importPath) { return path.isAbsolute(importPath) ? importPath : path.normalize(path.join(workingDirectory, importPath)); });
54 var tsCode = outputItems.map(function (item) { return item.typescript; });
55 if (importPaths.length) {
56 var importCode = importPaths
57 .map(function (importPath) { return generateImports(importPath, outputDirPath); })
58 .join('\n\n');
59 tsCode.unshift(importCode);
60 }
61 var concatenatedTypescript = tsCode
62 .map(function (code) { return code.trim(); })
63 .filter(function (code) { return code !== undefined && code.length > 0; })
64 .join('\n\n');
65 fs_helpers_1.writeFile(generatedFilePath, "/* tslint:disable */\n\n" + concatenatedTypescript + "\n");
66 writeIndex(workingDirectory, generatedFilePath, config);
67}
68function writeIndex(workingDirectory, generatedFilePath, config) {
69 var outputDirPath = path.dirname(generatedFilePath);
70 var indexFilePath = path.join(outputDirPath, 'index.ts');
71 var exportPaths = (config.exports || []).concat([generatedFilePath]).map(function (exportPath) { return path.isAbsolute(exportPath) ? exportPath : path.normalize(path.join(workingDirectory, exportPath)); });
72 var exportCode = exportPaths
73 .map(function (exportPath) { return generateExports(exportPath, outputDirPath); })
74 .join('\n\n');
75 fs_helpers_1.writeFile(indexFilePath, "/* tslint:disable */\n\n" + exportCode + "\n");
76}
77function generateImports(sourcePath, outputDirPath) {
78 return generateImportsOrExports('import', sourcePath, outputDirPath);
79}
80function generateExports(sourcePath, outputDirPath) {
81 return generateImportsOrExports('export', sourcePath, outputDirPath);
82}
83function generateImportsOrExports(type, sourcePath, outputDirPath) {
84 var exportRegex = /export\s+(?:(?:(?:class|const|enum|interface|function|type)\s+([0-9A-Za-z_]+))|(?:{\s+([0-9A-Za-z_]+)\s+}))/g;
85 var source = fs.readFileSync(sourcePath).toString();
86 var match;
87 var exportedNames = [];
88 while ((match = exportRegex.exec(source)) !== null) {
89 exportedNames.push(match[1] ? match[1] : match[2]);
90 }
91 var relativeReferencePath = path.relative(outputDirPath, sourcePath).replace(/\\/g, '/').replace(/\.ts$/, '');
92 return type + " {\n " + exportedNames.join(',\n ') + "\n} from './" + relativeReferencePath + "';";
93}
94//# sourceMappingURL=tsgen-cli.js.map
\No newline at end of file