fix(angular): support scheduling inferred angular cli builder targets (#21019)

This commit is contained in:
Leosvel Pérez Espinosa 2024-01-08 15:34:11 +01:00 committed by GitHub
parent 69523f1eed
commit 2ee326c1cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<any> {
}
}
private readMergedWorkspaceConfiguration() {
protected readMergedWorkspaceConfiguration() {
return zip(
from(createProjectGraphAsync()),
this.readExistingAngularJson(),
@ -470,8 +470,8 @@ export class NxScopedHost extends virtualFs.ScopedHost<any> {
);
}),
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<any> {
return this.readJson('angular.json');
}
private readJson<T = any>(path: string): Observable<T> {
protected readJson<T = any>(path: string): Observable<T> {
return super
.exists(path as any)
.pipe(
@ -613,6 +613,39 @@ export class NxScopedHost extends virtualFs.ScopedHost<any> {
}
}
/**
* 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<NxJsonConfiguration>('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);