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 all 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
274 changes: 163 additions & 111 deletions actions/build.action.ts
Expand Up @@ -78,113 +78,145 @@ 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 | undefined)[];
if (buildAll) {
appNames = [undefined]; // always include the default project

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(
configuration: Required<Configuration>,
appName: string,
appName: string | undefined,
pathToTsconfig: string,
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(
configuration: Required<Configuration>,
appName: string,
appName: string | undefined,
commandOptions: Input[],
pathToTsconfig: string,
debug: boolean,
watchMode: boolean,
onSuccess: (() => void) | undefined,
) {
const { WebpackCompiler } = await import(
'../lib/compiler/webpack-compiler'
Expand All @@ -199,28 +231,34 @@ 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(
watchMode: boolean,
options: Input[],
configuration: Required<Configuration>,
pathToTsconfig: string,
appName: string,
onSuccess: (() => void) | undefined,
appName: string | 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
2 changes: 1 addition & 1 deletion lib/compiler/assets-manager.ts
Expand Up @@ -38,7 +38,7 @@ export class AssetsManager {

public copyAssets(
configuration: Required<Configuration>,
appName: string,
appName: string | undefined,
outDir: string,
watchAssetsMode: boolean,
) {
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/compiler.ts
Expand Up @@ -18,7 +18,7 @@ export class Compiler extends BaseCompiler {
public run(
configuration: Required<Configuration>,
tsConfigPath: string,
appName: string,
appName: string | undefined,
_extras: any,
onSuccess?: () => void,
) {
Expand Down