Skip to content

Commit

Permalink
fix(angular): support scheduling inferred angular cli builder targets (
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez authored Jan 8, 2024
1 parent 69523f1 commit 2ee326c
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions packages/nx/src/adapter/ngcli-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -398,7 +398,7 @@ async function runSchematic(
})
.toPromise();
} catch (e) {
console.log(e);
console.error(e);
throw e;
}
if (!record.error) {
Expand Down Expand Up @@ -430,7 +430,7 @@ export class NxScopedHost extends virtualFs.ScopedHost<any> {
}
}

private readMergedWorkspaceConfiguration() {
protected readMergedWorkspaceConfiguration() {
return zip(
from(createProjectGraphAsync()),
this.readExistingAngularJson(),
Expand Down Expand Up @@ -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);
})
);
Expand Down Expand Up @@ -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(
Expand All @@ -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 = '';
Expand All @@ -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);
Expand Down

0 comments on commit 2ee326c

Please sign in to comment.