Skip to content

Commit eb425b6

Browse files
authoredApr 27, 2023
fix(angular): fix the imports of @angular-devkit/architect/node for n… (#16595)
1 parent 7b0f96b commit eb425b6

File tree

1 file changed

+123
-119
lines changed

1 file changed

+123
-119
lines changed
 

‎packages/nx/src/adapter/ngcli-adapter.ts

+123-119
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {
1212
import { createConsoleLogger, NodeJsSyncHost } from '@angular-devkit/core/node';
1313
import { FileBuffer } from '@angular-devkit/core/src/virtual-fs/host/interface';
1414

15+
// Importing @angular-devkit/architect here will cause issues importing this file without @angular-devkit/architect installed
1516
/* eslint-disable no-restricted-imports */
1617
import type { Architect } from '@angular-devkit/architect';
17-
import { WorkspaceNodeModulesArchitectHost } from '@angular-devkit/architect/node';
18-
import { NodeModulesBuilderInfo } from '@angular-devkit/architect/node/node-modules-architect-host';
18+
import type { NodeModulesBuilderInfo } from '@angular-devkit/architect/node/node-modules-architect-host';
1919

2020
import * as chalk from 'chalk';
2121
import { Stats } from 'fs';
@@ -52,123 +52,6 @@ import {
5252
} from '../config/misc-interfaces';
5353
import { readPluginPackageJson } from '../utils/nx-plugin';
5454

55-
class WrappedWorkspaceNodeModulesArchitectHost extends WorkspaceNodeModulesArchitectHost {
56-
private workspaces = new Workspaces(this.root);
57-
58-
constructor(private workspace, private root) {
59-
super(workspace, root);
60-
}
61-
async resolveBuilder(builderStr: string): Promise<NodeModulesBuilderInfo> {
62-
const [packageName, builderName] = builderStr.split(':');
63-
64-
const { executorsFilePath, executorConfig } = this.readExecutorsJson(
65-
packageName,
66-
builderName
67-
);
68-
const builderInfo = this.readExecutor(packageName, builderName);
69-
return {
70-
name: builderStr,
71-
builderName,
72-
description:
73-
readJsonFile<ExecutorsJson>(executorsFilePath).builders[builderName]
74-
.description,
75-
optionSchema: builderInfo.schema,
76-
import: this.workspaces['resolveImplementation'].bind(this.workspaces)(
77-
executorConfig.implementation,
78-
dirname(executorsFilePath)
79-
),
80-
};
81-
}
82-
83-
private readExecutorsJson(nodeModule: string, builder: string) {
84-
const { json: packageJson, path: packageJsonPath } = readPluginPackageJson(
85-
nodeModule,
86-
this.workspaces['resolvePaths'].bind(this.workspaces)()
87-
);
88-
const executorsFile = packageJson.executors ?? packageJson.builders;
89-
90-
if (!executorsFile) {
91-
throw new Error(
92-
`The "${nodeModule}" package does not support Nx executors or Angular Devkit Builders.`
93-
);
94-
}
95-
96-
const executorsFilePath = require.resolve(
97-
join(dirname(packageJsonPath), executorsFile)
98-
);
99-
const executorsJson = readJsonFile<ExecutorsJson>(executorsFilePath);
100-
const executorConfig: {
101-
implementation: string;
102-
batchImplementation?: string;
103-
schema: string;
104-
hasher?: string;
105-
} = executorsJson.builders?.[builder];
106-
if (!executorConfig) {
107-
throw new Error(
108-
`Cannot find builder '${builder}' in ${executorsFilePath}.`
109-
);
110-
}
111-
return { executorsFilePath, executorConfig, isNgCompat: true };
112-
}
113-
114-
private readExecutor(
115-
nodeModule: string,
116-
executor: string
117-
): ExecutorConfig & { isNgCompat: boolean } {
118-
try {
119-
const { executorsFilePath, executorConfig, isNgCompat } =
120-
this.readExecutorsJson(nodeModule, executor);
121-
const executorsDir = dirname(executorsFilePath);
122-
const schemaPath = this.workspaces['resolveSchema'].bind(this.workspaces)(
123-
executorConfig.schema,
124-
executorsDir
125-
);
126-
const schema = normalizeExecutorSchema(readJsonFile(schemaPath));
127-
128-
const implementationFactory = this.getImplementationFactory<Executor>(
129-
executorConfig.implementation,
130-
executorsDir
131-
);
132-
133-
const batchImplementationFactory = executorConfig.batchImplementation
134-
? this.getImplementationFactory<TaskGraphExecutor>(
135-
executorConfig.batchImplementation,
136-
executorsDir
137-
)
138-
: null;
139-
140-
const hasherFactory = executorConfig.hasher
141-
? this.getImplementationFactory<CustomHasher>(
142-
executorConfig.hasher,
143-
executorsDir
144-
)
145-
: null;
146-
147-
return {
148-
schema,
149-
implementationFactory,
150-
batchImplementationFactory,
151-
hasherFactory,
152-
isNgCompat,
153-
};
154-
} catch (e) {
155-
throw new Error(
156-
`Unable to resolve ${nodeModule}:${executor}.\n${e.message}`
157-
);
158-
}
159-
}
160-
161-
private getImplementationFactory<T>(
162-
implementation: string,
163-
executorsDir: string
164-
): () => T {
165-
return this.workspaces['getImplementationFactory'].bind(this.workspaces)(
166-
implementation,
167-
executorsDir
168-
);
169-
}
170-
}
171-
17255
export async function scheduleTarget(
17356
root: string,
17457
opts: {
@@ -194,6 +77,127 @@ export async function scheduleTarget(
19477
// This happens when context.scheduleTarget is used to run a target using nx:run-commands
19578
return [];
19679
});
80+
81+
const AngularWorkspaceNodeModulesArchitectHost =
82+
require('@angular-devkit/architect/node').WorkspaceNodeModulesArchitectHost;
83+
84+
class WrappedWorkspaceNodeModulesArchitectHost extends AngularWorkspaceNodeModulesArchitectHost {
85+
private workspaces = new Workspaces(this.root);
86+
87+
constructor(private workspace, private root) {
88+
super(workspace, root);
89+
}
90+
async resolveBuilder(builderStr: string): Promise<NodeModulesBuilderInfo> {
91+
const [packageName, builderName] = builderStr.split(':');
92+
93+
const { executorsFilePath, executorConfig } = this.readExecutorsJson(
94+
packageName,
95+
builderName
96+
);
97+
const builderInfo = this.readExecutor(packageName, builderName);
98+
return {
99+
name: builderStr,
100+
builderName,
101+
description:
102+
readJsonFile<ExecutorsJson>(executorsFilePath).builders[builderName]
103+
.description,
104+
optionSchema: builderInfo.schema,
105+
import: this.workspaces['resolveImplementation'].bind(this.workspaces)(
106+
executorConfig.implementation,
107+
dirname(executorsFilePath)
108+
),
109+
};
110+
}
111+
112+
private readExecutorsJson(nodeModule: string, builder: string) {
113+
const { json: packageJson, path: packageJsonPath } =
114+
readPluginPackageJson(
115+
nodeModule,
116+
this.workspaces['resolvePaths'].bind(this.workspaces)()
117+
);
118+
const executorsFile = packageJson.executors ?? packageJson.builders;
119+
120+
if (!executorsFile) {
121+
throw new Error(
122+
`The "${nodeModule}" package does not support Nx executors or Angular Devkit Builders.`
123+
);
124+
}
125+
126+
const executorsFilePath = require.resolve(
127+
join(dirname(packageJsonPath), executorsFile)
128+
);
129+
const executorsJson = readJsonFile<ExecutorsJson>(executorsFilePath);
130+
const executorConfig: {
131+
implementation: string;
132+
batchImplementation?: string;
133+
schema: string;
134+
hasher?: string;
135+
} = executorsJson.builders?.[builder];
136+
if (!executorConfig) {
137+
throw new Error(
138+
`Cannot find builder '${builder}' in ${executorsFilePath}.`
139+
);
140+
}
141+
return { executorsFilePath, executorConfig, isNgCompat: true };
142+
}
143+
144+
private readExecutor(
145+
nodeModule: string,
146+
executor: string
147+
): ExecutorConfig & { isNgCompat: boolean } {
148+
try {
149+
const { executorsFilePath, executorConfig, isNgCompat } =
150+
this.readExecutorsJson(nodeModule, executor);
151+
const executorsDir = dirname(executorsFilePath);
152+
const schemaPath = this.workspaces['resolveSchema'].bind(
153+
this.workspaces
154+
)(executorConfig.schema, executorsDir);
155+
const schema = normalizeExecutorSchema(readJsonFile(schemaPath));
156+
157+
const implementationFactory = this.getImplementationFactory<Executor>(
158+
executorConfig.implementation,
159+
executorsDir
160+
);
161+
162+
const batchImplementationFactory = executorConfig.batchImplementation
163+
? this.getImplementationFactory<TaskGraphExecutor>(
164+
executorConfig.batchImplementation,
165+
executorsDir
166+
)
167+
: null;
168+
169+
const hasherFactory = executorConfig.hasher
170+
? this.getImplementationFactory<CustomHasher>(
171+
executorConfig.hasher,
172+
executorsDir
173+
)
174+
: null;
175+
176+
return {
177+
schema,
178+
implementationFactory,
179+
batchImplementationFactory,
180+
hasherFactory,
181+
isNgCompat,
182+
};
183+
} catch (e) {
184+
throw new Error(
185+
`Unable to resolve ${nodeModule}:${executor}.\n${e.message}`
186+
);
187+
}
188+
}
189+
190+
private getImplementationFactory<T>(
191+
implementation: string,
192+
executorsDir: string
193+
): () => T {
194+
return this.workspaces['getImplementationFactory'].bind(this.workspaces)(
195+
implementation,
196+
executorsDir
197+
);
198+
}
199+
}
200+
197201
const architectHost = new WrappedWorkspaceNodeModulesArchitectHost(
198202
workspace,
199203
root

1 commit comments

Comments
 (1)

vercel[bot] commented on Apr 27, 2023

@vercel[bot]

Successfully deployed to the following URLs:

nx-dev – ./

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

Please sign in to comment.