UNPKG

@platform/ts

Version:

TypesScript build, prepare and publish toolchain.

58 lines (57 loc) 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tsconfig = exports.closestParentOf = void 0; var tslib_1 = require("tslib"); var libs_1 = require("./libs"); function closestParentOf(child, cwd) { if (cwd === void 0) { cwd = '.'; } return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: cwd = libs_1.fs.resolve(cwd); return [4, libs_1.fs.readdir(cwd)]; case 1: if ((_a.sent()).includes(child)) { return [2, cwd]; } cwd = libs_1.fs.dirname(cwd); if (!cwd || cwd === '/') { return [2, undefined]; } return [2, closestParentOf(child, cwd)]; } }); }); } exports.closestParentOf = closestParentOf; function tsconfig(cwd, filename) { if (filename === void 0) { filename = 'tsconfig.json'; } return tslib_1.__awaiter(this, void 0, void 0, function () { var dir, path, data, outDir, error_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); filename = filename.endsWith('.json') ? filename : "".concat(filename, ".json"); return [4, closestParentOf(filename, cwd)]; case 1: dir = _a.sent(); if (!dir) { return [2, { success: false }]; } path = libs_1.fs.join(dir, filename); return [4, libs_1.fs.file.loadAndParse(path)]; case 2: data = _a.sent(); outDir = (data && data.compilerOptions && data.compilerOptions.outDir) || undefined; return [2, { success: true, filename: filename, dir: dir, path: path, data: data, outDir: outDir }]; case 3: error_1 = _a.sent(); throw new Error("Failed to load '".concat(filename, "' file. ").concat(error_1.message)); case 4: return [2]; } }); }); } exports.tsconfig = tsconfig;