Skip to content

Commit bca2918

Browse files
committed
feat: support multiple --env-file arguments
1 parent 7397574 commit bca2918

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

actions/generate.action.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ import {
2020
} from '../lib/utils/project-utils';
2121
import { AbstractAction } from './abstract.action';
2222

23+
function assertNonArray<T>(value: T): asserts value is Exclude<T, any[]> {
24+
if (Array.isArray(value)) {
25+
throw new TypeError('Expected a non-array value');
26+
}
27+
}
28+
2329
export class GenerateAction extends AbstractAction {
2430
public async handle(inputs: Input[], options: Input[]) {
2531
await generateFiles(inputs.concat(options));
@@ -153,6 +159,7 @@ const mapSchematicOptions = (inputs: Input[]): SchematicOption[] => {
153159
const options: SchematicOption[] = [];
154160
inputs.forEach((input) => {
155161
if (!excludedInputNames.includes(input.name) && input.value !== undefined) {
162+
assertNonArray(input.value);
156163
options.push(new SchematicOption(input.name, input.value));
157164
}
158165
});

actions/new.action.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ export class NewAction extends AbstractAction {
6565
}
6666
}
6767

68+
function assertNonArray<T>(value: T): asserts value is Exclude<T, any[]> {
69+
if (Array.isArray(value)) {
70+
throw new TypeError('Expected a non-array value');
71+
}
72+
}
73+
6874
const getApplicationNameInput = (inputs: Input[]) =>
6975
inputs.find((input) => input.name === 'name');
7076

@@ -133,6 +139,7 @@ const mapSchematicOptions = (options: Input[]): SchematicOption[] => {
133139
return options.reduce(
134140
(schematicOptions: SchematicOption[], option: Input) => {
135141
if (option.name !== 'skip-install') {
142+
assertNonArray(option.value);
136143
schematicOptions.push(new SchematicOption(option.name, option.value));
137144
}
138145
return schematicOptions;

actions/start.action.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import { ERROR_PREFIX } from '../lib/ui';
1414
import { treeKillSync as killProcessSync } from '../lib/utils/tree-kill';
1515
import { BuildAction } from './build.action';
1616

17+
function assertNonArray<T>(value: T): asserts value is Exclude<T, any[]> {
18+
if (Array.isArray(value)) {
19+
throw new TypeError('Expected a non-array value');
20+
}
21+
}
22+
1723
export class StartAction extends BuildAction {
1824
public async handle(commandInputs: Input[], commandOptions: Input[]) {
1925
try {
@@ -44,6 +50,8 @@ export class StartAction extends BuildAction {
4450
watchAssetsModeOption && watchAssetsModeOption.value
4551
);
4652
const debugFlag = debugModeOption && debugModeOption.value;
53+
assertNonArray(debugFlag);
54+
4755
const binaryToRun = getValueOrDefault(
4856
configuration,
4957
'exec',
@@ -81,7 +89,7 @@ export class StartAction extends BuildAction {
8189
const envFileOption = commandOptions.find(
8290
(option) => option.name === 'envFile',
8391
);
84-
const envFile = envFileOption?.value as string;
92+
const envFile = (envFileOption?.value ?? []) as string[];
8593

8694
const onSuccess = this.createOnSuccessHook(
8795
entryFile,
@@ -120,7 +128,7 @@ export class StartAction extends BuildAction {
120128
binaryToRun: string,
121129
options: {
122130
shell: boolean;
123-
envFile?: string;
131+
envFile?: string[];
124132
},
125133
) {
126134
let childProcessRef: any;
@@ -177,7 +185,7 @@ export class StartAction extends BuildAction {
177185
binaryToRun: string,
178186
options: {
179187
shell: boolean;
180-
envFile?: string;
188+
envFile?: string[];
181189
},
182190
) {
183191
let outputFilePath = join(outDirName, sourceRoot, entryFile);
@@ -206,9 +214,14 @@ export class StartAction extends BuildAction {
206214
typeof debug === 'string' ? `--inspect=${debug}` : '--inspect';
207215
processArgs.unshift(inspectFlag);
208216
}
209-
if (options.envFile) {
210-
processArgs.unshift(`--env-file=${options.envFile}`);
217+
218+
if (options.envFile && options.envFile.length > 0) {
219+
const envFileNodeArgs = options.envFile.map(
220+
(envFilePath) => `--env-file=${envFilePath}`,
221+
);
222+
processArgs.unshift(envFileNodeArgs.join(' '));
211223
}
224+
212225
processArgs.unshift('--enable-source-maps');
213226

214227
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) => {

0 commit comments

Comments
 (0)