Skip to content

Commit

Permalink
feat(core): move plugins apis to support async create nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Nov 13, 2023
1 parent b13c137 commit 2a2ec8e
Show file tree
Hide file tree
Showing 42 changed files with 530 additions and 420 deletions.
9 changes: 9 additions & 0 deletions docs/generated/devkit/CreateNodesAsync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Type alias: CreateNodesAsync<T\>

Ƭ **CreateNodesAsync**<`T`\>: readonly [projectFilePattern: string, createNodesFunction: CreateNodesFunctionAsync<T\>]

#### Type parameters

| Name | Type |
| :--- | :-------- |
| `T` | `unknown` |
11 changes: 3 additions & 8 deletions docs/generated/devkit/CreateNodesFunction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Type alias: CreateNodesFunction<T\>

Ƭ **CreateNodesFunction**<`T`\>: (`projectConfigurationFile`: `string`, `options`: `T` \| `undefined`, `context`: [`CreateNodesContext`](../../devkit/documents/CreateNodesContext)) => { `externalNodes?`: `Record`<`string`, [`ProjectGraphExternalNode`](../../devkit/documents/ProjectGraphExternalNode)\> ; `projects?`: `Record`<`string`, `Optional`<[`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration), `"root"`\>\> }
Ƭ **CreateNodesFunction**<`T`\>: (`projectConfigurationFile`: `string`, `options`: `T` \| `undefined`, `context`: [`CreateNodesContext`](../../devkit/documents/CreateNodesContext)) => [`CreateNodesResult`](../../devkit/documents/CreateNodesResult)

#### Type parameters

Expand All @@ -10,7 +10,7 @@

#### Type declaration

▸ (`projectConfigurationFile`, `options`, `context`): `Object`
▸ (`projectConfigurationFile`, `options`, `context`): [`CreateNodesResult`](../../devkit/documents/CreateNodesResult)

A function which parses a configuration file into a set of nodes.
Used for creating nodes for the [ProjectGraph](../../devkit/documents/ProjectGraph)
Expand All @@ -25,9 +25,4 @@ Used for creating nodes for the [ProjectGraph](../../devkit/documents/ProjectGra

##### Returns

`Object`

| Name | Type | Description |
| :--------------- | :---------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------- |
| `externalNodes?` | `Record`<`string`, [`ProjectGraphExternalNode`](../../devkit/documents/ProjectGraphExternalNode)\> | A map of external node name -> external node. External nodes do not have a root, so the key is their name. |
| `projects?` | `Record`<`string`, `Optional`<[`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration), `"root"`\>\> | A map of project root -> project configuration |
[`CreateNodesResult`](../../devkit/documents/CreateNodesResult)
24 changes: 24 additions & 0 deletions docs/generated/devkit/CreateNodesResult.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Interface: CreateNodesResult

## Table of contents

### Properties

- [externalNodes](../../devkit/documents/CreateNodesResult#externalnodes): Record&lt;string, ProjectGraphExternalNode&gt;
- [projects](../../devkit/documents/CreateNodesResult#projects): Record&lt;string, Optional&lt;ProjectConfiguration, &quot;root&quot;&gt;&gt;

## Properties

### externalNodes

`Optional` **externalNodes**: `Record`<`string`, [`ProjectGraphExternalNode`](../../devkit/documents/ProjectGraphExternalNode)\>

A map of external node name -> external node. External nodes do not have a root, so the key is their name.

---

### projects

`Optional` **projects**: `Record`<`string`, `Optional`<[`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration), `"root"`\>\>

A map of project root -> project configuration
21 changes: 11 additions & 10 deletions docs/generated/devkit/NxPluginV2.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Type alias: NxPluginV2<T\>
# Type alias: NxPluginV2<TOptions, TCreateNodes\>

Ƭ **NxPluginV2**<`T`\>: `Object`
Ƭ **NxPluginV2**<`TOptions`, `TCreateNodes`\>: `Object`

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

#### Type parameters

| Name | Type |
| :--- | :-------- |
| `T` | `unknown` |
| Name | Type |
| :------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `TOptions` | `unknown` |
| `TCreateNodes` | extends [`CreateNodes`](../../devkit/documents/CreateNodes)<`TOptions`\> \| [`CreateNodesAsync`](../../devkit/documents/CreateNodesAsync)<`TOptions`\> = [`CreateNodes`](../../devkit/documents/CreateNodes)<`TOptions`\> \| [`CreateNodesAsync`](../../devkit/documents/CreateNodesAsync)<`TOptions`\> |

#### Type declaration

| Name | Type | Description |
| :-------------------- | :---------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------- |
| `createDependencies?` | [`CreateDependencies`](../../devkit/documents/CreateDependencies)<`T`\> | Provides a function to analyze files to create dependencies for the [ProjectGraph](../../devkit/documents/ProjectGraph) |
| `createNodes?` | [`CreateNodes`](../../devkit/documents/CreateNodes)<`T`\> | Provides a file pattern and function that retrieves configuration info from those files. e.g. { '\*_/_.csproj': buildProjectsFromCsProjFile } |
| `name` | `string` | - |
| Name | Type | Description |
| :-------------------- | :----------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------- |
| `createDependencies?` | [`CreateDependencies`](../../devkit/documents/CreateDependencies)<`TOptions`\> | Provides a function to analyze files to create dependencies for the [ProjectGraph](../../devkit/documents/ProjectGraph) |
| `createNodes?` | `TCreateNodes` | Provides a file pattern and function that retrieves configuration info from those files. e.g. { '\*_/_.csproj': buildProjectsFromCsProjFile } |
| `name` | `string` | - |
2 changes: 2 additions & 0 deletions docs/generated/devkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ It only uses language primitives and immutable objects

- [CreateDependenciesContext](../../devkit/documents/CreateDependenciesContext)
- [CreateNodesContext](../../devkit/documents/CreateNodesContext)
- [CreateNodesResult](../../devkit/documents/CreateNodesResult)
- [DefaultTasksRunnerOptions](../../devkit/documents/DefaultTasksRunnerOptions)
- [ExecutorContext](../../devkit/documents/ExecutorContext)
- [ExecutorsJson](../../devkit/documents/ExecutorsJson)
Expand Down Expand Up @@ -64,6 +65,7 @@ It only uses language primitives and immutable objects

- [CreateDependencies](../../devkit/documents/CreateDependencies)
- [CreateNodes](../../devkit/documents/CreateNodes)
- [CreateNodesAsync](../../devkit/documents/CreateNodesAsync)
- [CreateNodesFunction](../../devkit/documents/CreateNodesFunction)
- [CustomHasher](../../devkit/documents/CustomHasher)
- [DynamicDependency](../../devkit/documents/DynamicDependency)
Expand Down
2 changes: 2 additions & 0 deletions docs/generated/packages/devkit/documents/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ It only uses language primitives and immutable objects

- [CreateDependenciesContext](../../devkit/documents/CreateDependenciesContext)
- [CreateNodesContext](../../devkit/documents/CreateNodesContext)
- [CreateNodesResult](../../devkit/documents/CreateNodesResult)
- [DefaultTasksRunnerOptions](../../devkit/documents/DefaultTasksRunnerOptions)
- [ExecutorContext](../../devkit/documents/ExecutorContext)
- [ExecutorsJson](../../devkit/documents/ExecutorsJson)
Expand Down Expand Up @@ -64,6 +65,7 @@ It only uses language primitives and immutable objects

- [CreateDependencies](../../devkit/documents/CreateDependencies)
- [CreateNodes](../../devkit/documents/CreateNodes)
- [CreateNodesAsync](../../devkit/documents/CreateNodesAsync)
- [CreateNodesFunction](../../devkit/documents/CreateNodesFunction)
- [CustomHasher](../../devkit/documents/CustomHasher)
- [DynamicDependency](../../devkit/documents/DynamicDependency)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ function startDevRemotes(
const { schema } = getExecutorInformation(
collection,
executor,
workspaceRoot
workspaceRoot,
workspaceProjects
);
if (
(options.verbose && schema.additionalProperties) ||
Expand All @@ -223,6 +224,7 @@ function startDevRemotes(
target: 'serve',
configuration: context.target.configuration,
runOptions,
projects: workspaceProjects,
},
options.verbose
).then((obs) => {
Expand Down Expand Up @@ -279,8 +281,7 @@ export function executeModuleFederationDevServerBuilder(
configurationName: context.target.configuration,
cwd: context.currentDirectory,
isVerbose: options.verbose,
projectsConfigurations:
readProjectsConfigurationFromProjectGraph(projectGraph),
projectsConfigurations: { projects: workspaceProjects, version: 2 },
nxJsonConfiguration: readNxJson(),
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ export function executeModuleFederationDevSSRBuilder(
const { schema } = getExecutorInformation(
collection,
executor,
workspaceRoot
workspaceRoot,
workspaceProjects
);

if (schema.additionalProperties || 'verbose' in schema.properties) {
Expand Down Expand Up @@ -141,6 +142,7 @@ export function executeModuleFederationDevSSRBuilder(
target,
configuration: context.target.configuration,
runOptions,
projects: workspaceProjects,
},
options.verbose
).then((obs) =>
Expand Down
3 changes: 2 additions & 1 deletion packages/cypress/src/utils/start-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ ${e.message || e}`);
const { schema } = getExecutorInformation(
collection,
executor,
context.root
context.root,
context.projectsConfigurations.projects
);

// NOTE: schema won't have a default since readTargetOptions would have
Expand Down
7 changes: 6 additions & 1 deletion packages/devkit/src/executors/read-target-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export function readTargetOptions<T = any>(
const ws = new Workspaces(context.root);
const [nodeModule, executorName] = targetConfiguration.executor.split(':');
const { schema } = getExecutorInformation
? getExecutorInformation(nodeModule, executorName, context.root)
? getExecutorInformation(
nodeModule,
executorName,
context.root,
context.projectsConfigurations?.projects ?? context.workspace.projects
)
: // TODO(v18): remove readExecutor. This is to be backwards compatible with Nx 16.5 and below.
(ws as any).readExecutor(nodeModule, executorName);

Expand Down
49 changes: 36 additions & 13 deletions packages/nx/src/adapter/ngcli-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import {
getProjects,
updateProjectConfiguration,
} from '../generators/utils/project-configuration';
import { createProjectGraphAsync } from '../project-graph/project-graph';
import {
createProjectGraphAsync,
readProjectsConfigurationFromProjectGraph,
} from '../project-graph/project-graph';
import { readJsonFile } from '../utils/fileutils';
import { getNxRequirePaths } from '../utils/installation-directory';
import { parseJson } from '../utils/json';
Expand Down Expand Up @@ -79,7 +82,8 @@ export async function createBuilderContext(
);
const architectHost = await getWrappedWorkspaceNodeModulesArchitectHost(
workspace,
context.root
context.root,
context.projectsConfigurations.projects
);

const registry = new schema.CoreSchemaRegistry();
Expand Down Expand Up @@ -156,6 +160,7 @@ export async function scheduleTarget(
target: string;
configuration: string;
runOptions: any;
projects: Record<string, ProjectConfiguration>;
},
verbose: boolean
): Promise<Observable<import('@angular-devkit/architect').BuilderOutput>> {
Expand All @@ -177,7 +182,8 @@ export async function scheduleTarget(

const architectHost = await getWrappedWorkspaceNodeModulesArchitectHost(
workspace,
root
root,
opts.projects
);
const architect: Architect = new Architect(architectHost, registry);
const run = await architect.scheduleTarget(
Expand All @@ -204,7 +210,8 @@ export async function scheduleTarget(
}

function createNodeModulesEngineHost(
resolvePaths: string[]
resolvePaths: string[],
projects: Record<string, ProjectConfiguration>
): import('@angular-devkit/schematics/tools').NodeModulesEngineHost {
const NodeModulesEngineHost = require('@angular-devkit/schematics/tools')
.NodeModulesEngineHost as typeof import('@angular-devkit/schematics/tools').NodeModulesEngineHost;
Expand All @@ -226,7 +233,7 @@ function createNodeModulesEngineHost(
const {
json: { generators, schematics },
path: packageJsonPath,
} = readPluginPackageJson(name, paths);
} = readPluginPackageJson(name, projects, paths);

if (!schematics && !generators) {
throw new Error(
Expand Down Expand Up @@ -255,7 +262,8 @@ function createNodeModulesEngineHost(
function createWorkflow(
fsHost: virtualFs.Host<Stats>,
root: string,
opts: any
opts: any,
projects: Record<string, ProjectConfiguration>
): import('@angular-devkit/schematics/tools').NodeWorkflow {
const NodeWorkflow: typeof import('@angular-devkit/schematics/tools').NodeWorkflow =
require('@angular-devkit/schematics/tools').NodeWorkflow;
Expand All @@ -269,7 +277,7 @@ function createWorkflow(
),
resolvePaths: [process.cwd(), root],
engineHostCreator: (options) =>
createNodeModulesEngineHost(options.resolvePaths),
createNodeModulesEngineHost(options.resolvePaths, projects),
});
workflow.registry.addPostTransform(schema.transforms.addUndefinedDefaults);
workflow.engineHost.registerOptionsTransform(
Expand Down Expand Up @@ -676,6 +684,7 @@ function findMatchingFileChange(host: Tree, path: Path) {
export async function generate(
root: string,
opts: GenerateOptions,
projects: Record<string, ProjectConfiguration>,
verbose: boolean
) {
const logger = getLogger(verbose);
Expand All @@ -687,7 +696,7 @@ export async function generate(
`ng-cli generator: ${opts.collectionName}:${opts.generatorName}`
)
);
const workflow = createWorkflow(fsHost, root, opts);
const workflow = createWorkflow(fsHost, root, opts, projects);
const collection = getCollection(workflow, opts.collectionName);
const schematic = collection.createSchematic(opts.generatorName, true);
return (
Expand Down Expand Up @@ -769,6 +778,7 @@ export async function runMigration(
root: string,
packageName: string,
migrationName: string,
projects: Record<string, ProjectConfiguration>,
isVerbose: boolean
) {
const logger = getLogger(isVerbose);
Expand All @@ -780,7 +790,7 @@ export async function runMigration(
`ng-cli migration: ${packageName}:${migrationName}`
)
);
const workflow = createWorkflow(fsHost, root, {});
const workflow = createWorkflow(fsHost, root, {}, projects);
const collection = resolveMigrationsCollection(packageName);

const record = { loggingQueue: [] as string[], error: false };
Expand Down Expand Up @@ -889,6 +899,9 @@ export function wrapAngularDevkitSchematic(
require('./compat');

return async (host: Tree, generatorOptions: { [k: string]: any }) => {
const graph = await createProjectGraphAsync();
const { projects } = readProjectsConfigurationFromProjectGraph(graph);

if (
mockedSchematics &&
mockedSchematics[`${collectionName}:${generatorName}`]
Expand Down Expand Up @@ -950,7 +963,7 @@ export function wrapAngularDevkitSchematic(
defaults: false,
quiet: false,
};
const workflow = createWorkflow(fsHost, host.root, options);
const workflow = createWorkflow(fsHost, host.root, options, projects);

// used for testing
if (collectionResolutionOverrides) {
Expand Down Expand Up @@ -1053,14 +1066,19 @@ function saveProjectsConfigurationsInWrappedSchematic(

async function getWrappedWorkspaceNodeModulesArchitectHost(
workspace: workspaces.WorkspaceDefinition,
root: string
root: string,
projects: Record<string, ProjectConfiguration>
) {
const {
WorkspaceNodeModulesArchitectHost: AngularWorkspaceNodeModulesArchitectHost,
} = await import('@angular-devkit/architect/node');

class WrappedWorkspaceNodeModulesArchitectHost extends AngularWorkspaceNodeModulesArchitectHost {
constructor(private workspace, private root) {
constructor(
private workspace,
private root: string,
private projects: Record<string, ProjectConfiguration>
) {
super(workspace, root);
}

Expand Down Expand Up @@ -1090,6 +1108,7 @@ async function getWrappedWorkspaceNodeModulesArchitectHost(
const { json: packageJson, path: packageJsonPath } =
readPluginPackageJson(
nodeModule,
this.projects,
this.root ? [this.root, __dirname] : [__dirname]
);
const executorsFile = packageJson.executors ?? packageJson.builders;
Expand Down Expand Up @@ -1170,5 +1189,9 @@ async function getWrappedWorkspaceNodeModulesArchitectHost(
}
}

return new WrappedWorkspaceNodeModulesArchitectHost(workspace, root);
return new WrappedWorkspaceNodeModulesArchitectHost(
workspace,
root,
projects
);
}
Loading

0 comments on commit 2a2ec8e

Please sign in to comment.