Skip to content

Commit 08b5d33

Browse files
committed
feat(build): 增加--env-file参数支持自定义环境变量文件 (#315)
1 parent 7778d13 commit 08b5d33

File tree

14 files changed

+49
-4
lines changed

14 files changed

+49
-4
lines changed

packages/cli-build/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const build = async (cmd: BuildCommandLineArgs, projectSettings: ProjectSettings
7575
export const run = async (cmd: BuildCommandLineArgs): Promise<void> => {
7676
const {cwd, mode, configFile} = cmd;
7777
process.env.NODE_ENV = mode;
78-
await prepareEnvironment(cwd, mode);
78+
await prepareEnvironment(cwd, mode, cmd.envFiles);
7979

8080
if (cmd.analyze && !cmd.buildTarget) {
8181
logger.error('--analyze must be used with --build-target to specify only one target');

packages/cli-dev/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const createStart = async (cmd: DevCommandLineArgs, projectSettings: ProjectSett
4848

4949
export const run = async (cmd: DevCommandLineArgs): Promise<void> => {
5050
process.env.NODE_ENV = cmd.mode;
51-
await prepareEnvironment(cmd.cwd, cmd.mode);
51+
await prepareEnvironment(cmd.cwd, cmd.mode, cmd.envFiles);
5252

5353
const projectSettings = await readProjectSettings({commandName: 'dev', specifiedFile: cmd.configFile, ...cmd});
5454
const start = await createStart(cmd, projectSettings);

packages/cli-play/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const collectBuildContext = async <S extends ProjectSettings>(settings: S, cmd:
8383

8484
export const run = async (cmd: PlayCommandLineArgs, target: string): Promise<void> => {
8585
process.env.NODE_ENV = 'development';
86-
await prepareEnvironment(cmd.cwd, 'development');
86+
await prepareEnvironment(cmd.cwd, 'development', cmd.envFiles);
8787

8888
const projectSettings = await buildProjectSettings(cmd, target);
8989

packages/cli/src/BuildCommand.ts

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export default class BuildCommand extends DynamicImportCommand<BuildCommandLineA
2929
{description: 'specify a custom configuration file, default to "reskript.config.{ts|mjs}"'}
3030
);
3131

32+
envFiles = Option.Array(
33+
'--env-file',
34+
{description: 'Expand custom .env files to override built-in ones'}
35+
);
36+
3237
srcDirectory = Option.String(
3338
'--src-dir',
3439
'src',
@@ -73,6 +78,7 @@ export default class BuildCommand extends DynamicImportCommand<BuildCommandLineA
7378
cwd: this.cwd,
7479
mode: this.mode,
7580
configFile: this.configFile,
81+
envFiles: this.envFiles,
7682
srcDirectory: this.srcDirectory,
7783
entriesDirectory: this.entriesDirectory,
7884
buildTarget: this.buildTarget,

packages/cli/src/DevCommand.ts

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export default class DevCommand extends DynamicImportCommand<DevCommandLineArgs>
2929
{description: 'specify a custom configuration file, default to "reskript.config.{ts|mjs}"'}
3030
);
3131

32+
envFiles = Option.Array(
33+
'--env-file',
34+
{description: 'Expand custom .env files to override built-in ones'}
35+
);
36+
3237
srcDirectory = Option.String(
3338
'--src-dir',
3439
'src',
@@ -72,6 +77,7 @@ export default class DevCommand extends DynamicImportCommand<DevCommandLineArgs>
7277
cwd: this.cwd,
7378
mode: this.mode,
7479
configFile: this.configFile,
80+
envFiles: this.envFiles,
7581
srcDirectory: this.srcDirectory,
7682
entriesDirectory: this.entriesDirectory,
7783
buildTarget: this.buildTarget,

packages/cli/src/PlayCommand.ts

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export default class LintCommand extends DynamicImportCommand<PlayCommandLineArg
1919
{description: 'specify a custom configuration file, default to "reskript.config.{ts|mjs}"'}
2020
);
2121

22+
envFiles = Option.Array(
23+
'--env-file',
24+
{description: 'Expand custom .env files to override built-in ones'}
25+
);
26+
2227
buildTarget = Option.String('--build-target', 'dev', {description: 'set build target, default to "dev"'});
2328

2429
port = Option.String(
@@ -46,6 +51,7 @@ export default class LintCommand extends DynamicImportCommand<PlayCommandLineArg
4651
return {
4752
cwd: this.cwd,
4853
configFile: this.configFile,
54+
envFiles: this.envFiles,
4955
buildTarget: this.buildTarget,
5056
port: this.port,
5157
host: this.host,

packages/cli/src/TestCommand.ts

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export default class LintCommand extends DynamicImportCommand<TestCommandLineArg
1919
{description: 'specify a custom configuration file, default to "reskript.config.{ts|mjs}"'}
2020
);
2121

22+
envFiles = Option.Array(
23+
'--env-file',
24+
{description: 'Expand custom .env files to override built-in ones'}
25+
);
26+
2227
target = Option.String<TestCommandLineArgs['target']>(
2328
'--target',
2429
'node',
@@ -34,6 +39,7 @@ export default class LintCommand extends DynamicImportCommand<TestCommandLineArg
3439
return {
3540
cwd: this.cwd,
3641
configFile: this.configFile,
42+
envFiles: this.envFiles,
3743
target: this.target,
3844
jestArgs: this.jestArgs,
3945
};

packages/core/src/env.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {expand} from 'dotenv-expand';
55
import {findMonorepoRoot, isMonorepo} from './project.js';
66
import {WorkMode} from './interface.js';
77

8-
export const prepareEnvironment = async (cwd: string, mode: WorkMode) => {
8+
export const prepareEnvironment = async (cwd: string, mode: WorkMode, custom: string[] | undefined) => {
99
const files = [
1010
path.join(cwd, `.env.${mode}.local`),
1111
path.join(cwd, '.env.local'),
@@ -24,6 +24,11 @@ export const prepareEnvironment = async (cwd: string, mode: WorkMode) => {
2424
);
2525
}
2626

27+
if (custom) {
28+
// 自定义的优先级最高,越往后的越高,所以正好要反过来
29+
files.unshift(...custom.slice().reverse().map(v => path.resolve(cwd, v)));
30+
}
31+
2732
for (const file of files) {
2833
if (existsSync(file)) {
2934
expand(env.config({path: file}));

packages/settings/src/interface/command.ts

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface BabelCommandLineArgs {
1515

1616
export interface BuildCommandLineArgs extends WorkModeAware {
1717
readonly configFile?: string;
18+
readonly envFiles?: string[];
1819
readonly srcDirectory: string;
1920
readonly entriesDirectory: string;
2021
readonly buildTarget?: string;
@@ -31,6 +32,7 @@ export interface BuildCommandLineArgs extends WorkModeAware {
3132

3233
export interface DevCommandLineArgs extends WorkModeAware {
3334
readonly configFile?: string;
35+
readonly envFiles?: string[];
3436
readonly srcDirectory: string;
3537
readonly entriesDirectory: string;
3638
readonly buildTarget: string;
@@ -53,6 +55,7 @@ export interface LintCommandLineArgs {
5355

5456
export interface PlayCommandLineArgs extends ProjectAware {
5557
readonly configFile?: string;
58+
readonly envFiles?: string[];
5659
readonly buildTarget: string;
5760
readonly port: number;
5861
readonly host: HostType;
@@ -64,6 +67,7 @@ export type TestTarget = 'react' | 'node';
6467

6568
export interface TestCommandLineArgs extends ProjectAware {
6669
readonly configFile?: string;
70+
readonly envFiles?: string[];
6771
readonly target: TestTarget;
6872
readonly jestArgs: string[];
6973
}

site/docs/advanced/custom-env.md

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ title: 管理自定义环境变量
2828
/packages/{package}/.env.{mode}.local
2929
```
3030

31+
除此之外,对于`build``dev``test``play`命令,你可以使用`--env-file`参数传递一个或多个自定义的环境变量文件:
32+
33+
```shell
34+
skr build --env-file=.env.team --env-file=.env.me
35+
```
36+
37+
使用`--env-file`传递的文件优先级高于内置的逻辑,多个`--env-file`传递文件越靠后的优先级越高。上面的代码将以`.env.me`为最高优先级,`.env.team`次之,再次之为内置的读取逻辑。
38+
3139
:::caution
3240
不要在`.env.*`文件中放置任何敏感信息,不要将`.env.*.local`文件提交到远程仓库中。
3341
:::

site/docs/cli/build.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ title: 构建应用
1212
--cwd [value] 指定当前工作目录,默认为当前目录
1313
--mode [value] 设置构建的环境模式,可以为development或production,默认为production
1414
--config [value] 使用指定的配置文件,默认查找reskript.config.{ts,mjs}
15+
--env-file [value...] 提供一个或多个自定义的环境变量(.env)文件
1516
--src-dir [value] 指定项目源码所在的目录,默认为src
1617
--build-target [value] 指定构建的目标特性名称,如果有这个参数,最后会生成一个index.html包含该特性集
1718
--feature-only [value] 只构建指定的特性名称,其它的特性名称不参与构建

site/docs/cli/dev.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ title: 本地调试
1212
--cwd [value] 指定当前工作目录,默认为当前目录
1313
--mode [value] 设置构建的环境模式,可以为development或production,默认为development
1414
--config [value] 使用指定的配置文件,默认查找reskript.config.{ts,mjs}
15+
--env-file [value...] 提供一个或多个自定义的环境变量(.env)文件
1516
--src-dir [value] 指定项目源码所在的目录,默认为src
1617
--build-target [value] 指定调试的特性名称,默认为dev
1718
--proxy-domain [domain] 设置后端API代理的目标地址,用来覆盖项目配置文件中的devServer.defaultProxyDomain配置

site/docs/cli/play.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ title: 调试单个组件
1111
```
1212
--cwd [value] 指定当前工作目录,默认为当前目录
1313
--config [value] 使用指定的配置文件,默认查找reskript.config.{ts,mjs}
14+
--env-file [value...] 提供一个或多个自定义的环境变量(.env)文件
1415
--build-target [value] 指定调试的特性名称,默认为dev
1516
--port [value] 指定监听的端口,默认使用9999端口
1617
--host [value] 设置默认的本地服务器域,可以用localhost、loopback(对应127.0.0.1)、ip(对应本机的局域网IP),也可以指定一个自定义的域名或IP

site/docs/cli/test.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ title: 执行单元测试
1111
```
1212
--cwd [value] 指定当前工作目录,默认为当前目录
1313
--config [value] 使用指定的配置文件,默认查找reskript.config.{ts,mjs}
14+
--env-file [value...] 提供一个或多个自定义的环境变量(.env)文件
1415
--target [value] 指定单测的运行时环境,可以为react或node,默认是node
1516
-h, --help 显示帮助信息
1617
```

0 commit comments

Comments
 (0)