From 08b5d337a6c063bb07aa18667c3e528568b36d7c Mon Sep 17 00:00:00 2001 From: otakustay Date: Mon, 18 Dec 2023 11:20:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(build):=20=E5=A2=9E=E5=8A=A0--env-file?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E6=96=87=E4=BB=B6=20(#315)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli-build/src/index.ts | 2 +- packages/cli-dev/src/index.ts | 2 +- packages/cli-play/src/index.ts | 2 +- packages/cli/src/BuildCommand.ts | 6 ++++++ packages/cli/src/DevCommand.ts | 6 ++++++ packages/cli/src/PlayCommand.ts | 6 ++++++ packages/cli/src/TestCommand.ts | 6 ++++++ packages/core/src/env.ts | 7 ++++++- packages/settings/src/interface/command.ts | 4 ++++ site/docs/advanced/custom-env.md | 8 ++++++++ site/docs/cli/build.md | 1 + site/docs/cli/dev.md | 1 + site/docs/cli/play.md | 1 + site/docs/cli/test.md | 1 + 14 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/cli-build/src/index.ts b/packages/cli-build/src/index.ts index d17418c2..55caac6a 100644 --- a/packages/cli-build/src/index.ts +++ b/packages/cli-build/src/index.ts @@ -75,7 +75,7 @@ const build = async (cmd: BuildCommandLineArgs, projectSettings: ProjectSettings export const run = async (cmd: BuildCommandLineArgs): Promise => { 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'); diff --git a/packages/cli-dev/src/index.ts b/packages/cli-dev/src/index.ts index b584f550..74e20f3f 100644 --- a/packages/cli-dev/src/index.ts +++ b/packages/cli-dev/src/index.ts @@ -48,7 +48,7 @@ const createStart = async (cmd: DevCommandLineArgs, projectSettings: ProjectSett export const run = async (cmd: DevCommandLineArgs): Promise => { 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); diff --git a/packages/cli-play/src/index.ts b/packages/cli-play/src/index.ts index 4d379854..68e5b828 100644 --- a/packages/cli-play/src/index.ts +++ b/packages/cli-play/src/index.ts @@ -83,7 +83,7 @@ const collectBuildContext = async (settings: S, cmd: export const run = async (cmd: PlayCommandLineArgs, target: string): Promise => { process.env.NODE_ENV = 'development'; - await prepareEnvironment(cmd.cwd, 'development'); + await prepareEnvironment(cmd.cwd, 'development', cmd.envFiles); const projectSettings = await buildProjectSettings(cmd, target); diff --git a/packages/cli/src/BuildCommand.ts b/packages/cli/src/BuildCommand.ts index 429a4bd5..2c64d45c 100644 --- a/packages/cli/src/BuildCommand.ts +++ b/packages/cli/src/BuildCommand.ts @@ -29,6 +29,11 @@ export default class BuildCommand extends DynamicImportCommand {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', @@ -72,6 +77,7 @@ export default class DevCommand extends DynamicImportCommand cwd: this.cwd, mode: this.mode, configFile: this.configFile, + envFiles: this.envFiles, srcDirectory: this.srcDirectory, entriesDirectory: this.entriesDirectory, buildTarget: this.buildTarget, diff --git a/packages/cli/src/PlayCommand.ts b/packages/cli/src/PlayCommand.ts index 68333f60..85691096 100644 --- a/packages/cli/src/PlayCommand.ts +++ b/packages/cli/src/PlayCommand.ts @@ -19,6 +19,11 @@ export default class LintCommand extends DynamicImportCommand( '--target', 'node', @@ -34,6 +39,7 @@ export default class LintCommand extends DynamicImportCommand { +export const prepareEnvironment = async (cwd: string, mode: WorkMode, custom: string[] | undefined) => { const files = [ path.join(cwd, `.env.${mode}.local`), path.join(cwd, '.env.local'), @@ -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})); diff --git a/packages/settings/src/interface/command.ts b/packages/settings/src/interface/command.ts index 099e2fc8..10324808 100644 --- a/packages/settings/src/interface/command.ts +++ b/packages/settings/src/interface/command.ts @@ -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; @@ -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; @@ -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; @@ -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[]; } diff --git a/site/docs/advanced/custom-env.md b/site/docs/advanced/custom-env.md index ff5857a7..bdd23aab 100644 --- a/site/docs/advanced/custom-env.md +++ b/site/docs/advanced/custom-env.md @@ -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`文件提交到远程仓库中。 ::: diff --git a/site/docs/cli/build.md b/site/docs/cli/build.md index 81ebdb65..3ad3d543 100644 --- a/site/docs/cli/build.md +++ b/site/docs/cli/build.md @@ -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] 只构建指定的特性名称,其它的特性名称不参与构建 diff --git a/site/docs/cli/dev.md b/site/docs/cli/dev.md index 81245b03..1f4860c8 100644 --- a/site/docs/cli/dev.md +++ b/site/docs/cli/dev.md @@ -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配置 diff --git a/site/docs/cli/play.md b/site/docs/cli/play.md index 7fdd29a3..47de91e9 100644 --- a/site/docs/cli/play.md +++ b/site/docs/cli/play.md @@ -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 diff --git a/site/docs/cli/test.md b/site/docs/cli/test.md index cd4587a9..8d78d844 100644 --- a/site/docs/cli/test.md +++ b/site/docs/cli/test.md @@ -11,6 +11,7 @@ title: 执行单元测试 ``` --cwd [value] 指定当前工作目录,默认为当前目录 --config [value] 使用指定的配置文件,默认查找reskript.config.{ts,mjs} +--env-file [value...] 提供一个或多个自定义的环境变量(.env)文件 --target [value] 指定单测的运行时环境,可以为react或node,默认是node -h, --help 显示帮助信息 ```