nestjs/nest-cli

refactor request: avoid useless calls to `NestConfigurationLoader#load` on 'build' (and 'start') commands

micalevisk opened this issue · 1 comments

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

while investigating how to reduce the time spent on build command just for the cli bootstrapping, I notice that NestConfigurationLoader#load method was called twice for the same input:

image

public async load(name?: string): Promise<Required<Configuration>> {
const content: string | undefined = name
? await this.reader.read(name)
: await this.reader.readAnyOf([
'nest-cli.json',
'.nestcli.json',
'.nest-cli.json',
'nest.json',
]);
if (!content) {
return defaultConfiguration;
}
const fileConfig = JSON.parse(content);
if (fileConfig.compilerOptions) {
return {
...defaultConfiguration,
...fileConfig,
compilerOptions: {
...defaultConfiguration.compilerOptions,
...fileConfig.compilerOptions,
},
};
}
return {
...defaultConfiguration,
...fileConfig,
};
}

name is always undefined for those calls.

Describe the solution you'd like

somehow refactor the code so that NestConfigurationLoader#load will be invoked only once.

What is the motivation / use case for changing the behavior?

that method is using fs.readFile+JSON.parse. I'd like to avoid this for performance reasons (although minimal, I guess) and to improve the codebase (ie., as a refactor since that seems to be something missed from the legacy version)

Would you like to create a PR for this? If there's no easy way to refactor the code to just call the method once, we can just cache the invocation result and grab it from memory for subsequent calls.