Skip to content

Commit b7f9b4d

Browse files
author
Michael Hladky
committed
refactor: wip
1 parent 00197b9 commit b7f9b4d

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

packages/core/src/lib/implementation/read-rc-file.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export async function readRcByPath(
2929
const cfg: CoreConfig = await importModule({
3030
filepath: filePath,
3131
tsconfig,
32-
format: 'esm',
3332
});
3433

3534
return validate(coreConfigSchema, cfg, { filePath });

packages/utils/src/lib/file-system.ts

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ansis from 'ansis';
22
import { createJiti } from 'jiti';
33
import { mkdir, readFile, readdir, rm, stat } from 'node:fs/promises';
44
import path from 'node:path';
5+
import { parseJsonConfigFileContent, readConfigFile, sys } from 'typescript';
56
import type { Format, PersistConfig } from '@code-pushup/models';
67
import { formatBytes } from './formatting.js';
78
import { logMultipleResults } from './log-results.js';
@@ -77,16 +78,80 @@ export function logMultipleFileResults(
7778
);
7879
}
7980

80-
const jitiImport = createJiti(process.cwd());
81+
export function loadTargetConfig(tsConfigPath: string) {
82+
const resolvedConfigPath = path.resolve(tsConfigPath);
83+
const { config, error } = readConfigFile(resolvedConfigPath, sys.readFile);
84+
85+
if (error) {
86+
throw new Error(
87+
`Error reading TypeScript config file at ${tsConfigPath}:\n${error.messageText}`,
88+
);
89+
}
90+
91+
const parsedConfig = parseJsonConfigFileContent(
92+
config,
93+
sys,
94+
path.dirname(resolvedConfigPath),
95+
{},
96+
resolvedConfigPath,
97+
);
98+
99+
if (parsedConfig.fileNames.length === 0) {
100+
throw new Error(
101+
'No files matched by the TypeScript configuration. Check your "include", "exclude" or "files" settings.',
102+
);
103+
}
104+
105+
return parsedConfig;
106+
}
107+
81108
type JitiOptions = Parameters<typeof createJiti>[1];
82109

110+
export function tsConfigToJitiOptionsTransformer(
111+
tsConfigPath: string,
112+
): JitiOptions {
113+
const parsedConfig = loadTargetConfig(tsConfigPath);
114+
const compilerOptions = parsedConfig.options;
115+
116+
const jitiOptions: JitiOptions = {};
117+
118+
if (compilerOptions.paths) {
119+
const aliases: Record<string, string> = {};
120+
const basePath = compilerOptions.baseUrl || path.dirname(tsConfigPath);
121+
122+
for (const alias in compilerOptions.paths) {
123+
const paths = compilerOptions.paths[alias];
124+
if (paths && paths.length > 0) {
125+
aliases[alias] = path.resolve(basePath, paths[0] as string);
126+
}
127+
}
128+
jitiOptions.alias = aliases;
129+
}
130+
131+
if (compilerOptions.jsx) {
132+
jitiOptions.jsx = true;
133+
}
134+
135+
return jitiOptions;
136+
}
137+
83138
export async function importModule<T = unknown>(
84-
options: JitiOptions & { filepath: string },
139+
options: JitiOptions & { filepath: string; tsconfig?: string },
85140
): Promise<T> {
86-
const { filepath } = options;
87-
const { mod } = await jitiImport(filepath);
141+
const { filepath, tsconfig, ...jitiOptions } = options;
142+
143+
const jitiOptionsFromTsConfig = tsconfig
144+
? tsConfigToJitiOptionsTransformer(tsconfig)
145+
: {};
146+
147+
const jiti = createJiti(process.cwd(), {
148+
...jitiOptions,
149+
...jitiOptionsFromTsConfig,
150+
});
151+
152+
const mod = await jiti.import(filepath);
88153

89-
if (typeof mod === 'object' && 'default' in mod) {
154+
if (mod && typeof mod === 'object' && 'default' in mod) {
90155
return mod.default as T;
91156
}
92157
return mod as T;

0 commit comments

Comments
 (0)