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

#2026 feat: nest build --all flag #2312

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
268 changes: 160 additions & 108 deletions actions/build.action.ts
Expand Up @@ -78,72 +78,99 @@ export class BuildAction extends AbstractAction {
(option) => option.name === 'config',
)!.value as string;
const configuration = await this.loader.load(configFileName);
const appName = commandInputs.find((input) => input.name === 'app')!
.value as string;

const pathToTsconfig = getTscConfigPath(
configuration,
commandOptions,
appName,
);
const { options: tsOptions } =
this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
const outDir = tsOptions.outDir || defaultOutDir;
const buildAll = commandOptions.find((opt) => opt.name === 'all')!
.value as boolean;

const isWebpackEnabled = getValueOrDefault<boolean>(
configuration,
'compilerOptions.webpack',
appName,
'webpack',
commandOptions,
);
const builder = isWebpackEnabled
? { type: 'webpack' }
: getBuilder(configuration, commandOptions, appName);
let appNames: string[];
if (buildAll) {
appNames = [undefined!]; // always include the default project
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change this to be const appNames: string[] | undefined;

the undefined! looks hacky to me

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change that, but it requires to also change a lot of other APIs that do accept undefined to mean "default app", but do not declare that.

I was reluctant to do it, since it's a lot of changes, but if it's okay I'll change those to string | undefined.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this spot and all the APIs it trickles down to in f574cfd.

Haven't looked at other places that accept an appName - they may also need to be changed to explicitly allow undefined, but that would be outside of this PR's scope.


await this.workspaceUtils.deleteOutDirIfEnabled(
configuration,
appName,
outDir,
);
this.assetsManager.copyAssets(
configuration,
appName,
outDir,
watchAssetsMode,
);
if (configuration.projects) {
appNames.push(...Object.keys(configuration.projects));
}
} else {
appNames = commandInputs
.filter((input) => input.name === 'app')
.map((input) => input.value) as string[];
}

switch (builder.type) {
case 'tsc':
return this.runTsc(
watchMode,
commandOptions,
configuration,
pathToTsconfig,
appName,
onSuccess,
);
case 'webpack':
return this.runWebpack(
configuration,
appName,
commandOptions,
pathToTsconfig,
isDebugEnabled,
watchMode,
onSuccess,
);
case 'swc':
return this.runSwc(
configuration,
appName,
pathToTsconfig,
watchMode,
commandOptions,
tsOptions,
onSuccess,
);
for (const appName of appNames) {
// if we just always print the project name,
// it will change the output compared to previous version,
// and some clients may rely on that
// TODO: always print in next major version
if (appNames.length > 1) {
if (appName === undefined) {
console.log(`Building default project...`);
} else {
console.log(`Building project ${appName}`);
}
}

const pathToTsconfig = getTscConfigPath(
configuration,
commandOptions,
appName,
);
const { options: tsOptions } =
this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
const outDir = tsOptions.outDir || defaultOutDir;

const isWebpackEnabled = getValueOrDefault<boolean>(
configuration,
'compilerOptions.webpack',
appName,
'webpack',
commandOptions,
);
const builder = isWebpackEnabled
? { type: 'webpack' }
: getBuilder(configuration, commandOptions, appName);

await this.workspaceUtils.deleteOutDirIfEnabled(
configuration,
appName,
outDir,
);
this.assetsManager.copyAssets(
configuration,
appName,
outDir,
watchAssetsMode,
);

switch (builder.type) {
case 'tsc':
await this.runTsc(
watchMode,
commandOptions,
configuration,
pathToTsconfig,
appName,
);
case 'webpack':
await this.runWebpack(
configuration,
appName,
commandOptions,
pathToTsconfig,
isDebugEnabled,
watchMode,
);
case 'swc':
await this.runSwc(
configuration,
appName,
pathToTsconfig,
watchMode,
commandOptions,
tsOptions,
);
}
}

onSuccess?.();
}

