Skip to content

Commit 4c75af5

Browse files
committed
refactor: fix lint
1 parent aee50d9 commit 4c75af5

File tree

4 files changed

+18
-72
lines changed

4 files changed

+18
-72
lines changed

packages/utils/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
"@code-pushup/models": "0.108.1",
3131
"ansis": "^3.3.0",
3232
"build-md": "^0.4.2",
33-
"bundle-require": "^5.1.0",
3433
"esbuild": "^0.25.2",
3534
"ora": "^9.0.0",
3635
"semver": "^7.6.0",
3736
"simple-git": "^3.20.0",
3837
"string-width": "^8.1.0",
3938
"wrap-ansi": "^9.0.2",
4039
"zod": "^4.2.1",
41-
"typescript": "5.7.3"
40+
"typescript": "5.7.3",
41+
"jiti": "^2.4.2"
4242
},
4343
"files": [
4444
"src",

packages/utils/src/lib/import-module.int.test.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path';
22
import { describe, expect, it, vi } from 'vitest';
3-
import { deriveTsConfig, importModule } from './import-module.js';
3+
import { importModule } from './import-module.js';
44

55
describe('importModule', () => {
66
const mockDir = path.join(
@@ -82,35 +82,3 @@ describe('importModule', () => {
8282
).resolves.toStrictEqual({ key: 'value' });
8383
});
8484
});
85-
86-
describe('deriveTsConfig', () => {
87-
const mockDir = path.join(
88-
process.cwd(),
89-
'packages',
90-
'utils',
91-
'mocks',
92-
'fixtures',
93-
);
94-
95-
it('should load a valid tsconfig.json file', async () => {
96-
const configPath = path.join(mockDir, 'tsconfig-setup', 'tsconfig.json');
97-
98-
await expect(deriveTsConfig(configPath)).resolves.toStrictEqual({
99-
configFilePath: expect.any(String),
100-
paths: {
101-
'@utils/*': ['./utils.ts'],
102-
},
103-
pathsBasePath: expect.any(String),
104-
});
105-
});
106-
107-
it('should throw if the path is empty', async () => {
108-
await expect(deriveTsConfig('')).rejects.toThrow(/Tsconfig file not found/);
109-
});
110-
111-
it('should throw if the file does not exist', async () => {
112-
await expect(
113-
deriveTsConfig(path.join('non-existent', 'tsconfig.json')),
114-
).rejects.toThrow(/Tsconfig file not found/);
115-
});
116-
});

packages/utils/src/lib/import-module.ts

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { type JitiOptions, createJiti as createJitiSource } from 'jiti';
22
import { stat } from 'node:fs/promises';
33
import path from 'node:path';
44
import type { CompilerOptions } from 'typescript';
5-
import { fileExists } from './file-system';
6-
import { loadTargetConfig } from './load-ts-config';
5+
import { fileExists } from './file-system.js';
6+
import { loadTargetConfig } from './load-ts-config.js';
77
import { settlePromise } from './promises.js';
88

99
export async function importModule<T = unknown>(
@@ -132,11 +132,17 @@ export async function createTsJiti(
132132
) {
133133
const { tsconfigPath, ...jitiOptions } = options;
134134
const fallbackTsconfigPath = path.resolve('./tsconfig.json');
135-
const tsDerivedJitiOptions: MappableJitiOptions = tsconfigPath
136-
? await jitiOptionsFromTsConfig(tsconfigPath)
137-
: (await fileExists(fallbackTsconfigPath))
138-
? await jitiOptionsFromTsConfig(tsconfigPath)
139-
: {};
135+
136+
const validPath: null | string =
137+
tsconfigPath == null
138+
? (await fileExists(fallbackTsconfigPath))
139+
? fallbackTsconfigPath
140+
: null
141+
: tsconfigPath;
142+
143+
const tsDerivedJitiOptions: MappableJitiOptions = validPath
144+
? await jitiOptionsFromTsConfig(validPath)
145+
: {};
140146
return createJiti(filepath, { ...jitiOptions, ...tsDerivedJitiOptions });
141147
}
142148

@@ -147,26 +153,7 @@ export async function createTsJiti(
147153
export async function jitiOptionsFromTsConfig(
148154
tsconfigPath: string,
149155
): Promise<MappableJitiOptions> {
150-
const compilerOptions = await deriveTsConfig(tsconfigPath);
151-
const tsconfigDir = path.dirname(tsconfigPath);
152-
return parseTsConfigToJitiConfig(compilerOptions, tsconfigDir);
153-
}
154-
155-
/**
156-
* Read tsconfig file by path and return the parsed options as JSON object
157-
* @param tsconfigPath
158-
*/
159-
export async function deriveTsConfig(
160-
tsconfigPath: string,
161-
): Promise<CompilerOptions> {
162-
// check if tsconfig file exists
163-
const exists = await fileExists(tsconfigPath);
164-
if (!exists) {
165-
throw new Error(
166-
`Tsconfig file not found at path: ${tsconfigPath.replace(/\\/g, '/')}`,
167-
);
168-
}
169-
170156
const { options } = loadTargetConfig(tsconfigPath);
171157
return options;
158+
return parseTsConfigToJitiConfig(options, path.dirname(tsconfigPath));
172159
}

packages/utils/src/lib/import-module.unit.test.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { CompilerOptions } from 'typescript';
2-
import { describe, expect, it, vi } from 'vitest';
2+
import { describe, expect, it } from 'vitest';
33
import {
4-
deriveTsConfig,
54
mapTsPathsToJitiAlias,
65
parseTsConfigToJitiConfig,
76
} from './import-module.js';
@@ -108,11 +107,3 @@ describe('parseTsConfigToJitiConfig', () => {
108107
});
109108
});
110109
});
111-
112-
describe('deriveTsConfig', () => {
113-
it('throws error when tsconfig file does not exist', async () => {
114-
await expect(deriveTsConfig('missing.json')).rejects.toThrow(
115-
'Tsconfig file not found at path: missing.json',
116-
);
117-
});
118-
});

0 commit comments

Comments
 (0)