Skip to content

Commit a6ddad8

Browse files
Merge pull request #3000 from micalevisk/feat/issue-2981
feat: support multiple `--env-file` arguments
2 parents 62b2434 + c5df8e6 commit a6ddad8

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

actions/generate.action.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
shouldGenerateSpec,
2020
} from '../lib/utils/project-utils';
2121
import { AbstractAction } from './abstract.action';
22+
import { assertNonArray } from '../lib/utils/type-assertions';
2223

2324
export class GenerateAction extends AbstractAction {
2425
public async handle(inputs: Input[], options: Input[]) {
@@ -153,6 +154,7 @@ const mapSchematicOptions = (inputs: Input[]): SchematicOption[] => {
153154
const options: SchematicOption[] = [];
154155
inputs.forEach((input) => {
155156
if (!excludedInputNames.includes(input.name) && input.value !== undefined) {
157+
assertNonArray(input.value);
156158
options.push(new SchematicOption(input.name, input.value));
157159
}
158160
});

actions/new.action.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { EMOJIS, MESSAGES } from '../lib/ui';
2323
import { normalizeToKebabOrSnakeCase } from '../lib/utils/formatting';
2424
import { gracefullyExitOnPromptError } from '../lib/utils/gracefully-exit-on-prompt-error';
2525
import { AbstractAction } from './abstract.action';
26+
import { assertNonArray } from '../lib/utils/type-assertions';
2627

2728
export class NewAction extends AbstractAction {
2829
public async handle(inputs: Input[], options: Input[]) {
@@ -133,6 +134,7 @@ const mapSchematicOptions = (options: Input[]): SchematicOption[] => {
133134
return options.reduce(
134135
(schematicOptions: SchematicOption[], option: Input) => {
135136
if (option.name !== 'skip-install') {
137+
assertNonArray(option.value);
136138
schematicOptions.push(new SchematicOption(option.name, option.value));
137139
}
138140
return schematicOptions;

actions/start.action.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { ERROR_PREFIX } from '../lib/ui';
1414
import { treeKillSync as killProcessSync } from '../lib/utils/tree-kill';
1515
import { BuildAction } from './build.action';
16+
import { assertNonArray } from '../lib/utils/type-assertions';
1617

1718
export class StartAction extends BuildAction {
1819
public async handle(commandInputs: Input[], commandOptions: Input[]) {
@@ -44,6 +45,8 @@ export class StartAction extends BuildAction {
4445
watchAssetsModeOption && watchAssetsModeOption.value
4546
);
4647
const debugFlag = debugModeOption && debugModeOption.value;
48+
assertNonArray(debugFlag);
49+
4750
const binaryToRun = getValueOrDefault(
4851
configuration,
4952
'exec',
@@ -81,7 +84,7 @@ export class StartAction extends BuildAction {
8184
const envFileOption = commandOptions.find(
8285
(option) => option.name === 'envFile',
8386
);
84-
const envFile = envFileOption?.value as string;
87+
const envFile = (envFileOption?.value ?? []) as string[];
8588

8689
const onSuccess = this.createOnSuccessHook(
8790
entryFile,
@@ -120,7 +123,7 @@ export class StartAction extends BuildAction {
120123
binaryToRun: string,
121124
options: {
122125
shell: boolean;
123-
envFile?: string;
126+
envFile?: string[];
124127
},
125128
) {
126129
let childProcessRef: any;
@@ -177,7 +180,7 @@ export class StartAction extends BuildAction {
177180
binaryToRun: string,
178181
options: {
179182
shell: boolean;
180-
envFile?: string;
183+
envFile?: string[];
181184
},
182185
) {
183186
let outputFilePath = join(outDirName, sourceRoot, entryFile);
@@ -206,9 +209,14 @@ export class StartAction extends BuildAction {
206209
typeof debug === 'string' ? `--inspect=${debug}` : '--inspect';
207210
processArgs.unshift(inspectFlag);
208211
}
209-
if (options.envFile) {
210-
processArgs.unshift(`--env-file=${options.envFile}`);
212+
213+
if (options.envFile && options.envFile.length > 0) {
214+
const envFileNodeArgs = options.envFile.map(
215+
(envFilePath) => `--env-file=${envFilePath}`,
216+
);
217+
processArgs.unshift(envFileNodeArgs.join(' '));
211218
}
219+
212220
processArgs.unshift('--enable-source-maps');
213221

214222
return spawn(binaryToRun, processArgs, {

commands/command.input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface Input {
22
name: string;
3-
value: boolean | string;
3+
value: boolean | string | string[];
44
options?: any;
55
}

commands/start.command.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { Input } from './command.input';
66

77
export class StartCommand extends AbstractCommand {
88
public load(program: CommanderStatic): void {
9+
const collect = (value: any, previous: any) => {
10+
return previous.concat([value]);
11+
};
12+
913
program
1014
.command('start [app]')
1115
.allowUnknownOption()
@@ -47,6 +51,8 @@ export class StartCommand extends AbstractCommand {
4751
.option(
4852
'--env-file [path]',
4953
'Path to an env file (.env) to be loaded into the environment.',
54+
collect,
55+
[],
5056
)
5157
.description('Run Nest application.')
5258
.action(async (app: string, command: Command) => {

lib/utils/type-assertions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function assertNonArray<T>(
2+
value: T,
3+
): asserts value is Exclude<T, any[]> {
4+
if (Array.isArray(value)) {
5+
throw new TypeError('Expected a non-array value');
6+
}
7+
}

0 commit comments

Comments
 (0)