From c83102a2c4e54c47e3f27899bda4e902d629d2c0 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Fri, 13 Oct 2023 13:09:01 -0400 Subject: [PATCH] feat(core): add batch flag to run (#19575) Co-authored-by: FrozenPandaz --- docs/generated/cli/affected.md | 6 ++++++ docs/generated/cli/run-many.md | 6 ++++++ docs/generated/devkit/DefaultTasksRunnerOptions.md | 7 +++++++ docs/generated/packages/nx/documents/affected.md | 6 ++++++ docs/generated/packages/nx/documents/run-many.md | 6 ++++++ e2e/js/src/js-executor-tsc.test.ts | 5 ++++- .../nx/src/command-line/affected/command-object.ts | 5 ++++- .../nx/src/command-line/run-many/command-object.ts | 5 ++++- packages/nx/src/command-line/run/command-object.ts | 3 ++- .../src/command-line/yargs-utils/shared-options.ts | 13 ++++++++++++- .../nx/src/tasks-runner/default-tasks-runner.ts | 1 + packages/nx/src/tasks-runner/run-command.ts | 6 +++++- packages/nx/src/tasks-runner/tasks-schedule.ts | 2 +- packages/nx/src/utils/command-line-utils.ts | 1 + 14 files changed, 65 insertions(+), 7 deletions(-) diff --git a/docs/generated/cli/affected.md b/docs/generated/cli/affected.md index b8557f8d10726..993d5a8bb575e 100644 --- a/docs/generated/cli/affected.md +++ b/docs/generated/cli/affected.md @@ -79,6 +79,12 @@ Type: `string` Base of the current branch (usually main) +### batch + +Type: `boolean` + +Default: `false` + ### configuration Type: `string` diff --git a/docs/generated/cli/run-many.md b/docs/generated/cli/run-many.md index 2d80356950d79..9de3dfddd6e31 100644 --- a/docs/generated/cli/run-many.md +++ b/docs/generated/cli/run-many.md @@ -81,6 +81,12 @@ Default: `true` [deprecated] `run-many` runs all targets on all projects in the workspace if no projects are provided. This option is no longer required. +### batch + +Type: `boolean` + +Default: `false` + ### configuration Type: `string` diff --git a/docs/generated/devkit/DefaultTasksRunnerOptions.md b/docs/generated/devkit/DefaultTasksRunnerOptions.md index 886a21ce803eb..cb51ef9d8b9f1 100644 --- a/docs/generated/devkit/DefaultTasksRunnerOptions.md +++ b/docs/generated/devkit/DefaultTasksRunnerOptions.md @@ -4,6 +4,7 @@ ### Properties +- [batch](../../devkit/documents/DefaultTasksRunnerOptions#batch): boolean - [cacheDirectory](../../devkit/documents/DefaultTasksRunnerOptions#cachedirectory): string - [cacheableOperations](../../devkit/documents/DefaultTasksRunnerOptions#cacheableoperations): string[] - [cacheableTargets](../../devkit/documents/DefaultTasksRunnerOptions#cacheabletargets): string[] @@ -16,6 +17,12 @@ ## Properties +### batch + +• `Optional` **batch**: `boolean` + +--- + ### cacheDirectory • `Optional` **cacheDirectory**: `string` diff --git a/docs/generated/packages/nx/documents/affected.md b/docs/generated/packages/nx/documents/affected.md index b8557f8d10726..993d5a8bb575e 100644 --- a/docs/generated/packages/nx/documents/affected.md +++ b/docs/generated/packages/nx/documents/affected.md @@ -79,6 +79,12 @@ Type: `string` Base of the current branch (usually main) +### batch + +Type: `boolean` + +Default: `false` + ### configuration Type: `string` diff --git a/docs/generated/packages/nx/documents/run-many.md b/docs/generated/packages/nx/documents/run-many.md index 2d80356950d79..9de3dfddd6e31 100644 --- a/docs/generated/packages/nx/documents/run-many.md +++ b/docs/generated/packages/nx/documents/run-many.md @@ -81,6 +81,12 @@ Default: `true` [deprecated] `run-many` runs all targets on all projects in the workspace if no projects are provided. This option is no longer required. +### batch + +Type: `boolean` + +Default: `false` + ### configuration Type: `string` diff --git a/e2e/js/src/js-executor-tsc.test.ts b/e2e/js/src/js-executor-tsc.test.ts index 5a202c902b649..c43c777e9658c 100644 --- a/e2e/js/src/js-executor-tsc.test.ts +++ b/e2e/js/src/js-executor-tsc.test.ts @@ -152,7 +152,7 @@ describe('js:tsc executor', () => { // check batch build rmDist(); - const batchBuildOutput = runCLI(`build ${parentLib} --skip-nx-cache`, { + let batchBuildOutput = runCLI(`build ${parentLib} --skip-nx-cache`, { env: { NX_BATCH_MODE: 'true' }, }); @@ -173,6 +173,9 @@ describe('js:tsc executor', () => { `Successfully ran target build for project ${parentLib} and 1 task it depends on` ); + batchBuildOutput = runCLI(`build ${parentLib} --skip-nx-cache --batch`); + expect(batchBuildOutput).toContain(`Running 2 tasks with @nx/js:tsc`); + checkFilesExist( // parent `dist/libs/${parentLib}/package.json`, diff --git a/packages/nx/src/command-line/affected/command-object.ts b/packages/nx/src/command-line/affected/command-object.ts index 2127fa624cef9..b989a3ca8f8df 100644 --- a/packages/nx/src/command-line/affected/command-object.ts +++ b/packages/nx/src/command-line/affected/command-object.ts @@ -2,6 +2,7 @@ import { boolean, CommandModule, middleware } from 'yargs'; import { linkToNxDevAndExamples } from '../yargs-utils/documentation'; import { withAffectedOptions, + withBatch, withConfiguration, withDepGraphOptions, withOutputStyleOption, @@ -17,7 +18,9 @@ export const yargsAffectedCommand: CommandModule = { linkToNxDevAndExamples( withAffectedOptions( withRunOptions( - withOutputStyleOption(withTargetAndConfigurationOption(yargs)) + withOutputStyleOption( + withTargetAndConfigurationOption(withBatch(yargs)) + ) ) ) .option('all', { diff --git a/packages/nx/src/command-line/run-many/command-object.ts b/packages/nx/src/command-line/run-many/command-object.ts index 2b4fdf5502434..afe7167d8d048 100644 --- a/packages/nx/src/command-line/run-many/command-object.ts +++ b/packages/nx/src/command-line/run-many/command-object.ts @@ -5,6 +5,7 @@ import { withOutputStyleOption, withTargetAndConfigurationOption, withOverrides, + withBatch, } from '../yargs-utils/shared-options'; export const yargsRunManyCommand: CommandModule = { @@ -13,7 +14,9 @@ export const yargsRunManyCommand: CommandModule = { builder: (yargs) => linkToNxDevAndExamples( withRunManyOptions( - withOutputStyleOption(withTargetAndConfigurationOption(yargs)) + withOutputStyleOption( + withTargetAndConfigurationOption(withBatch(yargs)) + ) ), 'run-many' ), diff --git a/packages/nx/src/command-line/run/command-object.ts b/packages/nx/src/command-line/run/command-object.ts index 2c25e378c71c4..4461ec5c3d0d0 100644 --- a/packages/nx/src/command-line/run/command-object.ts +++ b/packages/nx/src/command-line/run/command-object.ts @@ -1,5 +1,6 @@ import { CommandModule } from 'yargs'; import { + withBatch, withOverrides, withRunOneOptions, } from '../yargs-utils/shared-options'; @@ -13,7 +14,7 @@ export const yargsRunCommand: CommandModule = { (e.g., nx serve myapp --configuration=production) You can skip the use of Nx cache by using the --skip-nx-cache option.`, - builder: (yargs) => withRunOneOptions(yargs), + builder: (yargs) => withRunOneOptions(withBatch(yargs)), handler: async (args) => (await import('./run-one')).runOne(process.cwd(), withOverrides(args)), }; diff --git a/packages/nx/src/command-line/yargs-utils/shared-options.ts b/packages/nx/src/command-line/yargs-utils/shared-options.ts index 9a294df04719f..7cdc97820a7b5 100644 --- a/packages/nx/src/command-line/yargs-utils/shared-options.ts +++ b/packages/nx/src/command-line/yargs-utils/shared-options.ts @@ -25,6 +25,7 @@ export interface RunOptions { skipNxCache: boolean; cloud: boolean; dte: boolean; + batch: boolean; } export function withRunOptions(yargs: Argv): Argv { @@ -89,7 +90,7 @@ export function withRunOptions(yargs: Argv): Argv { .options('dte', { type: 'boolean', hidden: true, - }) as Argv> as any; + }) as Argv> as any; } export function withTargetAndConfigurationOption( @@ -116,6 +117,16 @@ export function withConfiguration(yargs: Argv) { }); } +export function withBatch(yargs: Argv) { + return yargs.options('batch', { + type: 'boolean', + coerce: (v) => { + return v || process.env.BATCH_MODE === 'true'; + }, + default: false, + }) as any; +} + export function withAffectedOptions(yargs: Argv) { return withExcludeOption(yargs) .parserConfiguration({ diff --git a/packages/nx/src/tasks-runner/default-tasks-runner.ts b/packages/nx/src/tasks-runner/default-tasks-runner.ts index ff3dc631f587a..75e80cdc9386b 100644 --- a/packages/nx/src/tasks-runner/default-tasks-runner.ts +++ b/packages/nx/src/tasks-runner/default-tasks-runner.ts @@ -24,6 +24,7 @@ export interface DefaultTasksRunnerOptions { lifeCycle: LifeCycle; captureStderr?: boolean; skipNxCache?: boolean; + batch?: boolean; } export const defaultTasksRunner: TasksRunner< diff --git a/packages/nx/src/tasks-runner/run-command.ts b/packages/nx/src/tasks-runner/run-command.ts index acdcc9f9e9a84..077342d055f11 100644 --- a/packages/nx/src/tasks-runner/run-command.ts +++ b/packages/nx/src/tasks-runner/run-command.ts @@ -194,7 +194,11 @@ export async function runCommand( } function setEnvVarsBasedOnArgs(nxArgs: NxArgs, loadDotEnvFiles: boolean) { - if (nxArgs.outputStyle == 'stream' || process.env.NX_BATCH_MODE === 'true') { + if ( + nxArgs.outputStyle == 'stream' || + process.env.NX_BATCH_MODE === 'true' || + nxArgs.batch + ) { process.env.NX_STREAM_OUTPUT = 'true'; process.env.NX_PREFIX_OUTPUT = 'true'; } diff --git a/packages/nx/src/tasks-runner/tasks-schedule.ts b/packages/nx/src/tasks-runner/tasks-schedule.ts index 21d7b345b6a58..23776248b0567 100644 --- a/packages/nx/src/tasks-runner/tasks-schedule.ts +++ b/packages/nx/src/tasks-runner/tasks-schedule.ts @@ -71,7 +71,7 @@ export class TasksSchedule { } private async scheduleTasks() { - if (process.env.NX_BATCH_MODE === 'true') { + if (this.options.batch || process.env.NX_BATCH_MODE === 'true') { await this.scheduleBatches(); } for (let root of this.notScheduledTaskGraph.roots) { diff --git a/packages/nx/src/utils/command-line-utils.ts b/packages/nx/src/utils/command-line-utils.ts index 1348a16d82f45..cc5a731c92cc1 100644 --- a/packages/nx/src/utils/command-line-utils.ts +++ b/packages/nx/src/utils/command-line-utils.ts @@ -35,6 +35,7 @@ export interface NxArgs { nxBail?: boolean; nxIgnoreCycles?: boolean; type?: string; + batch?: boolean; } export function createOverrides(__overrides_unparsed__: string[] = []) {