@odata2ts/odata2ts
Version:
Flexible generator to produce various TypeScript artefacts (from simple model interfaces to complete odata clients) from OData metadata files
65 lines • 3.15 kB
JavaScript
import { __awaiter, __rest } from "tslib";
import { NewLineKind } from "@ts-morph/common";
import load from "tsconfig-loader";
import ts from "typescript";
import { EmitModes } from "../OptionModel.js";
/**
* Loads the TS configuration from the specified path.
* Then maps the appropriate options to ts-morph compiler options.
*
* @param tsConfigPath path to tsconfig.json
* @param emitMode the used emit mode
* @param outputDir the used output dir
*/
export function loadTsMorphCompilerOptions(tsConfigPath, emitMode, outputDir) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const generateDeclarations = EmitModes.js_dts === emitMode || EmitModes.dts === emitMode;
// load config file
// NOTE: not async...
// @ts-ignore
const doLoad = (_a = load === null || load === void 0 ? void 0 : load.default) !== null && _a !== void 0 ? _a : load;
const conf = doLoad({ filename: tsConfigPath });
const _b = (conf === null || conf === void 0 ? void 0 : conf.tsConfig.compilerOptions) || {}, {
// ignored props
noEmit, // we always want to emit
importsNotUsedAsValues, // type is missing
jsx, plugins,
// mapped props
moduleResolution, lib, module, newLine, target, rootDir, rootDirs } = _b, passThrough = __rest(_b, ["noEmit", "importsNotUsedAsValues", "jsx", "plugins", "moduleResolution", "lib", "module", "newLine", "target", "rootDir", "rootDirs"]);
const compilerOpts = Object.assign(Object.assign({}, passThrough), { outDir: outputDir, declaration: generateDeclarations, moduleResolution: getModuleResolutionKind(moduleResolution), module: getModuleKind(module), target: getTarget(target) });
if (lib) {
compilerOpts.lib = lib;
}
if (newLine) {
compilerOpts.newLine = (newLine === null || newLine === void 0 ? void 0 : newLine.toLowerCase()) === "lf" ? NewLineKind.LineFeed : NewLineKind.CarriageReturnLineFeed;
}
return compilerOpts;
});
}
/**
* Maps to lower case.
* Also translates "node" to "nodejs".
*
* @param moduleResolution
*/
function getModuleResolutionKind(moduleResolution) {
const modRes = typeof moduleResolution === "string"
? moduleResolution.toLowerCase() === "node"
? "nodejs"
: moduleResolution.toLowerCase()
: undefined;
const matchedKey = Object.keys(ts.ModuleResolutionKind).find((mk) => mk.toLowerCase() === modRes);
return matchedKey ? ts.ModuleResolutionKind[matchedKey] : undefined;
}
function getModuleKind(module) {
const mod = typeof module === "string" ? module.toLowerCase() : undefined;
const matchedKey = Object.keys(ts.ModuleKind).find((mk) => mk.toLowerCase() === mod);
return matchedKey ? ts.ModuleKind[matchedKey] : undefined;
}
function getTarget(target) {
const t = typeof target === "string" ? target.toLowerCase() : undefined;
const matchedKey = Object.keys(ts.ScriptTarget).find((st) => st.toLowerCase() === t);
return matchedKey ? ts.ScriptTarget[matchedKey] : undefined;
}
//# sourceMappingURL=TsMorphHelper.js.map