Skip to content

Commit

Permalink
feat: support configFilePath in context (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClarkXia authored Apr 2, 2024
1 parent 4901462 commit 3a94a8e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/build-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "build-scripts",
"version": "2.1.1",
"version": "2.1.2",
"license": "MIT",
"description": "scripts core",
"main": "lib/index.js",
Expand Down
7 changes: 5 additions & 2 deletions packages/build-scripts/src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import type {
EmptyObject,
} from './types.js';
import type { Config } from '@jest/types';
import { getUserConfig } from './utils/loadConfig.js';
import { getUserConfig, resolveConfigFile } from './utils/loadConfig.js';
import loadPkg from './utils/loadPkg.js';
import { createLogger } from './utils/logger.js';
import resolvePlugins from './utils/resolvePlugins.js';
Expand Down Expand Up @@ -95,6 +95,8 @@ class Context<T = {}, U = EmptyObject, K = EmptyObject> {

configFile: string | string[];

configFilePath: string;

private options: ContextOptions<U>;

// 存放 config 配置的数组
Expand Down Expand Up @@ -190,12 +192,13 @@ class Context<T = {}, U = EmptyObject, K = EmptyObject> {

resolveUserConfig = async (): Promise<UserConfig<K>> => {
if (!this.userConfig) {
this.configFilePath = await resolveConfigFile(this.configFile, this.commandArgs, this.rootDir);
this.userConfig = await getUserConfig<K>({
rootDir: this.rootDir,
commandArgs: this.commandArgs,
pkg: this.pkg,
logger: this.logger,
configFile: this.configFile,
configFilePath: this.configFilePath,
});
}
return this.userConfig;
Expand Down
1 change: 1 addition & 0 deletions packages/build-scripts/src/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const PLUGIN_CONTEXT_KEY = [
'originalUserConfig' as const,
'pkg' as const,
'extendsPluginAPI' as const,
'configFilePath' as const,
];

export const VALIDATION_MAP = {
Expand Down
40 changes: 22 additions & 18 deletions packages/build-scripts/src/utils/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,41 @@ export const mergeModeConfig = <K> (mode: string, userConfig: UserConfig<K>): Us
return userConfig;
};

export const resolveConfigFile = async (configFile: string | string[], commandArgs: CommandArgs, rootDir: string) => {
const { config } = commandArgs;
let configPath = '';
if (config) {
configPath = path.isAbsolute(config)
? config
: path.resolve(rootDir, config);
} else {
const [defaultUserConfig] = await fg(configFile, { cwd: rootDir, absolute: true });
configPath = defaultUserConfig;
}
return configPath;
}

export const getUserConfig = async <K extends EmptyObject>({
rootDir,
commandArgs,
logger,
pkg,
configFile,
configFilePath,
}: {
rootDir: string;
commandArgs: CommandArgs;
pkg: Json;
logger: CreateLoggerReturns;
configFile: string | string[];
configFilePath: string;
}): Promise<UserConfig<K>> => {
const { config } = commandArgs;
let configPath = '';
if (config) {
configPath = path.isAbsolute(config)
? config
: path.resolve(rootDir, config);
} else {
const [defaultUserConfig] = await fg(configFile, { cwd: rootDir, absolute: true });
configPath = defaultUserConfig;
}
let userConfig = {
plugins: [] as PluginList,
};
if (configPath && fs.existsSync(configPath)) {
if (configFilePath && fs.existsSync(configFilePath)) {
try {
userConfig = await loadConfig(configPath, pkg, logger);
userConfig = await loadConfig(configFilePath, pkg, logger);
} catch (err) {
logger.warn(`Fail to load config file ${configPath}`);
logger.warn(`Fail to load config file ${configFilePath}`);

if (err instanceof Error) {
logger.error(err.stack);
Expand All @@ -86,9 +90,9 @@ export const getUserConfig = async <K extends EmptyObject>({

process.exit(1);
}
} else if (configPath) {
} else if (configFilePath) {
// If path was not found
logger.error(`config file${`(${configPath})` || ''} is not exist`);
logger.error(`config file${`(${configFilePath})` || ''} is not exist`);
process.exit(1);
} else {
logger.debug(
Expand Down Expand Up @@ -164,4 +168,4 @@ async function executeTypescriptModule(code: string, filePath: string, isEsm = t
fs.unlinkSync(tempFile);

return userConfig;
}
}

0 comments on commit 3a94a8e

Please sign in to comment.