@nestia/sdk
Version:
Nestia SDK and Swagger generator
104 lines • 4.65 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TsConfigReader = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
var TsConfigReader;
(function (TsConfigReader) {
TsConfigReader.read = (file) => __awaiter(this, void 0, void 0, function* () { return merge(file, new Set()); });
const merge = (file, visited) => __awaiter(this, void 0, void 0, function* () {
const location = path_1.default.resolve(file);
if (visited.has(location))
return {};
visited.add(location);
const current = parse(yield fs_1.default.promises.readFile(location, "utf8"));
const bases = [];
for (const parent of asArray(current.extends)) {
const next = resolveExtends(parent, path_1.default.dirname(location));
if (next !== null)
bases.push(yield merge(next, visited));
}
return [...bases, current].reduce((acc, elem) => { var _a, _b; return (Object.assign(Object.assign(Object.assign({}, acc), elem), { compilerOptions: Object.assign(Object.assign({}, ((_a = acc.compilerOptions) !== null && _a !== void 0 ? _a : {})), ((_b = elem.compilerOptions) !== null && _b !== void 0 ? _b : {})) })); }, {});
});
const parse = (text) => JSON.parse(stripTrailingCommas(stripJsonComments(text)));
const asArray = (value) => value === undefined ? [] : Array.isArray(value) ? value : [value];
const resolveExtends = (specifier, cwd) => {
const candidates = (base) => {
const ext = path_1.default.extname(base);
return ext === ".json" || ext === ".jsonc"
? [base]
: [base, `${base}.json`];
};
if (path_1.default.isAbsolute(specifier) || specifier.startsWith(".")) {
for (const candidate of candidates(path_1.default.resolve(cwd, specifier)))
if (fs_1.default.existsSync(candidate))
return candidate;
return null;
}
for (const candidate of [
specifier,
...candidates(specifier),
path_1.default.join(specifier, "tsconfig.json"),
])
try {
return require.resolve(candidate, { paths: [cwd] });
}
catch (_a) {
continue;
}
return null;
};
const stripJsonComments = (input) => {
let output = "";
let string = false;
let escaped = false;
for (let i = 0; i < input.length; ++i) {
const ch = input[i];
const next = input[i + 1];
if (string !== false) {
output += ch;
if (escaped)
escaped = false;
else if (ch === "\\")
escaped = true;
else if (ch === string)
string = false;
continue;
}
if (ch === '"' || ch === "'") {
string = ch;
output += ch;
continue;
}
if (ch === "/" && next === "/") {
while (i < input.length && input[i] !== "\n")
++i;
output += "\n";
continue;
}
if (ch === "/" && next === "*") {
i += 2;
while (i < input.length && !(input[i] === "*" && input[i + 1] === "/"))
++i;
++i;
continue;
}
output += ch;
}
return output;
};
const stripTrailingCommas = (input) => input.replace(/,\s*([}\]])/g, "$1");
})(TsConfigReader || (exports.TsConfigReader = TsConfigReader = {}));
//# sourceMappingURL=TsConfigReader.js.map