Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): fix the imports of @angular-devkit/architect/node for n… #16595

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 123 additions & 119 deletions packages/nx/src/adapter/ngcli-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {
import { createConsoleLogger, NodeJsSyncHost } from '@angular-devkit/core/node';
import { FileBuffer } from '@angular-devkit/core/src/virtual-fs/host/interface';

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

import * as chalk from 'chalk';
import { Stats } from 'fs';
Expand Down Expand Up @@ -52,123 +52,6 @@ import {
} from '../config/misc-interfaces';
import { readPluginPackageJson } from '../utils/nx-plugin';

class WrappedWorkspaceNodeModulesArchitectHost extends WorkspaceNodeModulesArchitectHost {
private workspaces = new Workspaces(this.root);

constructor(private workspace, private root) {
super(workspace, root);
}
async resolveBuilder(builderStr: string): Promise<NodeModulesBuilderInfo> {
const [packageName, builderName] = builderStr.split(':');

const { executorsFilePath, executorConfig } = this.readExecutorsJson(
packageName,
builderName
);
const builderInfo = this.readExecutor(packageName, builderName);
return {
name: builderStr,
builderName,
description:
readJsonFile<ExecutorsJson>(executorsFilePath).builders[builderName]
.description,
optionSchema: builderInfo.schema,
import: this.workspaces['resolveImplementation'].bind(this.workspaces)(
executorConfig.implementation,
dirname(executorsFilePath)
),
};
}

private readExecutorsJson(nodeModule: string, builder: string) {
const { json: packageJson, path: packageJsonPath } = readPluginPackageJson(
nodeModule,
this.workspaces['resolvePaths'].bind(this.workspaces)()
);
const executorsFile = packageJson.executors ?? packageJson.builders;

if (!executorsFile) {
throw new Error(
`The "${nodeModule}" package does not support Nx executors or Angular Devkit Builders.`
);
}

const executorsFilePath = require.resolve(
join(dirname(packageJsonPath), executorsFile)
);
const executorsJson = readJsonFile<ExecutorsJson>(executorsFilePath);
const executorConfig: {
implementation: string;
batchImplementation?: string;
schema: string;
hasher?: string;
} = executorsJson.builders?.[builder];
if (!executorConfig) {
throw new Error(
`Cannot find builder '${builder}' in ${executorsFilePath}.`
);
}
return { executorsFilePath, executorConfig, isNgCompat: true };
}

private readExecutor(
nodeModule: string,
executor: string
): ExecutorConfig & { isNgCompat: boolean } {
try {
const { executorsFilePath, executorConfig, isNgCompat } =
this.readExecutorsJson(nodeModule, executor);
const executorsDir = dirname(executorsFilePath);
const schemaPath = this.workspaces['resolveSchema'].bind(this.workspaces)(
executorConfig.schema,
executorsDir
);
const schema = normalizeExecutorSchema(readJsonFile(schemaPath));

const implementationFactory = this.getImplementationFactory<Executor>(
executorConfig.implementation,
executorsDir
);

const batchImplementationFactory = executorConfig.batchImplementation
? this.getImplementationFactory<TaskGraphExecutor>(
executorConfig.batchImplementation,
executorsDir
)
: null;

const hasherFactory = executorConfig.hasher
? this.getImplementationFactory<CustomHasher>(
executorConfig.hasher,
executorsDir
)
: null;

return {
schema,
implementationFactory,
batchImplementationFactory,
hasherFactory,
isNgCompat,
};
} catch (e) {
throw new Error(
`Unable to resolve ${nodeModule}:${executor}.\n${e.message}`
);
}
}

private getImplementationFactory<T>(
implementation: string,
executorsDir: string
): () => T {
return this.workspaces['getImplementationFactory'].bind(this.workspaces)(
implementation,
executorsDir
);
}
}

export async function scheduleTarget(
root: string,
opts: {
Expand All @@ -194,6 +77,127 @@ export async function scheduleTarget(
// This happens when context.scheduleTarget is used to run a target using nx:run-commands
return [];
});

const AngularWorkspaceNodeModulesArchitectHost =
require('@angular-devkit/architect/node').WorkspaceNodeModulesArchitectHost;

class WrappedWorkspaceNodeModulesArchitectHost extends AngularWorkspaceNodeModulesArchitectHost {
private workspaces = new Workspaces(this.root);

constructor(private workspace, private root) {
super(workspace, root);
}
async resolveBuilder(builderStr: string): Promise<NodeModulesBuilderInfo> {
const [packageName, builderName] = builderStr.split(':');

const { executorsFilePath, executorConfig } = this.readExecutorsJson(
packageName,
builderName
);
const builderInfo = this.readExecutor(packageName, builderName);
return {
name: builderStr,
builderName,
description:
readJsonFile<ExecutorsJson>(executorsFilePath).builders[builderName]
.description,
optionSchema: builderInfo.schema,
import: this.workspaces['resolveImplementation'].bind(this.workspaces)(
executorConfig.implementation,
dirname(executorsFilePath)
),
};
}

private readExecutorsJson(nodeModule: string, builder: string) {
const { json: packageJson, path: packageJsonPath } =
readPluginPackageJson(
nodeModule,
this.workspaces['resolvePaths'].bind(this.workspaces)()
);
const executorsFile = packageJson.executors ?? packageJson.builders;

if (!executorsFile) {
throw new Error(
`The "${nodeModule}" package does not support Nx executors or Angular Devkit Builders.`
);
}

const executorsFilePath = require.resolve(
join(dirname(packageJsonPath), executorsFile)
);
const executorsJson = readJsonFile<ExecutorsJson>(executorsFilePath);
const executorConfig: {
implementation: string;
batchImplementation?: string;
schema: string;
hasher?: string;
} = executorsJson.builders?.[builder];
if (!executorConfig) {
throw new Error(
`Cannot find builder '${builder}' in ${executorsFilePath}.`
);
}
return { executorsFilePath, executorConfig, isNgCompat: true };
}

private readExecutor(
nodeModule: string,
executor: string
): ExecutorConfig & { isNgCompat: boolean } {
try {
const { executorsFilePath, executorConfig, isNgCompat } =
this.readExecutorsJson(nodeModule, executor);
const executorsDir = dirname(executorsFilePath);
const schemaPath = this.workspaces['resolveSchema'].bind(
this.workspaces
)(executorConfig.schema, executorsDir);
const schema = normalizeExecutorSchema(readJsonFile(schemaPath));

const implementationFactory = this.getImplementationFactory<Executor>(
executorConfig.implementation,
executorsDir
);

const batchImplementationFactory = executorConfig.batchImplementation
? this.getImplementationFactory<TaskGraphExecutor>(
executorConfig.batchImplementation,
executorsDir
)
: null;

const hasherFactory = executorConfig.hasher
? this.getImplementationFactory<CustomHasher>(
executorConfig.hasher,
executorsDir
)
: null;

return {
schema,
implementationFactory,
batchImplementationFactory,
hasherFactory,
isNgCompat,
};
} catch (e) {
throw new Error(
`Unable to resolve ${nodeModule}:${executor}.\n${e.message}`
);
}
}

private getImplementationFactory<T>(
implementation: string,
executorsDir: string
): () => T {
return this.workspaces['getImplementationFactory'].bind(this.workspaces)(
implementation,
executorsDir
);
}
}

const architectHost = new WrappedWorkspaceNodeModulesArchitectHost(
workspace,
root
Expand Down