From 261ef43443907899da7fe89796d7eb2d4b0322e1 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Sat, 30 Sep 2023 11:27:27 +0900 Subject: [PATCH] feat!: rewrite env features (#183) --- packages/shared-lib-node/src/env.ts | 6 +++--- .../shared-lib-node/test-fixtures/app1/.env | 1 + .../{app => app1}/.env.development | 0 .../{app => app1}/.env.production | 0 .../test-fixtures/{app => app1}/.env.test | 0 .../shared-lib-node/test-fixtures/app2/.env | 1 + .../{another-app => app2}/.env.development | 0 .../{another-app => app2}/.env.production | 0 .../{another-app => app2}/.env.test | 0 packages/shared-lib-node/tests/env.test.ts | 16 ++++++++-------- packages/wb/src/commands/setup.ts | 4 ++-- packages/wb/src/commands/start.ts | 19 +++++++++++++------ packages/wb/src/index.ts | 4 ++-- .../scripts/execution/baseExecutionScripts.ts | 8 +++++--- .../scripts/execution/httpServerScripts.ts | 6 ++++-- .../wb/src/scripts/execution/remixScripts.ts | 8 +++----- 16 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 packages/shared-lib-node/test-fixtures/app1/.env rename packages/shared-lib-node/test-fixtures/{app => app1}/.env.development (100%) rename packages/shared-lib-node/test-fixtures/{app => app1}/.env.production (100%) rename packages/shared-lib-node/test-fixtures/{app => app1}/.env.test (100%) create mode 100644 packages/shared-lib-node/test-fixtures/app2/.env rename packages/shared-lib-node/test-fixtures/{another-app => app2}/.env.development (100%) rename packages/shared-lib-node/test-fixtures/{another-app => app2}/.env.production (100%) rename packages/shared-lib-node/test-fixtures/{another-app => app2}/.env.test (100%) diff --git a/packages/shared-lib-node/src/env.ts b/packages/shared-lib-node/src/env.ts index 406be7d2..072dac0c 100644 --- a/packages/shared-lib-node/src/env.ts +++ b/packages/shared-lib-node/src/env.ts @@ -26,7 +26,7 @@ export const yargsOptionsBuilderForEnv = { type: 'boolean', }, 'auto-cascade-env': { - description: 'Same with --cascade-env=.', + description: 'Same with --cascade-env=.', type: 'boolean', default: true, }, @@ -47,7 +47,7 @@ export function loadEnvironmentVariables(argv: Options, cwd: string, orgCwd?: st (argv.cascadeNodeEnv ? process.env.NODE_ENV || 'development' : argv.autoCascadeEnv - ? process.env.WB_ENV || process.env.APP_ENV || process.env.NODE_ENV || 'development' + ? process.env.WB_ENV || process.env.NODE_ENV || 'development' : undefined); if (typeof cascade === 'string') { if (envPaths.length === 0) envPaths.push(path.join(cwd, '.env')); @@ -74,7 +74,7 @@ export function loadEnvironmentVariables(argv: Options, cwd: string, orgCwd?: st } } if (count > 0) { - console.info(`Updated ${count} environment variables:`, envPath); + console.info(`Loaded ${count} environment variables:`, envPath); } } diff --git a/packages/shared-lib-node/test-fixtures/app1/.env b/packages/shared-lib-node/test-fixtures/app1/.env new file mode 100644 index 00000000..b9759296 --- /dev/null +++ b/packages/shared-lib-node/test-fixtures/app1/.env @@ -0,0 +1 @@ +NAME=app1 diff --git a/packages/shared-lib-node/test-fixtures/app/.env.development b/packages/shared-lib-node/test-fixtures/app1/.env.development similarity index 100% rename from packages/shared-lib-node/test-fixtures/app/.env.development rename to packages/shared-lib-node/test-fixtures/app1/.env.development diff --git a/packages/shared-lib-node/test-fixtures/app/.env.production b/packages/shared-lib-node/test-fixtures/app1/.env.production similarity index 100% rename from packages/shared-lib-node/test-fixtures/app/.env.production rename to packages/shared-lib-node/test-fixtures/app1/.env.production diff --git a/packages/shared-lib-node/test-fixtures/app/.env.test b/packages/shared-lib-node/test-fixtures/app1/.env.test similarity index 100% rename from packages/shared-lib-node/test-fixtures/app/.env.test rename to packages/shared-lib-node/test-fixtures/app1/.env.test diff --git a/packages/shared-lib-node/test-fixtures/app2/.env b/packages/shared-lib-node/test-fixtures/app2/.env new file mode 100644 index 00000000..59707862 --- /dev/null +++ b/packages/shared-lib-node/test-fixtures/app2/.env @@ -0,0 +1 @@ +NAME=app2 diff --git a/packages/shared-lib-node/test-fixtures/another-app/.env.development b/packages/shared-lib-node/test-fixtures/app2/.env.development similarity index 100% rename from packages/shared-lib-node/test-fixtures/another-app/.env.development rename to packages/shared-lib-node/test-fixtures/app2/.env.development diff --git a/packages/shared-lib-node/test-fixtures/another-app/.env.production b/packages/shared-lib-node/test-fixtures/app2/.env.production similarity index 100% rename from packages/shared-lib-node/test-fixtures/another-app/.env.production rename to packages/shared-lib-node/test-fixtures/app2/.env.production diff --git a/packages/shared-lib-node/test-fixtures/another-app/.env.test b/packages/shared-lib-node/test-fixtures/app2/.env.test similarity index 100% rename from packages/shared-lib-node/test-fixtures/another-app/.env.test rename to packages/shared-lib-node/test-fixtures/app2/.env.test diff --git a/packages/shared-lib-node/tests/env.test.ts b/packages/shared-lib-node/tests/env.test.ts index 138751ae..d0c98f7d 100644 --- a/packages/shared-lib-node/tests/env.test.ts +++ b/packages/shared-lib-node/tests/env.test.ts @@ -9,39 +9,39 @@ describe('loadEnvironmentVariables()', () => { }); it('should load no env vars with empty options', () => { - const envVars = loadEnvironmentVariables({}, 'test-fixtures/app'); + const envVars = loadEnvironmentVariables({}, 'test-fixtures/app1'); expect(envVars).toEqual({}); }); it('should load env vars with --auto-cascade-env', () => { - const envVars = loadEnvironmentVariables({ autoCascadeEnv: true }, 'test-fixtures/app'); + const envVars = loadEnvironmentVariables({ autoCascadeEnv: true }, 'test-fixtures/app1'); expect(envVars).toEqual({ NAME: 'app1', ENV: 'development1' }); }); it('should load env vars with --cascade-env=production', () => { - const envVars = loadEnvironmentVariables({ cascadeEnv: 'production', env: ['.env'] }, 'test-fixtures/app'); + const envVars = loadEnvironmentVariables({ cascadeEnv: 'production', env: ['.env'] }, 'test-fixtures/app1'); expect(envVars).toEqual({ NAME: 'app1', ENV: 'production1' }); }); it('should load env vars with --cascade-node-env and NODE_ENV=""', () => { process.env.NODE_ENV = ''; - const envVars = loadEnvironmentVariables({ cascadeNodeEnv: true, env: ['.env'] }, 'test-fixtures/app'); + const envVars = loadEnvironmentVariables({ cascadeNodeEnv: true, env: ['.env'] }, 'test-fixtures/app1'); expect(envVars).toEqual({ NAME: 'app1', ENV: 'development1' }); }); it('should load env vars with --cascade-node-env and NODE_ENV=test', () => { process.env.NODE_ENV = 'test'; - const envVars = loadEnvironmentVariables({ cascadeNodeEnv: true, env: ['.env'] }, 'test-fixtures/app'); + const envVars = loadEnvironmentVariables({ cascadeNodeEnv: true, env: ['.env'] }, 'test-fixtures/app1'); expect(envVars).toEqual({ NAME: 'app1', ENV: 'test1' }); }); - it('should load env vars with --env=test-fixtures/another-app/.env --auto-cascade-env, WB_ENV=test and NODE_ENV=production', () => { + it('should load env vars with --env=test-fixtures/app2/.env --auto-cascade-env, WB_ENV=test and NODE_ENV=production', () => { process.env.WB_ENV = 'test'; process.env.NODE_ENV = 'production'; const envVars = loadEnvironmentVariables( { autoCascadeEnv: true, env: ['.env'] }, - 'test-fixtures/app', - 'test-fixtures/another-app' + 'test-fixtures/app1', + 'test-fixtures/app2' ); expect(envVars).toEqual({ NAME: 'app2', ENV: 'test2' }); }); diff --git a/packages/wb/src/commands/setup.ts b/packages/wb/src/commands/setup.ts index 85d47944..474ebe0f 100644 --- a/packages/wb/src/commands/setup.ts +++ b/packages/wb/src/commands/setup.ts @@ -53,9 +53,9 @@ export async function setup(argv: Partial