Skip to content

feat: support multiple --env-file arguments #3000

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

Merged
merged 2 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions actions/generate.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import {
} from '../lib/utils/project-utils';
import { AbstractAction } from './abstract.action';

function assertNonArray<T>(value: T): asserts value is Exclude<T, any[]> {
if (Array.isArray(value)) {
throw new TypeError('Expected a non-array value');
}
}

export class GenerateAction extends AbstractAction {
public async handle(inputs: Input[], options: Input[]) {
await generateFiles(inputs.concat(options));
Expand Down Expand Up @@ -153,6 +159,7 @@ const mapSchematicOptions = (inputs: Input[]): SchematicOption[] => {
const options: SchematicOption[] = [];
inputs.forEach((input) => {
if (!excludedInputNames.includes(input.name) && input.value !== undefined) {
assertNonArray(input.value);
options.push(new SchematicOption(input.name, input.value));
}
});
Expand Down
7 changes: 7 additions & 0 deletions actions/new.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export class NewAction extends AbstractAction {
}
}

function assertNonArray<T>(value: T): asserts value is Exclude<T, any[]> {
if (Array.isArray(value)) {
throw new TypeError('Expected a non-array value');
}
}

const getApplicationNameInput = (inputs: Input[]) =>
inputs.find((input) => input.name === 'name');

Expand Down Expand Up @@ -133,6 +139,7 @@ const mapSchematicOptions = (options: Input[]): SchematicOption[] => {
return options.reduce(
(schematicOptions: SchematicOption[], option: Input) => {
if (option.name !== 'skip-install') {
assertNonArray(option.value);
schematicOptions.push(new SchematicOption(option.name, option.value));
}
return schematicOptions;
Expand Down
23 changes: 18 additions & 5 deletions actions/start.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import { ERROR_PREFIX } from '../lib/ui';
import { treeKillSync as killProcessSync } from '../lib/utils/tree-kill';
import { BuildAction } from './build.action';

function assertNonArray<T>(value: T): asserts value is Exclude<T, any[]> {
if (Array.isArray(value)) {
throw new TypeError('Expected a non-array value');
}
}

export class StartAction extends BuildAction {
public async handle(commandInputs: Input[], commandOptions: Input[]) {
try {
Expand Down Expand Up @@ -44,6 +50,8 @@ export class StartAction extends BuildAction {
watchAssetsModeOption && watchAssetsModeOption.value
);
const debugFlag = debugModeOption && debugModeOption.value;
assertNonArray(debugFlag);

const binaryToRun = getValueOrDefault(
configuration,
'exec',
Expand Down Expand Up @@ -81,7 +89,7 @@ export class StartAction extends BuildAction {
const envFileOption = commandOptions.find(
(option) => option.name === 'envFile',
);
const envFile = envFileOption?.value as string;
const envFile = (envFileOption?.value ?? []) as string[];

const onSuccess = this.createOnSuccessHook(
entryFile,
Expand Down Expand Up @@ -120,7 +128,7 @@ export class StartAction extends BuildAction {
binaryToRun: string,
options: {
shell: boolean;
envFile?: string;
envFile?: string[];
},
) {
let childProcessRef: any;
Expand Down Expand Up @@ -177,7 +185,7 @@ export class StartAction extends BuildAction {
binaryToRun: string,
options: {
shell: boolean;
envFile?: string;
envFile?: string[];
},
) {
let outputFilePath = join(outDirName, sourceRoot, entryFile);
Expand Down Expand Up @@ -206,9 +214,14 @@ export class StartAction extends BuildAction {
typeof debug === 'string' ? `--inspect=${debug}` : '--inspect';
processArgs.unshift(inspectFlag);
}
if (options.envFile) {
processArgs.unshift(`--env-file=${options.envFile}`);

if (options.envFile && options.envFile.length > 0) {
const envFileNodeArgs = options.envFile.map(
(envFilePath) => `--env-file=${envFilePath}`,
);
processArgs.unshift(envFileNodeArgs.join(' '));
}

processArgs.unshift('--enable-source-maps');

return spawn(binaryToRun, processArgs, {
Expand Down
2 changes: 1 addition & 1 deletion commands/command.input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Input {
name: string;
value: boolean | string;
value: boolean | string | string[];
options?: any;
}
6 changes: 6 additions & 0 deletions commands/start.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { Input } from './command.input';

export class StartCommand extends AbstractCommand {
public load(program: CommanderStatic): void {
const collect = (value: any, previous: any) => {
return previous.concat([value]);
};

program
.command('start [app]')
.allowUnknownOption()
Expand Down Expand Up @@ -47,6 +51,8 @@ export class StartCommand extends AbstractCommand {
.option(
'--env-file [path]',
'Path to an env file (.env) to be loaded into the environment.',
collect,
[],
)
.description('Run Nest application.')
.action(async (app: string, command: Command) => {
Expand Down