Skip to content

Commit

Permalink
feat(build): 增加--env-file参数支持自定义环境变量文件 (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
otakustay committed Dec 30, 2023
1 parent 7778d13 commit 08b5d33
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/cli-build/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const build = async (cmd: BuildCommandLineArgs, projectSettings: ProjectSettings
export const run = async (cmd: BuildCommandLineArgs): Promise<void> => {
const {cwd, mode, configFile} = cmd;
process.env.NODE_ENV = mode;
await prepareEnvironment(cwd, mode);
await prepareEnvironment(cwd, mode, cmd.envFiles);

if (cmd.analyze && !cmd.buildTarget) {
logger.error('--analyze must be used with --build-target to specify only one target');
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-dev/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const createStart = async (cmd: DevCommandLineArgs, projectSettings: ProjectSett

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

const projectSettings = await readProjectSettings({commandName: 'dev', specifiedFile: cmd.configFile, ...cmd});
const start = await createStart(cmd, projectSettings);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-play/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const collectBuildContext = async <S extends ProjectSettings>(settings: S, cmd:

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

const projectSettings = await buildProjectSettings(cmd, target);

Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/BuildCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export default class BuildCommand extends DynamicImportCommand<BuildCommandLineA
{description: 'specify a custom configuration file, default to "reskript.config.{ts|mjs}"'}
);

envFiles = Option.Array(
'--env-file',
{description: 'Expand custom .env files to override built-in ones'}
);

srcDirectory = Option.String(
'--src-dir',
'src',
Expand Down Expand Up @@ -73,6 +78,7 @@ export default class BuildCommand extends DynamicImportCommand<BuildCommandLineA
cwd: this.cwd,
mode: this.mode,
configFile: this.configFile,
envFiles: this.envFiles,
srcDirectory: this.srcDirectory,
entriesDirectory: this.entriesDirectory,
buildTarget: this.buildTarget,
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/DevCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export default class DevCommand extends DynamicImportCommand<DevCommandLineArgs>
{description: 'specify a custom configuration file, default to "reskript.config.{ts|mjs}"'}
);

envFiles = Option.Array(
'--env-file',
{description: 'Expand custom .env files to override built-in ones'}
);

srcDirectory = Option.String(
'--src-dir',
'src',
Expand Down Expand Up @@ -72,6 +77,7 @@ export default class DevCommand extends DynamicImportCommand<DevCommandLineArgs>
cwd: this.cwd,
mode: this.mode,
configFile: this.configFile,
envFiles: this.envFiles,
srcDirectory: this.srcDirectory,
entriesDirectory: this.entriesDirectory,
buildTarget: this.buildTarget,
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/PlayCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default class LintCommand extends DynamicImportCommand<PlayCommandLineArg
{description: 'specify a custom configuration file, default to "reskript.config.{ts|mjs}"'}
);

envFiles = Option.Array(
'--env-file',
{description: 'Expand custom .env files to override built-in ones'}
);

buildTarget = Option.String('--build-target', 'dev', {description: 'set build target, default to "dev"'});

port = Option.String(
Expand Down Expand Up @@ -46,6 +51,7 @@ export default class LintCommand extends DynamicImportCommand<PlayCommandLineArg
return {
cwd: this.cwd,
configFile: this.configFile,
envFiles: this.envFiles,
buildTarget: this.buildTarget,
port: this.port,
host: this.host,
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/TestCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default class LintCommand extends DynamicImportCommand<TestCommandLineArg
{description: 'specify a custom configuration file, default to "reskript.config.{ts|mjs}"'}
);

envFiles = Option.Array(
'--env-file',
{description: 'Expand custom .env files to override built-in ones'}
);

target = Option.String<TestCommandLineArgs['target']>(
'--target',
'node',
Expand All @@ -34,6 +39,7 @@ export default class LintCommand extends DynamicImportCommand<TestCommandLineArg
return {
cwd: this.cwd,
configFile: this.configFile,
envFiles: this.envFiles,
target: this.target,
jestArgs: this.jestArgs,
};
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {expand} from 'dotenv-expand';
import {findMonorepoRoot, isMonorepo} from './project.js';
import {WorkMode} from './interface.js';

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

if (custom) {
// 自定义的优先级最高,越往后的越高,所以正好要反过来
files.unshift(...custom.slice().reverse().map(v => path.resolve(cwd, v)));
}

for (const file of files) {
if (existsSync(file)) {
expand(env.config({path: file}));
Expand Down
4 changes: 4 additions & 0 deletions packages/settings/src/interface/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface BabelCommandLineArgs {

export interface BuildCommandLineArgs extends WorkModeAware {
readonly configFile?: string;
readonly envFiles?: string[];
readonly srcDirectory: string;
readonly entriesDirectory: string;
readonly buildTarget?: string;
Expand All @@ -31,6 +32,7 @@ export interface BuildCommandLineArgs extends WorkModeAware {

export interface DevCommandLineArgs extends WorkModeAware {
readonly configFile?: string;
readonly envFiles?: string[];
readonly srcDirectory: string;
readonly entriesDirectory: string;
readonly buildTarget: string;
Expand All @@ -53,6 +55,7 @@ export interface LintCommandLineArgs {

export interface PlayCommandLineArgs extends ProjectAware {
readonly configFile?: string;
readonly envFiles?: string[];
readonly buildTarget: string;
readonly port: number;
readonly host: HostType;
Expand All @@ -64,6 +67,7 @@ export type TestTarget = 'react' | 'node';

export interface TestCommandLineArgs extends ProjectAware {
readonly configFile?: string;
readonly envFiles?: string[];
readonly target: TestTarget;
readonly jestArgs: string[];
}
Expand Down
8 changes: 8 additions & 0 deletions site/docs/advanced/custom-env.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ title: 管理自定义环境变量
/packages/{package}/.env.{mode}.local
```

除此之外,对于`build``dev``test``play`命令,你可以使用`--env-file`参数传递一个或多个自定义的环境变量文件:

```shell
skr build --env-file=.env.team --env-file=.env.me
```

使用`--env-file`传递的文件优先级高于内置的逻辑,多个`--env-file`传递文件越靠后的优先级越高。上面的代码将以`.env.me`为最高优先级,`.env.team`次之,再次之为内置的读取逻辑。

:::caution
不要在`.env.*`文件中放置任何敏感信息,不要将`.env.*.local`文件提交到远程仓库中。
:::
Expand Down
1 change: 1 addition & 0 deletions site/docs/cli/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ title: 构建应用
--cwd [value] 指定当前工作目录,默认为当前目录
--mode [value] 设置构建的环境模式,可以为development或production,默认为production
--config [value] 使用指定的配置文件,默认查找reskript.config.{ts,mjs}
--env-file [value...] 提供一个或多个自定义的环境变量(.env)文件
--src-dir [value] 指定项目源码所在的目录,默认为src
--build-target [value] 指定构建的目标特性名称,如果有这个参数,最后会生成一个index.html包含该特性集
--feature-only [value] 只构建指定的特性名称,其它的特性名称不参与构建
Expand Down
1 change: 1 addition & 0 deletions site/docs/cli/dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ title: 本地调试
--cwd [value] 指定当前工作目录,默认为当前目录
--mode [value] 设置构建的环境模式,可以为development或production,默认为development
--config [value] 使用指定的配置文件,默认查找reskript.config.{ts,mjs}
--env-file [value...] 提供一个或多个自定义的环境变量(.env)文件
--src-dir [value] 指定项目源码所在的目录,默认为src
--build-target [value] 指定调试的特性名称,默认为dev
--proxy-domain [domain] 设置后端API代理的目标地址,用来覆盖项目配置文件中的devServer.defaultProxyDomain配置
Expand Down
1 change: 1 addition & 0 deletions site/docs/cli/play.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ title: 调试单个组件
```
--cwd [value] 指定当前工作目录,默认为当前目录
--config [value] 使用指定的配置文件,默认查找reskript.config.{ts,mjs}
--env-file [value...] 提供一个或多个自定义的环境变量(.env)文件
--build-target [value] 指定调试的特性名称,默认为dev
--port [value] 指定监听的端口,默认使用9999端口
--host [value] 设置默认的本地服务器域,可以用localhost、loopback(对应127.0.0.1)、ip(对应本机的局域网IP),也可以指定一个自定义的域名或IP
Expand Down
1 change: 1 addition & 0 deletions site/docs/cli/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ title: 执行单元测试
```
--cwd [value] 指定当前工作目录,默认为当前目录
--config [value] 使用指定的配置文件,默认查找reskript.config.{ts,mjs}
--env-file [value...] 提供一个或多个自定义的环境变量(.env)文件
--target [value] 指定单测的运行时环境,可以为react或node,默认是node
-h, --help 显示帮助信息
```
Expand Down

0 comments on commit 08b5d33

Please sign in to comment.