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

feat(core): add pre and post run apis #29636

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/generated/devkit/NxPlugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

Ƭ **NxPlugin**: [`NxPluginV2`](../../devkit/documents/NxPluginV2)

A plugin for Nx
A plugin which enhances the behavior of Nx
4 changes: 3 additions & 1 deletion docs/generated/devkit/NxPluginV2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Ƭ **NxPluginV2**\<`TOptions`\>: `Object`

A plugin for Nx which creates nodes and dependencies for the [ProjectGraph](../../devkit/documents/ProjectGraph)
A plugin which enhances the behavior of Nx

#### Type parameters

Expand All @@ -19,3 +19,5 @@ A plugin for Nx which creates nodes and dependencies for the [ProjectGraph](../.
| `createNodes?` | [`CreateNodes`](../../devkit/documents/CreateNodes)\<`TOptions`\> | Provides a file pattern and function that retrieves configuration info from those files. e.g. { '**/\*.csproj': buildProjectsFromCsProjFile } **`Deprecated`\*\* Use createNodesV2 instead. In Nx 21 support for calling createNodes with a single file for the first argument will be removed. |
| `createNodesV2?` | [`CreateNodesV2`](../../devkit/documents/CreateNodesV2)\<`TOptions`\> | Provides a file pattern and function that retrieves configuration info from those files. e.g. { '\*_/_.csproj': buildProjectsFromCsProjFiles } In Nx 21 createNodes will be replaced with this property. In Nx 22, this property will be removed. |
| `name` | `string` | - |
| `postTasksExecution?` | [`PostTasksExecution`](../../devkit/documents/PostTasksExecution)\<`TOptions`\> | Provides a function to run after the Nx runs tasks |
| `preTasksExecution?` | [`PreTasksExecution`](../../devkit/documents/PreTasksExecution)\<`TOptions`\> | Provides a function to run before the Nx runs tasks |
24 changes: 24 additions & 0 deletions docs/generated/devkit/PostTasksExecution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Type alias: PostTasksExecution\<TOptions\>

Ƭ **PostTasksExecution**\<`TOptions`\>: (`options`: `TOptions` \| `undefined`, `context`: [`PostTasksExecutionContext`](../../devkit/documents/PostTasksExecutionContext)) => `void` \| `Promise`\<`void`\>

#### Type parameters

| Name | Type |
| :--------- | :-------- |
| `TOptions` | `unknown` |

#### Type declaration

▸ (`options`, `context`): `void` \| `Promise`\<`void`\>

##### Parameters

| Name | Type |
| :-------- | :------------------------------------------------------------------------------ |
| `options` | `TOptions` \| `undefined` |
| `context` | [`PostTasksExecutionContext`](../../devkit/documents/PostTasksExecutionContext) |

##### Returns

`void` \| `Promise`\<`void`\>
11 changes: 11 additions & 0 deletions docs/generated/devkit/PostTasksExecutionContext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Type alias: PostTasksExecutionContext

Ƭ **PostTasksExecutionContext**: `Object`

#### Type declaration

| Name | Type |
| :-------------------- | :------------------------------------------------------------------ |
| `nxJsonConfiguration` | [`NxJsonConfiguration`](../../devkit/documents/NxJsonConfiguration) |
| `taskResults` | [`TaskResults`](../../devkit/documents/TaskResults) |
| `workspaceRoot` | `string` |
24 changes: 24 additions & 0 deletions docs/generated/devkit/PreTasksExecution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Type alias: PreTasksExecution\<TOptions\>

Ƭ **PreTasksExecution**\<`TOptions`\>: (`options`: `TOptions` \| `undefined`, `context`: [`PreTasksExecutionContext`](../../devkit/documents/PreTasksExecutionContext)) => `void` \| `Promise`\<`void`\>

#### Type parameters

| Name | Type |
| :--------- | :-------- |
| `TOptions` | `unknown` |

#### Type declaration

▸ (`options`, `context`): `void` \| `Promise`\<`void`\>

##### Parameters

| Name | Type |
| :-------- | :---------------------------------------------------------------------------- |
| `options` | `TOptions` \| `undefined` |
| `context` | [`PreTasksExecutionContext`](../../devkit/documents/PreTasksExecutionContext) |

##### Returns

`void` \| `Promise`\<`void`\>
10 changes: 10 additions & 0 deletions docs/generated/devkit/PreTasksExecutionContext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Type alias: PreTasksExecutionContext

Ƭ **PreTasksExecutionContext**: `Object`

#### Type declaration

| Name | Type |
| :-------------------- | :------------------------------------------------------------------ |
| `nxJsonConfiguration` | [`NxJsonConfiguration`](../../devkit/documents/NxJsonConfiguration) |
| `workspaceRoot` | `string` |
6 changes: 6 additions & 0 deletions docs/generated/devkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ It only uses language primitives and immutable objects
- [Task](../../devkit/documents/Task)
- [TaskGraph](../../devkit/documents/TaskGraph)
- [TaskHasher](../../devkit/documents/TaskHasher)
- [TaskResult](../../devkit/documents/TaskResult)
- [Tree](../../devkit/documents/Tree)
- [Workspace](../../devkit/documents/Workspace)

Expand Down Expand Up @@ -86,6 +87,10 @@ It only uses language primitives and immutable objects
- [NxPluginV2](../../devkit/documents/NxPluginV2)
- [PackageManager](../../devkit/documents/PackageManager)
- [PluginConfiguration](../../devkit/documents/PluginConfiguration)
- [PostTasksExecution](../../devkit/documents/PostTasksExecution)
- [PostTasksExecutionContext](../../devkit/documents/PostTasksExecutionContext)
- [PreTasksExecution](../../devkit/documents/PreTasksExecution)
- [PreTasksExecutionContext](../../devkit/documents/PreTasksExecutionContext)
- [ProjectType](../../devkit/documents/ProjectType)
- [ProjectsMetadata](../../devkit/documents/ProjectsMetadata)
- [PromiseExecutor](../../devkit/documents/PromiseExecutor)
Expand All @@ -94,6 +99,7 @@ It only uses language primitives and immutable objects
- [StringChange](../../devkit/documents/StringChange)
- [TargetDefaults](../../devkit/documents/TargetDefaults)
- [TaskGraphExecutor](../../devkit/documents/TaskGraphExecutor)
- [TaskResults](../../devkit/documents/TaskResults)
- [ToJSOptions](../../devkit/documents/ToJSOptions)
- [WorkspaceJsonConfiguration](../../devkit/documents/WorkspaceJsonConfiguration)

Expand Down
36 changes: 36 additions & 0 deletions docs/generated/devkit/TaskResult.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Interface: TaskResult

The result of a completed [Task](../../devkit/documents/Task)

## Table of contents

### Properties

- [code](../../devkit/documents/TaskResult#code): number
- [status](../../devkit/documents/TaskResult#status): TaskStatus
- [task](../../devkit/documents/TaskResult#task): Task
- [terminalOutput](../../devkit/documents/TaskResult#terminaloutput): string

## Properties

### code

**code**: `number`

---

### status

**status**: `TaskStatus`

---

### task

**task**: [`Task`](../../devkit/documents/Task)

---

### terminalOutput

`Optional` **terminalOutput**: `string`
5 changes: 5 additions & 0 deletions docs/generated/devkit/TaskResults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Type alias: TaskResults

Ƭ **TaskResults**: `Record`\<`string`, [`TaskResult`](../../devkit/documents/TaskResult)\>

A map of [TaskResult](../../devkit/documents/TaskResult) keyed by the ID of the completed [Task](../../devkit/documents/Task)s
6 changes: 6 additions & 0 deletions docs/generated/packages/devkit/documents/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ It only uses language primitives and immutable objects
- [Task](../../devkit/documents/Task)
- [TaskGraph](../../devkit/documents/TaskGraph)
- [TaskHasher](../../devkit/documents/TaskHasher)
- [TaskResult](../../devkit/documents/TaskResult)
- [Tree](../../devkit/documents/Tree)
- [Workspace](../../devkit/documents/Workspace)

Expand Down Expand Up @@ -86,6 +87,10 @@ It only uses language primitives and immutable objects
- [NxPluginV2](../../devkit/documents/NxPluginV2)
- [PackageManager](../../devkit/documents/PackageManager)
- [PluginConfiguration](../../devkit/documents/PluginConfiguration)
- [PostTasksExecution](../../devkit/documents/PostTasksExecution)
- [PostTasksExecutionContext](../../devkit/documents/PostTasksExecutionContext)
- [PreTasksExecution](../../devkit/documents/PreTasksExecution)
- [PreTasksExecutionContext](../../devkit/documents/PreTasksExecutionContext)
- [ProjectType](../../devkit/documents/ProjectType)
- [ProjectsMetadata](../../devkit/documents/ProjectsMetadata)
- [PromiseExecutor](../../devkit/documents/PromiseExecutor)
Expand All @@ -94,6 +99,7 @@ It only uses language primitives and immutable objects
- [StringChange](../../devkit/documents/StringChange)
- [TargetDefaults](../../devkit/documents/TargetDefaults)
- [TaskGraphExecutor](../../devkit/documents/TaskGraphExecutor)
- [TaskResults](../../devkit/documents/TaskResults)
- [ToJSOptions](../../devkit/documents/ToJSOptions)
- [WorkspaceJsonConfiguration](../../devkit/documents/WorkspaceJsonConfiguration)

Expand Down
1 change: 1 addition & 0 deletions e2e/jest/src/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
describe('Jest', () => {
beforeAll(() => {
newProject({ name: uniq('proj-jest'), packages: ['@nx/js', '@nx/node'] });
process.env.NX_E2E_VERBOSE_LOGGING = 'true';
});

afterAll(() => cleanupProject());
Expand Down
2 changes: 1 addition & 1 deletion packages/devkit/src/utils/convert-nx-executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Convert Nx Executor', () => {

const registry = new schema.CoreSchemaRegistry();
registry.addPostTransform(schema.transforms.addUndefinedDefaults);
const testArchitectHost = new TestingArchitectHost();
const testArchitectHost = new TestingArchitectHost(fs.tempDir, fs.tempDir);
testArchitectHost.workspaceRoot = fs.tempDir;
const architect = new Architect(testArchitectHost, registry);

Expand Down
14 changes: 14 additions & 0 deletions packages/nx/src/command-line/release/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import {
import { deepMergeJson } from './config/deep-merge-json';
import { filterReleaseGroups } from './config/filter-release-groups';
import { printConfigAndExit } from './utils/print-config';
import { workspaceRoot } from '../../utils/workspace-root';
import {
runPostTasksExecution,
runPreTasksExecution,
} from '../../project-graph/plugins/tasks-execution-hooks';

export interface PublishProjectsResult {
[projectName: string]: {
Expand Down Expand Up @@ -249,6 +254,10 @@ async function runPublishOnProjects(
].join('\n')}\n`
);
}
await runPreTasksExecution({
workspaceRoot,
nxJsonConfiguration: nxJson,
});

/**
* Run the relevant nx-release-publish executor on each of the selected projects.
Expand Down Expand Up @@ -276,6 +285,11 @@ async function runPublishOnProjects(
code: taskData.code,
};
}
await runPostTasksExecution({
taskResults: commandResults,
workspaceRoot,
nxJsonConfiguration: nxJson,
});

return publishProjectsResult;
}
30 changes: 30 additions & 0 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ import {
type HandleFlushSyncGeneratorChangesToDiskMessage,
} from '../message-types/flush-sync-generator-changes-to-disk';
import { DelayedSpinner } from '../../utils/delayed-spinner';
import {
PostTasksExecutionContext,
PreTasksExecutionContext,
} from '../../project-graph/plugins/public-api';
import {
HandlePostTasksExecutionMessage,
HandlePreTasksExecutionMessage,
POST_TASKS_EXECUTION,
PRE_TASKS_EXECUTION,
} from '../message-types/run-tasks-execution-hooks';

const DAEMON_ENV_SETTINGS = {
NX_PROJECT_GLOB_CACHE: 'false',
Expand Down Expand Up @@ -435,6 +445,26 @@ export class DaemonClient {
return this.sendToDaemonViaQueue(message);
}

async runPreTasksExecution(
context: PreTasksExecutionContext
): Promise<NodeJS.ProcessEnv[]> {
const message: HandlePreTasksExecutionMessage = {
type: PRE_TASKS_EXECUTION,
context,
};
return this.sendToDaemonViaQueue(message);
}

async runPostTasksExecution(
context: PostTasksExecutionContext
): Promise<void> {
const message: HandlePostTasksExecutionMessage = {
type: POST_TASKS_EXECUTION,
context,
};
return this.sendToDaemonViaQueue(message);
}

async isServerAvailable(): Promise<boolean> {
return new Promise((resolve) => {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type {
PostTasksExecutionContext,
PreTasksExecutionContext,
} from '../../project-graph/plugins';

export const PRE_TASKS_EXECUTION = 'PRE_TASKS_EXECUTION' as const;
export const POST_TASKS_EXECUTION = 'POST_TASKS_EXECUTION' as const;

export type HandlePreTasksExecutionMessage = {
type: typeof PRE_TASKS_EXECUTION;
context: PreTasksExecutionContext;
};
export type HandlePostTasksExecutionMessage = {
type: typeof POST_TASKS_EXECUTION;
context: PostTasksExecutionContext;
};

export function isHandlePreTasksExecutionMessage(
message: unknown
): message is HandlePreTasksExecutionMessage {
return (
typeof message === 'object' &&
message !== null &&
'type' in message &&
message['type'] === PRE_TASKS_EXECUTION
);
}

export function isHandlePostTasksExecutionMessage(
message: unknown
): message is HandlePostTasksExecutionMessage {
return (
typeof message === 'object' &&
message !== null &&
'type' in message &&
message['type'] === POST_TASKS_EXECUTION
);
}
41 changes: 41 additions & 0 deletions packages/nx/src/daemon/server/handle-tasks-execution-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type {
PostTasksExecutionContext,
PreTasksExecutionContext,
} from '../../project-graph/plugins/public-api';
import {
runPostTasksExecution,
runPreTasksExecution,
} from '../../project-graph/plugins/tasks-execution-hooks';

export async function handleRunPreTasksExecution(
context: PreTasksExecutionContext
) {
try {
const envs = await runPreTasksExecution(context);
return {
response: JSON.stringify(envs),
description: 'handleRunPreTasksExecution',
};
} catch (e) {
return {
error: e,
description: `Error when running preTasksExecution.`,
};
}
}
export async function handleRunPostTasksExecution(
context: PostTasksExecutionContext
) {
try {
await runPostTasksExecution(context);
return {
response: 'true',
description: 'handleRunPostTasksExecution',
};
} catch (e) {
return {
error: e,
description: `Error when running postTasksExecution.`,
};
}
}
18 changes: 18 additions & 0 deletions packages/nx/src/daemon/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ import {
isHandleFlushSyncGeneratorChangesToDiskMessage,
} from '../message-types/flush-sync-generator-changes-to-disk';
import { handleFlushSyncGeneratorChangesToDisk } from './handle-flush-sync-generator-changes-to-disk';
import {
isHandlePostTasksExecutionMessage,
isHandlePreTasksExecutionMessage,
POST_TASKS_EXECUTION,
PRE_TASKS_EXECUTION,
} from '../message-types/run-tasks-execution-hooks';
import {
handleRunPostTasksExecution,
handleRunPreTasksExecution,
} from './handle-tasks-execution-hooks';

let performanceObserver: PerformanceObserver | undefined;
let workspaceWatcherError: Error | undefined;
Expand Down Expand Up @@ -281,6 +291,14 @@ async function handleMessage(socket, data: string) {
payload.deletedFiles
)
);
} else if (isHandlePreTasksExecutionMessage(payload)) {
await handleResult(socket, PRE_TASKS_EXECUTION, () =>
handleRunPreTasksExecution(payload.context)
);
} else if (isHandlePostTasksExecutionMessage(payload)) {
await handleResult(socket, POST_TASKS_EXECUTION, () =>
handleRunPostTasksExecution(payload.context)
);
} else {
await respondWithErrorAndExit(
socket,
Expand Down
Loading
Loading