Skip to content

Commit

Permalink
feat(core): add batch flag to run (#19575)
Browse files Browse the repository at this point in the history
Co-authored-by: FrozenPandaz <[email protected]>
  • Loading branch information
xiongemi and FrozenPandaz authored Oct 13, 2023
1 parent 09ff419 commit c83102a
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/generated/cli/affected.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ Type: `string`

Base of the current branch (usually main)

### batch

Type: `boolean`

Default: `false`

### configuration

Type: `string`
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/cli/run-many.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
7 changes: 7 additions & 0 deletions docs/generated/devkit/DefaultTasksRunnerOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -16,6 +17,12 @@

## Properties

### batch

`Optional` **batch**: `boolean`

---

### cacheDirectory

`Optional` **cacheDirectory**: `string`
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/packages/nx/documents/affected.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ Type: `string`

Base of the current branch (usually main)

### batch

Type: `boolean`

Default: `false`

### configuration

Type: `string`
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/packages/nx/documents/run-many.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
5 changes: 4 additions & 1 deletion e2e/js/src/js-executor-tsc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
});

Expand All @@ -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`,
Expand Down
5 changes: 4 additions & 1 deletion packages/nx/src/command-line/affected/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { boolean, CommandModule, middleware } from 'yargs';
import { linkToNxDevAndExamples } from '../yargs-utils/documentation';
import {
withAffectedOptions,
withBatch,
withConfiguration,
withDepGraphOptions,
withOutputStyleOption,
Expand All @@ -17,7 +18,9 @@ export const yargsAffectedCommand: CommandModule = {
linkToNxDevAndExamples(
withAffectedOptions(
withRunOptions(
withOutputStyleOption(withTargetAndConfigurationOption(yargs))
withOutputStyleOption(
withTargetAndConfigurationOption(withBatch(yargs))
)
)
)
.option('all', {
Expand Down
5 changes: 4 additions & 1 deletion packages/nx/src/command-line/run-many/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
withOutputStyleOption,
withTargetAndConfigurationOption,
withOverrides,
withBatch,
} from '../yargs-utils/shared-options';

export const yargsRunManyCommand: CommandModule = {
Expand All @@ -13,7 +14,9 @@ export const yargsRunManyCommand: CommandModule = {
builder: (yargs) =>
linkToNxDevAndExamples(
withRunManyOptions(
withOutputStyleOption(withTargetAndConfigurationOption(yargs))
withOutputStyleOption(
withTargetAndConfigurationOption(withBatch(yargs))
)
),
'run-many'
),
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/command-line/run/command-object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CommandModule } from 'yargs';
import {
withBatch,
withOverrides,
withRunOneOptions,
} from '../yargs-utils/shared-options';
Expand All @@ -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)),
};
13 changes: 12 additions & 1 deletion packages/nx/src/command-line/yargs-utils/shared-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface RunOptions {
skipNxCache: boolean;
cloud: boolean;
dte: boolean;
batch: boolean;
}

export function withRunOptions<T>(yargs: Argv<T>): Argv<T & RunOptions> {
Expand Down Expand Up @@ -89,7 +90,7 @@ export function withRunOptions<T>(yargs: Argv<T>): Argv<T & RunOptions> {
.options('dte', {
type: 'boolean',
hidden: true,
}) as Argv<Omit<RunOptions, 'projects' | 'exclude'>> as any;
}) as Argv<Omit<RunOptions, 'exclude' | 'batch'>> as any;
}

export function withTargetAndConfigurationOption(
Expand All @@ -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({
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/tasks-runner/default-tasks-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface DefaultTasksRunnerOptions {
lifeCycle: LifeCycle;
captureStderr?: boolean;
skipNxCache?: boolean;
batch?: boolean;
}

export const defaultTasksRunner: TasksRunner<
Expand Down
6 changes: 5 additions & 1 deletion packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/tasks-runner/tasks-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/utils/command-line-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface NxArgs {
nxBail?: boolean;
nxIgnoreCycles?: boolean;
type?: string;
batch?: boolean;
}

export function createOverrides(__overrides_unparsed__: string[] = []) {
Expand Down

1 comment on commit c83102a

@vercel
Copy link

@vercel vercel bot commented on c83102a Oct 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.