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: add support for --env-file flag #2818

Merged
merged 1 commit into from
Nov 25, 2024
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
37 changes: 31 additions & 6 deletions actions/start.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -126,7 +139,10 @@ export class StartAction extends BuildAction {
debugFlag,
outDirName,
binaryToRun,
useShell,
{
shell: options.shell,
envFile: options.envFile,
},
);
childProcessRef.on('exit', () => (childProcessRef = undefined));
});
Expand All @@ -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;
Expand All @@ -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')) {
Expand Down Expand Up @@ -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,
});
}
}
8 changes: 8 additions & 0 deletions commands/start.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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)) {
Expand Down