Skip to content

Commit

Permalink
chore(core): update API to pre/post tasks execution and for it to run…
Browse files Browse the repository at this point in the history
… via the daemon
  • Loading branch information
FrozenPandaz committed Jan 24, 2025
1 parent 7060411 commit 50e6965
Show file tree
Hide file tree
Showing 23 changed files with 331 additions and 169 deletions.
4 changes: 2 additions & 2 deletions docs/generated/devkit/NxPluginV2.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ A plugin which enhances the behavior of Nx
| `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` | - |
| `postRun?` | [`PostRun`](../../devkit/documents/PostRun)\<`TOptions`\> | Provides a function to run after the Nx runs tasks |
| `preRun?` | [`PreRun`](../../devkit/documents/PreRun)\<`TOptions`\> | Provides a function to run before the Nx runs tasks |
| `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 |
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Type alias: PreRun\<TOptions\>
# Type alias: PostTasksExecution\<TOptions\>

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

#### Type parameters

Expand All @@ -14,10 +14,10 @@

##### Parameters

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

##### Returns

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Type alias: PostRunContext
# Type alias: PostTasksExecutionContext

Ƭ **PostRunContext**: `Object`
Ƭ **PostTasksExecutionContext**: `Object`

#### Type declaration

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Type alias: PostRun\<TOptions\>
# Type alias: PreTasksExecution\<TOptions\>

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

#### Type parameters

Expand All @@ -14,10 +14,10 @@

##### Parameters

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

##### Returns

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Type alias: PreRunContext
# Type alias: PreTasksExecutionContext

Ƭ **PreRunContext**: `Object`
Ƭ **PreTasksExecutionContext**: `Object`

#### Type declaration

Expand Down
8 changes: 4 additions & 4 deletions docs/generated/devkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ It only uses language primitives and immutable objects
- [NxPluginV2](../../devkit/documents/NxPluginV2)
- [PackageManager](../../devkit/documents/PackageManager)
- [PluginConfiguration](../../devkit/documents/PluginConfiguration)
- [PostRun](../../devkit/documents/PostRun)
- [PostRunContext](../../devkit/documents/PostRunContext)
- [PreRun](../../devkit/documents/PreRun)
- [PreRunContext](../../devkit/documents/PreRunContext)
- [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 Down
8 changes: 4 additions & 4 deletions docs/generated/packages/devkit/documents/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ It only uses language primitives and immutable objects
- [NxPluginV2](../../devkit/documents/NxPluginV2)
- [PackageManager](../../devkit/documents/PackageManager)
- [PluginConfiguration](../../devkit/documents/PluginConfiguration)
- [PostRun](../../devkit/documents/PostRun)
- [PostRunContext](../../devkit/documents/PostRunContext)
- [PreRun](../../devkit/documents/PreRun)
- [PreRunContext](../../devkit/documents/PreRunContext)
- [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 Down
12 changes: 6 additions & 6 deletions packages/nx/src/command-line/release/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import {
import { deepMergeJson } from './config/deep-merge-json';
import { filterReleaseGroups } from './config/filter-release-groups';
import { printConfigAndExit } from './utils/print-config';
import { getPlugins } from '../../project-graph/plugins/get-plugins';
import { workspaceRoot } from '../../utils/workspace-root';
import { runPostRun, runPreRun } from '../../project-graph/plugins/run-hooks';
import {
runPostTasksExecution,
runPreTasksExecution,
} from '../../project-graph/plugins/tasks-execution-hooks';

export interface PublishProjectsResult {
[projectName: string]: {
Expand Down Expand Up @@ -252,9 +254,7 @@ async function runPublishOnProjects(
].join('\n')}\n`
);
}
const plugins = await getPlugins();

await runPreRun(plugins, {
await runPreTasksExecution({
workspaceRoot,
nxJsonConfiguration: nxJson,
});
Expand Down Expand Up @@ -285,7 +285,7 @@ async function runPublishOnProjects(
code: taskData.code,
};
}
await runPostRun(plugins, {
await runPostTasksExecution({
taskResults: commandResults,
workspaceRoot,
nxJsonConfiguration: nxJson,
Expand Down
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
38 changes: 38 additions & 0 deletions packages/nx/src/daemon/message-types/run-tasks-execution-hooks.ts
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
8 changes: 4 additions & 4 deletions packages/nx/src/devkit-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ export type {
CreateMetadata,
CreateMetadataContext,
ProjectsMetadata,
PreRun,
PreRunContext,
PostRun,
PostRunContext,
PreTasksExecution,
PreTasksExecutionContext,
PostTasksExecution,
PostTasksExecutionContext,
} from './project-graph/plugins';

export { AggregateCreateNodesError } from './project-graph/error-types';
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/project-graph/plugins/isolation/enabled.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IS_WASM } from 'nx/src/native';
import { IS_WASM } from '../../../native';

export function isIsolationEnabled() {
// Explicitly enabled, regardless of further conditions
Expand Down
Loading

0 comments on commit 50e6965

Please sign in to comment.