From aeb062251ff17539f6013d7d3be0efee6557bc5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Mon, 25 Nov 2024 12:01:57 +0100 Subject: [PATCH] feat: add support for --env-file flag --- actions/start.action.ts | 37 +++++++++++++++++++++++++++++++------ commands/start.command.ts | 8 ++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/actions/start.action.ts b/actions/start.action.ts index 05a14057c..4e209bcac 100644 --- a/actions/start.action.ts +++ b/actions/start.action.ts @@ -72,17 +72,27 @@ export class StartAction extends BuildAction { commandOptions, defaultConfiguration.sourceRoot, ); + const shellOption = commandOptions.find( (option) => option.name === 'shell', ); const useShell = !!shellOption?.value; + + const envFileOption = commandOptions.find( + (option) => option.name === 'envFile', + ); + const envFile = envFileOption?.value as string; + const onSuccess = this.createOnSuccessHook( entryFile, sourceRoot, debugFlag, outDir, binaryToRun, - useShell, + { + shell: useShell, + envFile, + }, ); await this.runBuild( @@ -108,7 +118,10 @@ export class StartAction extends BuildAction { debugFlag: boolean | string | undefined, outDirName: string, binaryToRun: string, - useShell: boolean, + options: { + shell: boolean; + envFile?: string; + }, ) { let childProcessRef: any; process.on( @@ -126,7 +139,10 @@ export class StartAction extends BuildAction { debugFlag, outDirName, binaryToRun, - useShell, + { + shell: options.shell, + envFile: options.envFile, + }, ); childProcessRef.on('exit', () => (childProcessRef = undefined)); }); @@ -140,7 +156,10 @@ export class StartAction extends BuildAction { debugFlag, outDirName, binaryToRun, - useShell, + { + shell: options.shell, + envFile: options.envFile, + }, ); childProcessRef.on('exit', (code: number) => { process.exitCode = code; @@ -156,7 +175,10 @@ export class StartAction extends BuildAction { debug: boolean | string | undefined, outDirName: string, binaryToRun: string, - useShell: boolean, + options: { + shell: boolean; + envFile?: string; + }, ) { let outputFilePath = join(outDirName, sourceRoot, entryFile); if (!fs.existsSync(outputFilePath + '.js')) { @@ -184,11 +206,14 @@ export class StartAction extends BuildAction { typeof debug === 'string' ? `--inspect=${debug}` : '--inspect'; processArgs.unshift(inspectFlag); } + if (options.envFile) { + processArgs.unshift(`--env-file=${options.envFile}`); + } processArgs.unshift('--enable-source-maps'); return spawn(binaryToRun, processArgs, { stdio: 'inherit', - shell: useShell, + shell: options.shell, }); } } diff --git a/commands/start.command.ts b/commands/start.command.ts index 1554c389d..54042fa37 100644 --- a/commands/start.command.ts +++ b/commands/start.command.ts @@ -44,6 +44,10 @@ export class StartCommand extends AbstractCommand { true, ) .option('--no-shell', 'Do not spawn child processes within a shell.') + .option( + '--env-file [path]', + 'Path to an env file (.env) to be loaded into the environment.', + ) .description('Run Nest application.') .action(async (app: string, command: Command) => { const options: Input[] = []; @@ -89,6 +93,10 @@ export class StartCommand extends AbstractCommand { name: 'shell', value: !!command.shell, }); + options.push({ + name: 'envFile', + value: command.envFile, + }); const availableBuilders = ['tsc', 'webpack', 'swc']; if (command.builder && !availableBuilders.includes(command.builder)) {