1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.tsconfig = exports.closestParentOf = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var libs_1 = require("./libs");
|
6 | function closestParentOf(child, cwd) {
|
7 | if (cwd === void 0) { cwd = '.'; }
|
8 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
9 | return tslib_1.__generator(this, function (_a) {
|
10 | switch (_a.label) {
|
11 | case 0:
|
12 | cwd = libs_1.fs.resolve(cwd);
|
13 | return [4, libs_1.fs.readdir(cwd)];
|
14 | case 1:
|
15 | if ((_a.sent()).includes(child)) {
|
16 | return [2, cwd];
|
17 | }
|
18 | cwd = libs_1.fs.dirname(cwd);
|
19 | if (!cwd || cwd === '/') {
|
20 | return [2, undefined];
|
21 | }
|
22 | return [2, closestParentOf(child, cwd)];
|
23 | }
|
24 | });
|
25 | });
|
26 | }
|
27 | exports.closestParentOf = closestParentOf;
|
28 | function tsconfig(cwd, filename) {
|
29 | if (filename === void 0) { filename = 'tsconfig.json'; }
|
30 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
31 | var dir, path, data, outDir, error_1;
|
32 | return tslib_1.__generator(this, function (_a) {
|
33 | switch (_a.label) {
|
34 | case 0:
|
35 | _a.trys.push([0, 3, , 4]);
|
36 | filename = filename.endsWith('.json') ? filename : filename + ".json";
|
37 | return [4, closestParentOf(filename, cwd)];
|
38 | case 1:
|
39 | dir = _a.sent();
|
40 | if (!dir) {
|
41 | return [2, { success: false }];
|
42 | }
|
43 | path = libs_1.fs.join(dir, filename);
|
44 | return [4, libs_1.fs.file.loadAndParse(path)];
|
45 | case 2:
|
46 | data = _a.sent();
|
47 | outDir = (data && data.compilerOptions && data.compilerOptions.outDir) || undefined;
|
48 | return [2, { success: true, filename: filename, dir: dir, path: path, data: data, outDir: outDir }];
|
49 | case 3:
|
50 | error_1 = _a.sent();
|
51 | throw new Error("Failed to load '" + filename + "' file. " + error_1.message);
|
52 | case 4: return [2];
|
53 | }
|
54 | });
|
55 | });
|
56 | }
|
57 | exports.tsconfig = tsconfig;
|