Skip to content

Commit

Permalink
feat(core): accept various task runner options from root of nx.json
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Sep 27, 2023
1 parent 8d767ed commit 3dc9366
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 44 deletions.
37 changes: 37 additions & 0 deletions docs/generated/devkit/NxJsonConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ Nx.json configuration

### Properties

- [accessToken](../../devkit/documents/NxJsonConfiguration#accesstoken): string
- [affected](../../devkit/documents/NxJsonConfiguration#affected): NxAffectedConfig
- [cacheDirectory](../../devkit/documents/NxJsonConfiguration#cachedirectory): string
- [cli](../../devkit/documents/NxJsonConfiguration#cli): Object
- [defaultProject](../../devkit/documents/NxJsonConfiguration#defaultproject): string
- [extends](../../devkit/documents/NxJsonConfiguration#extends): string
Expand All @@ -27,15 +29,26 @@ Nx.json configuration
- [installation](../../devkit/documents/NxJsonConfiguration#installation): NxInstallationConfiguration
- [namedInputs](../../devkit/documents/NxJsonConfiguration#namedinputs): Object
- [npmScope](../../devkit/documents/NxJsonConfiguration#npmscope): string
- [parallel](../../devkit/documents/NxJsonConfiguration#parallel): number
- [plugins](../../devkit/documents/NxJsonConfiguration#plugins): string[]
- [pluginsConfig](../../devkit/documents/NxJsonConfiguration#pluginsconfig): Record<string, unknown>
- [release](../../devkit/documents/NxJsonConfiguration#release): NxReleaseConfiguration
- [targetDefaults](../../devkit/documents/NxJsonConfiguration#targetdefaults): TargetDefaults
- [tasksRunnerOptions](../../devkit/documents/NxJsonConfiguration#tasksrunneroptions): Object
- [useDaemonProcess](../../devkit/documents/NxJsonConfiguration#usedaemonprocess): boolean
- [workspaceLayout](../../devkit/documents/NxJsonConfiguration#workspacelayout): Object

## Properties

### accessToken

`Optional` **accessToken**: `string`

If specified Nx will use nx-cloud by default with the given token.
To use a different runner that accepts an access token, define it in [tasksRunnerOptions](../../devkit/documents/Workspace#tasksrunneroptions)

---

### affected

`Optional` **affected**: [`NxAffectedConfig`](../../devkit/documents/NxAffectedConfig)
Expand All @@ -44,6 +57,14 @@ Default options for `nx affected`

---

### cacheDirectory

`Optional` **cacheDirectory**: `string`

Changes the default location of the cache directory.

---

### cli

`Optional` **cli**: `Object`
Expand Down Expand Up @@ -148,6 +169,14 @@ NPM Scope that the workspace uses

---

### parallel

`Optional` **parallel**: `number`

Specifies how many tasks are ran in parallel by Nx for the default tasks runner.

---

### plugins

`Optional` **plugins**: `string`[]
Expand Down Expand Up @@ -192,6 +221,14 @@ Available Task Runners

---

### useDaemonProcess

`Optional` **useDaemonProcess**: `boolean`

Allows turning the daemon off if set to false explicitly.

---

### workspaceLayout

`Optional` **workspaceLayout**: `Object`
Expand Down
10 changes: 10 additions & 0 deletions docs/generated/devkit/TargetConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Target's configuration

### Properties

- [cache](../../devkit/documents/TargetConfiguration#cache): boolean
- [command](../../devkit/documents/TargetConfiguration#command): string
- [configurations](../../devkit/documents/TargetConfiguration#configurations): Object
- [defaultConfiguration](../../devkit/documents/TargetConfiguration#defaultconfiguration): string
Expand All @@ -23,6 +24,15 @@ Target's configuration

## Properties

### cache

`Optional` **cache**: `boolean`

Determines if Nx is able to cache a given target.
Currently only supported in `targetDefaults`.

---

### command

`Optional` **command**: `string`
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/devkit/Task.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ A representation of the invocation of an Executor

### Properties

- [cache](../../devkit/documents/Task#cache): boolean
- [endTime](../../devkit/documents/Task#endtime): number
- [hash](../../devkit/documents/Task#hash): string
- [hashDetails](../../devkit/documents/Task#hashdetails): Object
Expand All @@ -17,6 +18,14 @@ A representation of the invocation of an Executor

## Properties

### cache

`Optional` **cache**: `boolean`

Determines if a given task should be cacheable.

---

### endTime

`Optional` **endTime**: `number`
Expand Down
53 changes: 53 additions & 0 deletions docs/generated/devkit/Workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use ProjectsConfigurations or NxJsonConfiguration

### Properties

- [accessToken](../../devkit/documents/Workspace#accesstoken): string
- [affected](../../devkit/documents/Workspace#affected): NxAffectedConfig
- [cacheDirectory](../../devkit/documents/Workspace#cachedirectory): string
- [cli](../../devkit/documents/Workspace#cli): Object
- [defaultProject](../../devkit/documents/Workspace#defaultproject): string
- [extends](../../devkit/documents/Workspace#extends): string
Expand All @@ -25,17 +27,32 @@ use ProjectsConfigurations or NxJsonConfiguration
- [installation](../../devkit/documents/Workspace#installation): NxInstallationConfiguration
- [namedInputs](../../devkit/documents/Workspace#namedinputs): Object
- [npmScope](../../devkit/documents/Workspace#npmscope): string
- [parallel](../../devkit/documents/Workspace#parallel): number
- [plugins](../../devkit/documents/Workspace#plugins): string[]
- [pluginsConfig](../../devkit/documents/Workspace#pluginsconfig): Record<string, unknown>
- [projects](../../devkit/documents/Workspace#projects): Record<string, ProjectConfiguration>
- [release](../../devkit/documents/Workspace#release): NxReleaseConfiguration
- [targetDefaults](../../devkit/documents/Workspace#targetdefaults): TargetDefaults
- [tasksRunnerOptions](../../devkit/documents/Workspace#tasksrunneroptions): Object
- [useDaemonProcess](../../devkit/documents/Workspace#usedaemonprocess): boolean
- [version](../../devkit/documents/Workspace#version): number
- [workspaceLayout](../../devkit/documents/Workspace#workspacelayout): Object

## Properties

### accessToken

`Optional` **accessToken**: `string`

If specified Nx will use nx-cloud by default with the given token.
To use a different runner that accepts an access token, define it in [tasksRunnerOptions](../../devkit/documents/Workspace#tasksrunneroptions)

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[accessToken](../../devkit/documents/NxJsonConfiguration#accesstoken)

---

### affected

`Optional` **affected**: [`NxAffectedConfig`](../../devkit/documents/NxAffectedConfig)
Expand All @@ -48,6 +65,18 @@ Default options for `nx affected`

---

### cacheDirectory

`Optional` **cacheDirectory**: `string`

Changes the default location of the cache directory.

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[cacheDirectory](../../devkit/documents/NxJsonConfiguration#cachedirectory)

---

### cli

`Optional` **cli**: `Object`
Expand Down Expand Up @@ -184,6 +213,18 @@ NPM Scope that the workspace uses

---

### parallel

`Optional` **parallel**: `number`

Specifies how many tasks are ran in parallel by Nx for the default tasks runner.

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[parallel](../../devkit/documents/NxJsonConfiguration#parallel)

---

### plugins

`Optional` **plugins**: `string`[]
Expand Down Expand Up @@ -260,6 +301,18 @@ Available Task Runners

---

### useDaemonProcess

`Optional` **useDaemonProcess**: `boolean`

Allows turning the daemon off if set to false explicitly.

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[useDaemonProcess](../../devkit/documents/NxJsonConfiguration#usedaemonprocess)

---

### version

**version**: `number`
Expand Down
21 changes: 21 additions & 0 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
* **ALPHA**: Configuration for `nx release` (versioning and publishing of applications and libraries)
*/
release?: NxReleaseConfiguration;

/**
* If specified Nx will use nx-cloud by default with the given token.
* To use a different runner that accepts an access token, define it in {@link tasksRunnerOptions}
*/
accessToken?: string;

/**
* Specifies how many tasks are ran in parallel by Nx for the default tasks runner.
*/
parallel?: number;

/**
* Changes the default location of the cache directory.
*/
cacheDirectory?: string;

/**
* Allows turning the daemon off if set to false explicitly.
*/
useDaemonProcess?: boolean;
}

export function readNxJson(root: string = workspaceRoot): NxJsonConfiguration {
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/config/task-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export interface Task {
* Unix timestamp of when a Batch Task ends
**/
endTime?: number;

/**
* Determines if a given task should be cacheable.
*/
cache?: boolean;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/config/workspace-json-project-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,10 @@ export interface TargetConfiguration<T = any> {
* A default named configuration to use when a target configuration is not provided.
*/
defaultConfiguration?: string;

/**
* Determines if Nx is able to cache a given target.
* Currently only supported in `targetDefaults`.
*/
cache?: boolean;
}
1 change: 1 addition & 0 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class DaemonClient {
enabled() {
if (this._enabled === undefined) {
const useDaemonProcessOption =
this.nxJson.useDaemonProcess ??
this.nxJson.tasksRunnerOptions?.['default']?.options?.useDaemonProcess;
const env = process.env.NX_DAEMON;

Expand Down
8 changes: 8 additions & 0 deletions packages/nx/src/generators/utils/deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export function updateWorkspaceConfiguration(
extends: ext,
installation,
release,
accessToken,
cacheDirectory,
parallel,
useDaemonProcess,
} = workspaceConfig;

const nxJson: Required<NxJsonConfiguration> = {
Expand All @@ -58,6 +62,10 @@ export function updateWorkspaceConfiguration(
extends: ext,
installation,
release,
accessToken,
cacheDirectory,
parallel,
useDaemonProcess,
};

updateNxJson(tree, nxJson);
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/tasks-runner/create-task-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export class ProcessTasks {
target: qualifiedTarget,
projectRoot: project.data.root,
overrides: interpolateOverrides(overrides, project.name, project.data),
cache: project.data.targets[target].cache,
};
}

Expand Down
51 changes: 51 additions & 0 deletions packages/nx/src/tasks-runner/run-command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TasksRunner } from './tasks-runner';
import { getRunner } from './run-command';
import { NxJsonConfiguration } from '../config/nx-json';
import { join } from 'path';

describe('getRunner', () => {
let nxJson: NxJsonConfiguration;
Expand Down Expand Up @@ -73,4 +74,54 @@ describe('getRunner', () => {

expect(tasksRunner).toEqual(mockRunner);
});

it('uses default runner when no tasksRunnerOptions are present', () => {
jest.mock(join(__dirname, './default-tasks-runner.ts'), () => mockRunner);

const { tasksRunner } = getRunner({}, {});

expect(tasksRunner).toEqual(mockRunner);
});

it('uses nx-cloud when no tasksRunnerOptions are present and accessToken is specified', () => {
jest.mock('nx-cloud', () => mockRunner);

const { tasksRunner } = getRunner(
{},
{
accessToken: 'XXXX-XXX-XXXX',
}
);

expect(tasksRunner).toEqual(mockRunner);
});

it('reads options from base properties if no runner options provided', () => {
jest.mock(join(__dirname, './default-tasks-runner.ts'), () => mockRunner);

const { runnerOptions } = getRunner(
{},
{
cacheDirectory: '.nx/cache',
parallel: 3,
useDaemonProcess: false,
targetDefaults: {
build: {
cache: true,
},
},
}
);

expect(runnerOptions).toMatchInlineSnapshot(`
{
"cacheDirectory": ".nx/cache",
"cacheableOperations": [
"build",
],
"parallel": 3,
"useDaemonProcess": false,
}
`);
});
});
Loading

0 comments on commit 3dc9366

Please sign in to comment.