@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
90 lines (87 loc) • 3.83 kB
JavaScript
import { __name } from './chunk-IRPJW6HH.js';
import { existsSync } from '@stryke/fs/exists';
import { readJsonFileSync } from '@stryke/fs/json';
import { joinPaths } from '@stryke/path/join-paths';
import { replacePath } from '@stryke/path/replace';
import defu from 'defu';
import ts from 'typescript';
function getTsconfigFilePath(projectRoot, tsconfig = "tsconfig.json") {
let tsconfigFilePath = tsconfig;
if (!existsSync(tsconfigFilePath)) {
tsconfigFilePath = joinPaths(projectRoot, replacePath(tsconfig, projectRoot));
if (!existsSync(tsconfigFilePath)) {
throw new Error(`Cannot find the \`tsconfig.json\` configuration file at ${joinPaths(projectRoot, replacePath(tsconfig, projectRoot))} or ${tsconfigFilePath}`);
}
}
return tsconfigFilePath;
}
__name(getTsconfigFilePath, "getTsconfigFilePath");
function findMatch(tsconfigType, types, extensions = [
".ts",
".tsx",
".d.ts"
]) {
return types.find((type) => tsconfigType?.toString().toLowerCase() === type?.toString().toLowerCase() || tsconfigType?.toString().toLowerCase() === `./${type?.toString().toLowerCase()}` || `./${tsconfigType?.toString().toLowerCase()}` === type?.toString().toLowerCase() || extensions.some((ext) => `${tsconfigType?.toString().toLowerCase()}${ext}` === type?.toString().toLowerCase() || `${tsconfigType?.toString().toLowerCase()}${ext}` === `./${type?.toString().toLowerCase()}` || `${type?.toString().toLowerCase()}${ext}` === `./${tsconfigType?.toString().toLowerCase()}` || tsconfigType?.toString().toLowerCase() === `${type?.toString().toLowerCase()}${ext}` || tsconfigType?.toString().toLowerCase() === `./${type?.toString().toLowerCase()}${ext}` || type?.toString().toLowerCase() === `./${tsconfigType?.toString().toLowerCase()}${ext}`));
}
__name(findMatch, "findMatch");
function findIncludeMatch(tsconfigType, types) {
return findMatch(tsconfigType, types, [
".ts",
".tsx",
".d.ts",
".js",
".jsx",
".mjs",
".cjs",
".mts",
".cts",
"/*.ts",
"/*.tsx",
"/*.d.ts",
"/*.js",
"/*.jsx",
"/*.mjs",
"/*.cjs",
"/*.mts",
"/*.cts",
"/**/*.ts",
"/**/*.tsx",
"/**/*.d.ts",
"/**/*.js",
"/**/*.jsx",
"/**/*.mjs",
"/**/*.cjs",
"/**/*.mts",
"/**/*.cts"
]);
}
__name(findIncludeMatch, "findIncludeMatch");
function isMatchFound(tsconfigType, types) {
return findMatch(tsconfigType, types) !== void 0;
}
__name(isMatchFound, "isMatchFound");
function isIncludeMatchFound(tsconfigType, types) {
return findIncludeMatch(tsconfigType, types) !== void 0;
}
__name(isIncludeMatchFound, "isIncludeMatchFound");
function getParsedTypeScriptConfig(workspaceRoot, projectRoot, tsconfig, tsconfigRaw = {}, host = ts.sys) {
const tsconfigFilePath = getTsconfigFilePath(projectRoot, tsconfig);
const tsconfigJson = readJsonFileSync(tsconfigFilePath);
if (!tsconfigJson) {
throw new Error(`Cannot find the \`tsconfig.json\` configuration file at ${joinPaths(projectRoot, tsconfig ?? "tsconfig.json")}`);
}
const parsedCommandLine = ts.parseJsonConfigFileContent(defu(tsconfigRaw ?? {}, tsconfigJson), host, joinPaths(workspaceRoot, projectRoot));
if (parsedCommandLine.errors.length > 0) {
const errorMessage = `Cannot parse the TypeScript compiler options. Please investigate the following issues:
${parsedCommandLine.errors.map((error) => `- ${(error.category !== void 0 && error.code ? `[${error.category}-${error.code}]: ` : "") + error.messageText.toString()}`).join("\n")}
`;
throw new Error(errorMessage);
}
return {
...parsedCommandLine,
tsconfigJson,
tsconfigFilePath
};
}
__name(getParsedTypeScriptConfig, "getParsedTypeScriptConfig");
export { findIncludeMatch, findMatch, getParsedTypeScriptConfig, getTsconfigFilePath, isIncludeMatchFound, isMatchFound };