Skip to content

Commit

Permalink
feat(cli): add --tsconfig option
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Feb 19, 2024
1 parent 474625c commit 67abfb7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ export async function coreConfigMiddleware<
const args = processArgs;
const {
config,
tsconfig,
persist: cliPersist,
upload: cliUpload,
...remainingCliOptions
} = args as GeneralCliOptions & Required<CoreConfig>;
// if config path is given use it otherwise auto-load
const importedRc = config ? await readRcByPath(config) : await autoloadRc();
const importedRc = config
? await readRcByPath(config, tsconfig)
: await autoloadRc(tsconfig);
const {
persist: rcPersist,
upload: rcUpload,
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/lib/implementation/core-config.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export type UploadConfigCliOptions = {
};
/* eslint-enable @typescript-eslint/naming-convention */

export type ConfigCliOptions = { config: string };
export type ConfigCliOptions = {
config: string;
tsconfig?: string;
};

export type CoreConfigCliOptions = ConfigCliOptions &
PersistConfigCliOptions &
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/lib/implementation/global.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export function yargsGlobalOptionsDefinition(): Record<
},
config: {
describe:
'Path the the config file, e.g. code-pushup.config.ts. By default it loads code-pushup.config.(ts|mjs|js).',
'Path to config file, e.g. code-pushup.config.ts. By default it loads code-pushup.config.(ts|mjs|js).',
type: 'string',
},
tsconfig: {
describe:
'Path to a TypeScript config, to be used when loading config file.',
type: 'string',
},
};
Expand Down
14 changes: 10 additions & 4 deletions packages/core/src/lib/implementation/read-rc-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export class ConfigPathError extends Error {
}
}

export async function readRcByPath(filepath: string): Promise<CoreConfig> {
export async function readRcByPath(
filepath: string,
tsconfig?: string,
): Promise<CoreConfig> {
if (filepath.length === 0) {
throw new Error('The path to the configuration file is empty.');
}
Expand All @@ -22,12 +25,12 @@ export async function readRcByPath(filepath: string): Promise<CoreConfig> {
throw new ConfigPathError(filepath);
}

const cfg = await importEsmModule({ filepath });
const cfg = await importEsmModule({ filepath, tsconfig });

return coreConfigSchema.parse(cfg);
}

export async function autoloadRc(): Promise<CoreConfig> {
export async function autoloadRc(tsconfig?: string): Promise<CoreConfig> {
// eslint-disable-next-line functional/no-let
let ext = '';
// eslint-disable-next-line functional/no-loop-statements
Expand All @@ -49,5 +52,8 @@ export async function autoloadRc(): Promise<CoreConfig> {
);
}

return readRcByPath(join(process.cwd(), `${CONFIG_FILE_NAME}.${ext}`));
return readRcByPath(
join(process.cwd(), `${CONFIG_FILE_NAME}.${ext}`),
tsconfig,
);
}
4 changes: 2 additions & 2 deletions packages/core/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type GlobalOptions = {
// 'Show progress bar in stdout'
// Show progress bar in stdout
progress: boolean;
// Outputs additional information for a run'
// Outputs additional information for a run
verbose: boolean;
};

0 comments on commit 67abfb7

Please sign in to comment.