Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@nx/react": "22.3.3",
"@nx/vite": "22.3.3",
"@nx/workspace": "22.3.3",
"@push-based/jiti-tsc": "^0.0.2",
"@push-based/nx-verdaccio": "0.0.7",
"@swc-node/register": "1.9.2",
"@swc/cli": "0.6.0",
Expand Down Expand Up @@ -102,7 +103,7 @@
"husky": "^8.0.0",
"inquirer": "^9.3.7",
"jest-extended": "^6.0.0",
"jiti": "2.4.2",
"jiti": "^2.6.1",
"jsdom": "~24.0.0",
"jsonc-eslint-parser": "^2.4.0",
"knip": "^5.33.3",
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-typescript/src/lib/runner/ts-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import {
createProgram,
getPreEmitDiagnostics,
} from 'typescript';
import { logger, pluralizeToken, stringifyError } from '@code-pushup/utils';
import { loadTargetConfig } from './utils.js';
import {
loadTargetConfig,
logger,
pluralizeToken,
stringifyError,
} from '@code-pushup/utils';

export type DiagnosticsOptions = {
tsconfig: string;
Expand Down
30 changes: 0 additions & 30 deletions packages/plugin-typescript/src/lib/runner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import {
type Diagnostic,
DiagnosticCategory,
flattenDiagnosticMessageText,
parseJsonConfigFileContent,
readConfigFile,
sys,
} from 'typescript';
import type { Issue } from '@code-pushup/models';
import { truncateIssueMessage } from '@code-pushup/utils';
Expand Down Expand Up @@ -88,30 +85,3 @@ export function getIssueFromDiagnostic(diag: Diagnostic) {
},
} satisfies Issue;
}

export function loadTargetConfig(tsConfigPath: string) {
const resolvedConfigPath = path.resolve(tsConfigPath);
const { config, error } = readConfigFile(resolvedConfigPath, sys.readFile);

if (error) {
throw new Error(
`Error reading TypeScript config file at ${tsConfigPath}:\n${error.messageText}`,
);
}

const parsedConfig = parseJsonConfigFileContent(
config,
sys,
path.dirname(resolvedConfigPath),
{},
resolvedConfigPath,
);

if (parsedConfig.fileNames.length === 0) {
throw new Error(
'No files matched by the TypeScript configuration. Check your "include", "exclude" or "files" settings.',
);
}

return parsedConfig;
}
3 changes: 3 additions & 0 deletions packages/utils/mocks/fixtures/tsconfig-setup/import-alias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { name } from '@utils';

export default `valid-ts-default-export-${name}`;
8 changes: 8 additions & 0 deletions packages/utils/mocks/fixtures/tsconfig-setup/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"paths": {
"@utils/*": ["./utils.ts"]
}
},
"include": ["*.ts"]
}
1 change: 1 addition & 0 deletions packages/utils/mocks/fixtures/tsconfig-setup/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = 'utils-export';
5 changes: 3 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
"@code-pushup/models": "0.108.1",
"ansis": "^3.3.0",
"build-md": "^0.4.2",
"bundle-require": "^5.1.0",
"esbuild": "^0.25.2",
"ora": "^9.0.0",
"semver": "^7.6.0",
"simple-git": "^3.20.0",
"string-width": "^8.1.0",
"wrap-ansi": "^9.0.2",
"zod": "^4.2.1"
"zod": "^4.2.1",
"typescript": "5.7.3",
"jiti": "^2.4.2"
},
"files": [
"src",
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export {
type ProcessObserver,
type ProcessResult,
} from './lib/execute-process.js';
export { loadTargetConfig } from './lib/load-ts-config.js';
export {
crawlFileSystem,
createReportPath,
Expand All @@ -41,7 +42,6 @@ export {
filePathToCliArg,
findLineNumberInText,
findNearestFile,
importModule,
pluginWorkDir,
projectToFilename,
readJsonFile,
Expand Down Expand Up @@ -178,3 +178,4 @@ export type {
Prettify,
WithRequired,
} from './lib/types.js';
export * from './lib/import-module.js';
19 changes: 0 additions & 19 deletions packages/utils/src/lib/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { type Options, bundleRequire } from 'bundle-require';
import { mkdir, readFile, readdir, rm, stat } from 'node:fs/promises';
import path from 'node:path';
import type { Format, PersistConfig } from '@code-pushup/models';
import { logger } from './logger.js';
import { settlePromise } from './promises.js';

export async function readTextFile(filePath: string): Promise<string> {
const buffer = await readFile(filePath);
Expand Down Expand Up @@ -52,23 +50,6 @@ export async function removeDirectoryIfExists(dir: string) {
}
}

export async function importModule<T = unknown>(options: Options): Promise<T> {
const resolvedStats = await settlePromise(stat(options.filepath));
if (resolvedStats.status === 'rejected') {
throw new Error(`File '${options.filepath}' does not exist`);
}
if (!resolvedStats.value.isFile()) {
throw new Error(`Expected '${options.filepath}' to be a file`);
}

const { mod } = await bundleRequire<object>(options);

if (typeof mod === 'object' && 'default' in mod) {
return mod.default as T;
}
return mod as T;
}

export function createReportPath({
outputDir,
filename,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import { importModule } from './file-system.js';
import { describe, expect, it, vi } from 'vitest';
import { importModule } from './import-module.js';

describe('importModule', () => {
const mockDir = path.join(
Expand Down Expand Up @@ -45,6 +45,25 @@ describe('importModule', () => {
).resolves.toBe('valid-ts-default-export');
});

it('imports module with default tsconfig when tsconfig undefined', async () => {
vi.clearAllMocks();
await expect(
importModule({
filepath: path.join(mockDir, 'valid-ts-default-export.ts'),
}),
).resolves.toBe('valid-ts-default-export');
});

it('imports module with custom tsconfig', async () => {
vi.clearAllMocks();
await expect(
importModule({
filepath: path.join(mockDir, 'tsconfig-setup', 'import-alias.ts'),
tsconfig: path.join(mockDir, 'tsconfig-setup', 'tsconfig.json'),
}),
).resolves.toBe('valid-ts-default-export-utils-export');
});

it('should throw if the file does not exist', async () => {
await expect(
importModule({ filepath: 'path/to/non-existent-export.mjs' }),
Expand All @@ -57,11 +76,9 @@ describe('importModule', () => {
);
});

it('should throw if file is not valid JS', async () => {
it('should load valid JSON', async () => {
await expect(
importModule({ filepath: path.join(mockDir, 'invalid-js-file.json') }),
).rejects.toThrow(
`${path.join(mockDir, 'invalid-js-file.json')} is not a valid JS file`,
);
).resolves.toStrictEqual({ key: 'value' });
});
});
Loading
Loading