1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.checkTSConfigIsExist = checkTSConfigIsExist;
|
9 | exports.getExistFile = getExistFile;
|
10 | exports.getNodeModulePKG = getNodeModulePKG;
|
11 | exports.getParsedTSConfig = getParsedTSConfig;
|
12 | exports.getTsConfigPath = getTsConfigPath;
|
13 | exports.parseMappingArgument = parseMappingArgument;
|
14 | exports.tryDefault = tryDefault;
|
15 |
|
16 | var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
17 |
|
18 | var _path = require("path");
|
19 |
|
20 | var _fs = require("fs");
|
21 |
|
22 | var _os = _interopRequireDefault(require("os"));
|
23 |
|
24 | var _sync = _interopRequireDefault(require("escalade/sync"));
|
25 |
|
26 | function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
27 |
|
28 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
29 |
|
30 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
31 |
|
32 | function getExistFile(_ref) {
|
33 | var cwd = _ref.cwd,
|
34 | files = _ref.files,
|
35 | returnRelative = _ref.returnRelative;
|
36 |
|
37 | var _iterator = _createForOfIteratorHelper(files),
|
38 | _step;
|
39 |
|
40 | try {
|
41 | for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
42 | var file = _step.value;
|
43 | var absFilePath = (0, _path.join)(cwd, file);
|
44 |
|
45 | if ((0, _fs.existsSync)(absFilePath)) {
|
46 | return returnRelative ? file : absFilePath;
|
47 | }
|
48 | }
|
49 | } catch (err) {
|
50 | _iterator.e(err);
|
51 | } finally {
|
52 | _iterator.f();
|
53 | }
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function checkTSConfigIsExist() {
|
62 | var cwd = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : process.cwd();
|
63 | return (0, _fs.existsSync)((0, _path.join)(cwd, 'tsconfig.json'));
|
64 | }
|
65 |
|
66 | function tryDefault(obj) {
|
67 | return obj["default"] || obj;
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function parseMappingArgument(globalStrings, processValue) {
|
75 | var globals = {};
|
76 | globalStrings.split(',').forEach(function (globalString) {
|
77 | var _globalString$split = globalString.split('='),
|
78 | _globalString$split2 = (0, _slicedToArray2["default"])(_globalString$split, 2),
|
79 | key = _globalString$split2[0],
|
80 | value = _globalString$split2[1];
|
81 |
|
82 | if (processValue) {
|
83 | var r = processValue(value, key);
|
84 |
|
85 | if (r !== undefined) {
|
86 | if (Array.isArray(r)) {
|
87 | var _r = (0, _slicedToArray2["default"])(r, 2);
|
88 |
|
89 | value = _r[0];
|
90 | key = _r[1];
|
91 | } else {
|
92 | value = r;
|
93 | }
|
94 | }
|
95 | }
|
96 |
|
97 | globals[key] = value;
|
98 | });
|
99 | return globals;
|
100 | }
|
101 |
|
102 | function getTsConfigPath(cwd, rootPath) {
|
103 | var tsconfigPath = (0, _path.join)(cwd, 'tsconfig.json');
|
104 | var templateTsconfigPath = (0, _path.join)(__dirname, '../template/tsconfig.json');
|
105 |
|
106 | try {
|
107 | if ((0, _fs.existsSync)(tsconfigPath)) {
|
108 | return tsconfigPath;
|
109 | }
|
110 |
|
111 | if (rootPath && (0, _fs.existsSync)((0, _path.join)(rootPath, 'tsconfig.json'))) {
|
112 | return (0, _path.join)(rootPath, 'tsconfig.json');
|
113 | }
|
114 | } catch (e) {}
|
115 |
|
116 | return templateTsconfigPath;
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | function getParsedTSConfig(cwd, rootPath) {
|
124 | try {
|
125 | var parseTsconfig = function parseTsconfig(path) {
|
126 | var _parsed$errors;
|
127 |
|
128 | var _require = require('typescript'),
|
129 | readConfigFile = _require.readConfigFile,
|
130 | sys = _require.sys,
|
131 | formatDiagnostic = _require.formatDiagnostic,
|
132 | parseJsonConfigFileContent = _require.parseJsonConfigFileContent;
|
133 |
|
134 | var result = readConfigFile(path, sys.readFile);
|
135 | var formatDiagnosticHost = {
|
136 | getCanonicalFileName: function getCanonicalFileName(fileName) {
|
137 | return fileName;
|
138 | },
|
139 | getCurrentDirectory: sys.getCurrentDirectory,
|
140 | getNewLine: function getNewLine() {
|
141 | return _os["default"].EOL;
|
142 | }
|
143 | };
|
144 |
|
145 | if (result !== null && result !== void 0 && result.error) {
|
146 | console.log(formatDiagnostic(result.error, formatDiagnosticHost));
|
147 | return;
|
148 | }
|
149 |
|
150 | var parsed = parseJsonConfigFileContent(result.config, {
|
151 | useCaseSensitiveFileNames: false,
|
152 | readDirectory: sys.readDirectory,
|
153 | fileExists: sys.fileExists,
|
154 | readFile: sys.readFile
|
155 | }, (0, _path.dirname)(path));
|
156 |
|
157 | if (parsed !== null && parsed !== void 0 && (_parsed$errors = parsed.errors) !== null && _parsed$errors !== void 0 && _parsed$errors.length) {
|
158 | var _parsed$errors2;
|
159 |
|
160 | console.log(formatDiagnostic(parsed === null || parsed === void 0 ? void 0 : (_parsed$errors2 = parsed.errors) === null || _parsed$errors2 === void 0 ? void 0 : _parsed$errors2[0], formatDiagnosticHost));
|
161 | return;
|
162 | }
|
163 |
|
164 | return parsed.options;
|
165 | };
|
166 |
|
167 | var getTsconfigCompilerOptions = function getTsconfigCompilerOptions(path) {
|
168 | var config = parseTsconfig(path);
|
169 | return config || undefined;
|
170 | };
|
171 |
|
172 | return getTsconfigCompilerOptions(getTsConfigPath(cwd, rootPath)) || {};
|
173 | } catch (e) {
|
174 | return {};
|
175 | }
|
176 | }
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | function getNodeModulePKG(moduleName) {
|
183 | try {
|
184 | var pkg = (0, _sync["default"])(require.resolve(moduleName), function (directory, names) {
|
185 | if (names.includes('package.json')) {
|
186 | return 'package.json';
|
187 | }
|
188 |
|
189 | return undefined;
|
190 | });
|
191 |
|
192 | if (!pkg) {
|
193 | throw new Error("".concat(moduleName, " is not installed"));
|
194 | }
|
195 |
|
196 | return {
|
197 | "default": require(moduleName),
|
198 | version: require(pkg).version
|
199 | };
|
200 | } catch (e) {
|
201 | return {
|
202 | "default": undefined
|
203 | };
|
204 | }
|
205 | } |
\ | No newline at end of file |