private async runSwc(
Expand All @@ -153,28 +180,34 @@ export class BuildAction extends AbstractAction {
watchMode: boolean,
options: Input[],
tsOptions: ts.CompilerOptions,
onSuccess: (() => void) | undefined,
) {
const { SwcCompiler } = await import('../lib/compiler/swc/swc-compiler');
const swc = new SwcCompiler(this.pluginsLoader);
await swc.run(
configuration,
pathToTsconfig,
appName,
{
watch: watchMode,
typeCheck: getValueOrDefault<boolean>(

return new Promise<void>((onSuccess, onError) => {
try {
swc.run(
configuration,
'compilerOptions.typeCheck',
pathToTsconfig,
appName,
'typeCheck',
options,
),
tsOptions,
assetsManager: this.assetsManager,
},
onSuccess,
);
{
watch: watchMode,
typeCheck: getValueOrDefault<boolean>(
configuration,
'compilerOptions.typeCheck',
appName,
'typeCheck',
options,
),
tsOptions,
assetsManager: this.assetsManager,
},
onSuccess,
);
} catch (error) {
onError(error);
}
});
}

private async runWebpack(
Expand All @@ -184,7 +217,6 @@ export class BuildAction extends AbstractAction {
pathToTsconfig: string,
debug: boolean,
watchMode: boolean,
onSuccess: (() => void) | undefined,
) {
const { WebpackCompiler } = await import(
'../lib/compiler/webpack-compiler'
Expand All @@ -199,19 +231,26 @@ export class BuildAction extends AbstractAction {
webpackPath,
defaultWebpackConfigFilename,
);
return webpackCompiler.run(
configuration,
pathToTsconfig,
appName,
{
inputs: commandOptions,
webpackConfigFactoryOrConfig,
debug,
watchMode,
assetsManager: this.assetsManager,
},
onSuccess,
);

return new Promise<void>((onSuccess, onError) => {
try {
return webpackCompiler.run(
configuration,
pathToTsconfig,
appName,
{
inputs: commandOptions,
webpackConfigFactoryOrConfig,
debug,
watchMode,
assetsManager: this.assetsManager,
},
onSuccess,
);
} catch (error) {
onError(error);
}
});
}

private async runTsc(
Expand All @@ -220,7 +259,6 @@ export class BuildAction extends AbstractAction {
configuration: Required<Configuration>,
pathToTsconfig: string,
appName: string,
onSuccess: (() => void) | undefined,
) {
if (watchMode) {
const { WatchCompiler } = await import('../lib/compiler/watch-compiler');
Expand All @@ -233,28 +271,42 @@ export class BuildAction extends AbstractAction {
(option) =>
option.name === 'preserveWatchOutput' && option.value === true,
)?.value as boolean | undefined;
watchCompiler.run(
configuration,
pathToTsconfig,
appName,
{ preserveWatchOutput: isPreserveWatchOutputEnabled },
onSuccess,
);

return new Promise<void>((onSuccess, onError) => {
try {
watchCompiler.run(
configuration,
pathToTsconfig,
appName,
{ preserveWatchOutput: isPreserveWatchOutputEnabled },
onSuccess,
);
} catch (error) {
onError(error);
}
});
} else {
const { Compiler } = await import('../lib/compiler/compiler');
const compiler = new Compiler(
this.pluginsLoader,
this.tsConfigProvider,
this.tsLoader,
);
compiler.run(
configuration,
pathToTsconfig,
appName,
undefined,
onSuccess,
);
this.assetsManager.closeWatchers();

return new Promise<void>((onSuccess, onError) => {
try {
compiler.run(
configuration,
pathToTsconfig,
appName,
undefined,
onSuccess,
);
this.assetsManager.closeWatchers();
} catch (error) {
onError(error);
}
});
}
}

Expand Down
19 changes: 15 additions & 4 deletions commands/build.command.ts
Expand Up @@ -6,7 +6,7 @@ import { Input } from './command.input';
export class BuildCommand extends AbstractCommand {
public load(program: CommanderStatic): void {
program
.command('build [app]')
.command('build [apps...]')
.option('-c, --config [path]', 'Path to nest-cli configuration file.')
.option('-p, --path [path]', 'Path to tsconfig file.')
.option('-w, --watch', 'Run in watch mode (live-reload).')
Expand All @@ -23,8 +23,9 @@ export class BuildCommand extends AbstractCommand {
'--preserveWatchOutput',
'Use "preserveWatchOutput" option when using tsc watch mode.',
)
.option('--all', 'Build all projects in a monorepo')
.description('Build Nest application.')
.action(async (app: string, command: Command) => {
.action(async (apps: string[], command: Command) => {
const options: Input[] = [];

options.push({
Expand Down Expand Up @@ -79,8 +80,18 @@ export class BuildCommand extends AbstractCommand {
!isWebpackEnabled,
});

const inputs: Input[] = [];
inputs.push({ name: 'app', value: app });
options.push({ name: 'all', value: !!command.all });

const inputs: Input[] = apps.map((app) => ({
name: 'app',
value: app,
}));

// handle the default project for `nest build` with no args
if (inputs.length === 0) {
inputs.push({ name: 'app', value: undefined! });
}

await this.action.handle(inputs, options);
});
}
Expand Down