From 2ee326c1cc54d6d19f15af342fd05689f14556c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Mon, 8 Jan 2024 15:34:11 +0100 Subject: [PATCH] fix(angular): support scheduling inferred angular cli builder targets (#21019) --- packages/nx/src/adapter/ngcli-adapter.ts | 51 ++++++++++++++++++++---- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/packages/nx/src/adapter/ngcli-adapter.ts b/packages/nx/src/adapter/ngcli-adapter.ts index 7cde3577f32f9..1f1d137ad3e28 100644 --- a/packages/nx/src/adapter/ngcli-adapter.ts +++ b/packages/nx/src/adapter/ngcli-adapter.ts @@ -75,7 +75,7 @@ export async function createBuilderContext( context: ExecutorContext ) { require('./compat'); - const fsHost = new NxScopedHost(context.root); + const fsHost = new NxScopedHostForBuilders(context.root); // the top level import is not patched because it is imported before the // patching happens so we require it here to use the patched version below const { workspaces } = require('@angular-devkit/core'); @@ -199,7 +199,7 @@ export async function scheduleTarget( const { Architect } = require('@angular-devkit/architect'); const logger = getLogger(verbose); - const fsHost = new NxScopedHost(root); + const fsHost = new NxScopedHostForBuilders(root); const { workspace } = await workspaces.readWorkspace( 'angular.json', workspaces.createWorkspaceHost(fsHost) @@ -398,7 +398,7 @@ async function runSchematic( }) .toPromise(); } catch (e) { - console.log(e); + console.error(e); throw e; } if (!record.error) { @@ -430,7 +430,7 @@ export class NxScopedHost extends virtualFs.ScopedHost { } } - private readMergedWorkspaceConfiguration() { + protected readMergedWorkspaceConfiguration() { return zip( from(createProjectGraphAsync()), this.readExistingAngularJson(), @@ -470,8 +470,8 @@ export class NxScopedHost extends virtualFs.ScopedHost { ); }), catchError((err) => { - console.log('Unable to read angular.json'); - console.log(err); + console.error('Unable to read angular.json'); + console.error(err); process.exit(1); }) ); @@ -598,7 +598,7 @@ export class NxScopedHost extends virtualFs.ScopedHost { return this.readJson('angular.json'); } - private readJson(path: string): Observable { + protected readJson(path: string): Observable { return super .exists(path as any) .pipe( @@ -613,6 +613,39 @@ export class NxScopedHost extends virtualFs.ScopedHost { } } +/** + * Host used by Angular CLI builders. It reads the project configurations from + * the project graph to access the expanded targets. + */ +export class NxScopedHostForBuilders extends NxScopedHost { + protected readMergedWorkspaceConfiguration() { + return zip( + from(createProjectGraphAsync()), + this.readExistingAngularJson(), + this.readJson('nx.json') + ).pipe( + map(([graph, angularJson, nxJson]) => { + const workspaceConfig = (angularJson || { projects: {} }) as any; + workspaceConfig.cli ??= nxJson.cli; + workspaceConfig.schematics ??= nxJson.generators; + + for (const projectName of Object.keys(graph.nodes)) { + workspaceConfig.projects[projectName] ??= { + ...graph.nodes[projectName].data, + }; + } + + return workspaceConfig; + }), + catchError((err) => { + console.error('Unable to read angular.json'); + console.error(err); + process.exit(1); + }) + ); + } +} + export function arrayBufferToString(buffer: any) { const array = new Uint8Array(buffer); let result = ''; @@ -628,6 +661,10 @@ export function arrayBufferToString(buffer: any) { return result; } +/** + * Host used by Angular CLI schematics. It reads the project configurations from + * the project configuration files. + */ export class NxScopeHostUsedForWrappedSchematics extends NxScopedHost { constructor(root: string, private readonly host: Tree) { super(root);