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): resolve generator collections when generating a extended schematic #19815

Merged
merged 1 commit into from
Oct 25, 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
54 changes: 53 additions & 1 deletion packages/nx/src/adapter/ngcli-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,62 @@ export async function scheduleTarget(
);
}

function createNodeModulesEngineHost(
resolvePaths: string[]
): import('@angular-devkit/schematics/tools').NodeModulesEngineHost {
const NodeModulesEngineHost = require('@angular-devkit/schematics/tools')
.NodeModulesEngineHost as typeof import('@angular-devkit/schematics/tools').NodeModulesEngineHost;

class NxNodeModulesEngineHost extends NodeModulesEngineHost {
constructor() {
super(resolvePaths);
}

override _resolveCollectionPath(name: string, requester?: string): string {
let collectionFilePath: string;
const paths = requester
? [dirname(requester), ...(resolvePaths || [])]
: resolvePaths || [];

if (name.endsWith('.json')) {
collectionFilePath = require.resolve(name, { paths });
} else {
const {
json: { generators, schematics },
path: packageJsonPath,
} = readPluginPackageJson(name, paths);

if (!schematics && !generators) {
throw new Error(
`The "${name}" package does not support Nx generators or Angular Devkit schematics.`
);
}

collectionFilePath = require.resolve(
join(dirname(packageJsonPath), schematics ?? generators)
);
}

return collectionFilePath;
}

override _transformCollectionDescription(name: string, desc: any) {
desc.schematics ??= desc.generators;

return super._transformCollectionDescription(name, desc);
}
}

return new NxNodeModulesEngineHost();
}

function createWorkflow(
fsHost: virtualFs.Host<Stats>,
root: string,
opts: any
): import('@angular-devkit/schematics/tools').NodeWorkflow {
const NodeWorkflow = require('@angular-devkit/schematics/tools').NodeWorkflow;
const NodeWorkflow: typeof import('@angular-devkit/schematics/tools').NodeWorkflow =
require('@angular-devkit/schematics/tools').NodeWorkflow;
const workflow = new NodeWorkflow(fsHost, {
force: false,
dryRun: opts.dryRun,
Expand All @@ -218,6 +268,8 @@ function createWorkflow(
require('@angular-devkit/schematics').formats.standardFormats
),
resolvePaths: [process.cwd(), root],
engineHostCreator: (options) =>
createNodeModulesEngineHost(options.resolvePaths),
});
workflow.registry.addPostTransform(schema.transforms.addUndefinedDefaults);
workflow.engineHost.registerOptionsTransform(
Expand Down