Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: rewrite env features #183

Merged
merged 7 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/shared-lib-node/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const yargsOptionsBuilderForEnv = {
type: 'boolean',
},
'auto-cascade-env': {
description: 'Same with --cascade-env=<WB_ENV || APP_ENV || NODE_ENV || "development">.',
description: 'Same with --cascade-env=<WB_ENV || NODE_ENV || "development">.',
type: 'boolean',
default: true,
},
Expand All @@ -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'));
Expand All @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/shared-lib-node/test-fixtures/app1/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NAME=app1
1 change: 1 addition & 0 deletions packages/shared-lib-node/test-fixtures/app2/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NAME=app2
16 changes: 8 additions & 8 deletions packages/shared-lib-node/tests/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
});
Expand Down
4 changes: 2 additions & 2 deletions packages/wb/src/commands/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export async function setup(argv: Partial<ArgumentsCamelCase<InferredOptionTypes
const newDevDeps: string[] = [];
if (deps['blitz'] || deps['next']) {
newDeps.push('pm2');
newDevDeps.push('concurrently', 'dotenv-cli', 'open-cli', 'vitest', 'wait-on');
newDevDeps.push('concurrently', 'open-cli', 'vitest', 'wait-on');
} else if (devDeps['@remix-run/dev']) {
newDeps.push('dotenv-cli', 'pm2');
newDeps.push('pm2');
newDevDeps.push('concurrently', 'open-cli', 'vitest', 'wait-on');
} else if (deps['express'] || deps['fastify']) {
newDeps.push('pm2');
Expand Down
19 changes: 13 additions & 6 deletions packages/wb/src/commands/start.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { loadEnvironmentVariables } from '@willbooster/shared-lib-node/src';
import type { CommandModule, InferredOptionTypes } from 'yargs';

import { project } from '../project.js';
Expand All @@ -13,7 +14,7 @@ import { runWithSpawn } from '../scripts/run.js';
const builder = {
...scriptOptionsBuilder,
mode: {
description: 'Start mode: dev[elopment] (default) | prod[uction] | docker',
description: 'Start mode: dev[elopment] (default) | stg | staging | docker',
type: 'string',
alias: 'm',
},
Expand Down Expand Up @@ -47,16 +48,22 @@ export const startCommand: CommandModule<unknown, InferredOptionTypes<typeof bui
switch (argv.mode || 'dev') {
case 'dev':
case 'development': {
await runWithSpawn(scripts.start(argv), argv);
process.env.WB_ENV ||= 'development';
loadEnvironmentVariables(argv, project.dirPath);
await runWithSpawn(`WB_ENV=${process.env.WB_ENV} ${scripts.start(argv)}`, argv);
break;
}
case 'prod':
case 'production': {
await runWithSpawn(scripts.startProduction(argv, 8080), argv);
case 'stg':
case 'staging': {
process.env.WB_ENV ||= 'staging';
loadEnvironmentVariables(argv, project.dirPath);
await runWithSpawn(`WB_ENV=${process.env.WB_ENV} ${scripts.startProduction(argv, 8080)}`, argv);
break;
}
case 'docker': {
await runWithSpawn(scripts.startDocker(argv), argv);
process.env.WB_ENV ||= 'staging';
loadEnvironmentVariables(argv, project.dirPath);
await runWithSpawn(`WB_ENV=${process.env.WB_ENV} ${scripts.startDocker(argv)}`, argv);
break;
}
default: {
Expand Down
4 changes: 2 additions & 2 deletions packages/wb/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ await yargs(hideBin(process.argv))
removeNpmAndYarnEnvironmentVariables(process.env);

const command = argv._[0].toString();
if (['tc', 'typecheck'].includes(command)) return;
if (['start', 'tc', 'typecheck'].includes(command)) return;
if (command === 'test') {
process.env.WB_ENV = process.env.WB_ENV || 'test';
process.env.WB_ENV ||= 'test';
}
loadEnvironmentVariables(argv, project.dirPath);
})
Expand Down
8 changes: 5 additions & 3 deletions packages/wb/src/scripts/execution/baseExecutionScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ export abstract class BaseExecutionScripts {
// Basically, `playwright` (not `yarn playwright`) should work,
// but it doesn't work on a project depending on `artillery-engine-playwright`.
// So we use `yarn playwright` instead of `playwright` here.
return `APP_ENV=test WB_ENV=test PORT=8080 YARN dotenv -c production -- concurrently --kill-others --raw --success first
const env = process.env.WB_ENV;
return `WB_ENV=${env} NEXT_PUBLIC_WB_ENV=${env} APP_ENV=${env} PORT=8080 YARN concurrently --kill-others --raw --success first
"rm -Rf ${prismaDirectory}/mount && ${startCommand} && exit 1"
"concurrently --kill-others-on-fail --raw 'wait-on -t 600000 -i 2000 http://127.0.0.1:8080' 'yarn playwright install --with-deps'
&& yarn playwright ${playwrightArgs}"`;
}

testE2EDev(argv: ScriptArgv, { playwrightArgs, startCommand }: TestE2EDevOptions): string {
return `APP_ENV=test WB_ENV=test NEXT_PUBLIC_WB_ENV=test PORT=8080 YARN dotenv -c development -- concurrently --kill-others --raw --success first
const env = process.env.WB_ENV;
return `WB_ENV=${env} NEXT_PUBLIC_WB_ENV=${env} APP_ENV=${env} PORT=8080 YARN concurrently --kill-others --raw --success first
"${startCommand} && exit 1"
"concurrently --kill-others-on-fail --raw 'wait-on -t 600000 -i 2000 http://127.0.0.1:8080' 'yarn playwright install --with-deps'
&& yarn playwright ${playwrightArgs}"`;
Expand All @@ -54,7 +56,7 @@ export abstract class BaseExecutionScripts {

testUnit(_: ScriptArgv): string {
// Since this command is referred to from other commands, we have to use "vitest run".
return `YARN vitest run tests/unit --color --passWithNoTests`;
return `WB_ENV=${process.env.WB_ENV} YARN vitest run tests/unit --color --passWithNoTests`;
}

protected waitApp(argv: ScriptArgv, port = this.defaultPort): string {
Expand Down
6 changes: 4 additions & 2 deletions packages/wb/src/scripts/execution/httpServerScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ class HttpServerScripts extends BaseExecutionScripts {
)})`,
}: TestE2EOptions
): string {
return `NODE_ENV=production WB_ENV=test PORT=8080 YARN concurrently --kill-others --raw --success first
return `NODE_ENV=production WB_ENV=${process.env.WB_ENV} PORT=8080 YARN concurrently --kill-others --raw --success first
"${startCommand} && exit 1"
"wait-on -t 600000 -i 2000 http://127.0.0.1:8080 && vitest run tests/e2e --color --passWithNoTests"`;
}

override testE2EDev(argv: ScriptArgv, { startCommand }: TestE2EDevOptions): string {
return `NODE_ENV=production WB_ENV=test PORT=8080 YARN concurrently --kill-others --raw --success first
return `NODE_ENV=production WB_ENV=${
process.env.WB_ENV
} PORT=8080 YARN concurrently --kill-others --raw --success first
"${startCommand || this.start(argv)} && exit 1"
"wait-on -t 600000 -i 2000 http://127.0.0.1:8080 && vitest run tests/e2e --color --passWithNoTests"`;
}
Expand Down
8 changes: 3 additions & 5 deletions packages/wb/src/scripts/execution/remixScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class RemixScripts extends BaseExecutionScripts {

override start(argv: ScriptArgv): string {
return `YARN concurrently --raw --kill-others-on-fail
"dotenv -c development -- remix dev ${argv.normalizedArgsText ?? ''}"
"remix dev ${argv.normalizedArgsText ?? ''}"
"${this.waitAndOpenApp(argv)}"`;
}

override startProduction(argv: ScriptArgv, port: number): string {
return `NODE_ENV=production YARN dotenv -c production -- concurrently --raw --kill-others-on-fail
return `NODE_ENV=production YARN concurrently --raw --kill-others-on-fail
"${prismaScripts.reset()} && ${project.getBuildCommand(
argv
)} && PORT=${port} pm2-runtime start ecosystem.config.cjs ${argv.normalizedArgsText ?? ''}"
Expand All @@ -48,9 +48,7 @@ class RemixScripts extends BaseExecutionScripts {
}

override testStart(argv: ScriptArgv): string {
return `YARN concurrently --kill-others --raw --success first "dotenv -c development -- remix dev" "${this.waitApp(
argv
)}"`;
return `YARN concurrently --kill-others --raw --success first "remix dev" "${this.waitApp(argv)}"`;
}
}

Expand Down