Skip to content

Commit 3a94a8e

Browse files
authored
feat: support configFilePath in context (#130)
1 parent 4901462 commit 3a94a8e

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

Diff for: packages/build-scripts/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "build-scripts",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"license": "MIT",
55
"description": "scripts core",
66
"main": "lib/index.js",

Diff for: packages/build-scripts/src/Context.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import type {
4848
EmptyObject,
4949
} from './types.js';
5050
import type { Config } from '@jest/types';
51-
import { getUserConfig } from './utils/loadConfig.js';
51+
import { getUserConfig, resolveConfigFile } from './utils/loadConfig.js';
5252
import loadPkg from './utils/loadPkg.js';
5353
import { createLogger } from './utils/logger.js';
5454
import resolvePlugins from './utils/resolvePlugins.js';
@@ -95,6 +95,8 @@ class Context<T = {}, U = EmptyObject, K = EmptyObject> {
9595

9696
configFile: string | string[];
9797

98+
configFilePath: string;
99+
98100
private options: ContextOptions<U>;
99101

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

191193
resolveUserConfig = async (): Promise<UserConfig<K>> => {
192194
if (!this.userConfig) {
195+
this.configFilePath = await resolveConfigFile(this.configFile, this.commandArgs, this.rootDir);
193196
this.userConfig = await getUserConfig<K>({
194197
rootDir: this.rootDir,
195198
commandArgs: this.commandArgs,
196199
pkg: this.pkg,
197200
logger: this.logger,
198-
configFile: this.configFile,
201+
configFilePath: this.configFilePath,
199202
});
200203
}
201204
return this.userConfig;

Diff for: packages/build-scripts/src/utils/constant.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const PLUGIN_CONTEXT_KEY = [
88
'originalUserConfig' as const,
99
'pkg' as const,
1010
'extendsPluginAPI' as const,
11+
'configFilePath' as const,
1112
];
1213

1314
export const VALIDATION_MAP = {

Diff for: packages/build-scripts/src/utils/loadConfig.ts

+22-18
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,41 @@ export const mergeModeConfig = <K> (mode: string, userConfig: UserConfig<K>): Us
4646
return userConfig;
4747
};
4848

49+
export const resolveConfigFile = async (configFile: string | string[], commandArgs: CommandArgs, rootDir: string) => {
50+
const { config } = commandArgs;
51+
let configPath = '';
52+
if (config) {
53+
configPath = path.isAbsolute(config)
54+
? config
55+
: path.resolve(rootDir, config);
56+
} else {
57+
const [defaultUserConfig] = await fg(configFile, { cwd: rootDir, absolute: true });
58+
configPath = defaultUserConfig;
59+
}
60+
return configPath;
61+
}
62+
4963
export const getUserConfig = async <K extends EmptyObject>({
5064
rootDir,
5165
commandArgs,
5266
logger,
5367
pkg,
54-
configFile,
68+
configFilePath,
5569
}: {
5670
rootDir: string;
5771
commandArgs: CommandArgs;
5872
pkg: Json;
5973
logger: CreateLoggerReturns;
60-
configFile: string | string[];
74+
configFilePath: string;
6175
}): Promise<UserConfig<K>> => {
62-
const { config } = commandArgs;
63-
let configPath = '';
64-
if (config) {
65-
configPath = path.isAbsolute(config)
66-
? config
67-
: path.resolve(rootDir, config);
68-
} else {
69-
const [defaultUserConfig] = await fg(configFile, { cwd: rootDir, absolute: true });
70-
configPath = defaultUserConfig;
71-
}
7276
let userConfig = {
7377
plugins: [] as PluginList,
7478
};
75-
if (configPath && fs.existsSync(configPath)) {
79+
if (configFilePath && fs.existsSync(configFilePath)) {
7680
try {
77-
userConfig = await loadConfig(configPath, pkg, logger);
81+
userConfig = await loadConfig(configFilePath, pkg, logger);
7882
} catch (err) {
79-
logger.warn(`Fail to load config file ${configPath}`);
83+
logger.warn(`Fail to load config file ${configFilePath}`);
8084

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

8791
process.exit(1);
8892
}
89-
} else if (configPath) {
93+
} else if (configFilePath) {
9094
// If path was not found
91-
logger.error(`config file${`(${configPath})` || ''} is not exist`);
95+
logger.error(`config file${`(${configFilePath})` || ''} is not exist`);
9296
process.exit(1);
9397
} else {
9498
logger.debug(
@@ -164,4 +168,4 @@ async function executeTypescriptModule(code: string, filePath: string, isEsm = t
164168
fs.unlinkSync(tempFile);
165169

166170
return userConfig;
167-
}
171+
}

0 commit comments

Comments
 (0)