mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-19 12:34:32 +08:00
This change addresses an issue encountered when running external schematics from a yarn pnp workspace. The function used to resolve a collection json using node used recursion in a way that it effectively walked itself into an exception. Then if the exception is the type it expected, it would keep going. This was flawed in that yarn with pnp throws a different type of error when it failed to load the mis-constructed collection path (e.g. `/node_modules/@schematics/angular/collection.json/package.json`). `ENOTDIR` instead of `MODULE_NOT_FOUND`. This process of intentionally / knowingly walking into an exception seems problematic in general. So, I addressed it by removing the recursion that was used mainly because there's a similar need to construct the collection path from a relative path in the package.json as there is to construct the collection path from a relative path that's passed in. Rather than leaning on the recursion to do this, I added the logic at the time we pull the schematics path from the package, and move on. Since the recursion was removed, the infinite recursion safety check at the start wasn't needed anymore. I've tested this in both yarn pnp and non-pnp environments.
139 lines
4.0 KiB
TypeScript
139 lines
4.0 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
import { BaseException } from '@angular-devkit/core';
|
|
import { dirname, join, resolve } from 'path';
|
|
import { RuleFactory } from '../src';
|
|
import { FileSystemCollectionDesc, FileSystemSchematicDesc } from './description';
|
|
import { ExportStringRef } from './export-ref';
|
|
import {
|
|
CollectionCannotBeResolvedException,
|
|
CollectionMissingSchematicsMapException,
|
|
FileSystemEngineHostBase,
|
|
SchematicMissingFieldsException,
|
|
} from './file-system-engine-host-base';
|
|
import { readJsonFile } from './file-system-utility';
|
|
|
|
export class NodePackageDoesNotSupportSchematics extends BaseException {
|
|
constructor(name: string) {
|
|
super(`Package ${JSON.stringify(name)} was found but does not support schematics.`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A simple EngineHost that uses NodeModules to resolve collections.
|
|
*/
|
|
export class NodeModulesEngineHost extends FileSystemEngineHostBase {
|
|
constructor(private readonly paths?: string[]) {
|
|
super();
|
|
}
|
|
|
|
private resolve(name: string, requester?: string): string {
|
|
const relativeBase = requester ? dirname(requester) : process.cwd();
|
|
let collectionPath: string | undefined = undefined;
|
|
|
|
if (name.startsWith('.')) {
|
|
name = resolve(relativeBase, name);
|
|
}
|
|
|
|
const resolveOptions = {
|
|
paths: requester ? [dirname(requester), ...(this.paths || [])] : this.paths,
|
|
};
|
|
|
|
// Try to resolve as a package
|
|
let possibleCollectionPath = name;
|
|
try {
|
|
const packageJsonPath = require.resolve(join(name, 'package.json'), resolveOptions);
|
|
const { schematics } = require(packageJsonPath);
|
|
|
|
if (!schematics || typeof schematics !== 'string') {
|
|
throw new NodePackageDoesNotSupportSchematics(name);
|
|
}
|
|
|
|
// If this is a relative path to the collection, then create the collection
|
|
// path in relation to the package path
|
|
if (schematics.startsWith('.')) {
|
|
const packageDirectory = dirname(packageJsonPath);
|
|
collectionPath = resolve(packageDirectory, schematics);
|
|
}
|
|
// Otherwise use the path as-is to attempt resolution
|
|
else {
|
|
possibleCollectionPath = schematics;
|
|
}
|
|
} catch (e) {
|
|
if ((e as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
// If not a package, try to resolve as a file
|
|
if (!collectionPath) {
|
|
try {
|
|
collectionPath = require.resolve(possibleCollectionPath, resolveOptions);
|
|
} catch (e) {
|
|
if ((e as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
// If not a package or a file, error
|
|
if (!collectionPath) {
|
|
throw new CollectionCannotBeResolvedException(name);
|
|
}
|
|
|
|
return collectionPath;
|
|
}
|
|
|
|
protected _resolveCollectionPath(name: string, requester?: string): string {
|
|
const collectionPath = this.resolve(name, requester);
|
|
readJsonFile(collectionPath);
|
|
|
|
return collectionPath;
|
|
}
|
|
|
|
protected _resolveReferenceString(
|
|
refString: string,
|
|
parentPath: string,
|
|
collectionDescription?: FileSystemCollectionDesc,
|
|
) {
|
|
const ref = new ExportStringRef<RuleFactory<{}>>(refString, parentPath);
|
|
if (!ref.ref) {
|
|
return null;
|
|
}
|
|
|
|
return { ref: ref.ref, path: ref.module };
|
|
}
|
|
|
|
protected _transformCollectionDescription(
|
|
name: string,
|
|
desc: Partial<FileSystemCollectionDesc>,
|
|
): FileSystemCollectionDesc {
|
|
if (!desc.schematics || typeof desc.schematics != 'object') {
|
|
throw new CollectionMissingSchematicsMapException(name);
|
|
}
|
|
|
|
return {
|
|
...desc,
|
|
name,
|
|
} as FileSystemCollectionDesc;
|
|
}
|
|
|
|
protected _transformSchematicDescription(
|
|
name: string,
|
|
_collection: FileSystemCollectionDesc,
|
|
desc: Partial<FileSystemSchematicDesc>,
|
|
): FileSystemSchematicDesc {
|
|
if (!desc.factoryFn || !desc.path || !desc.description) {
|
|
throw new SchematicMissingFieldsException(name);
|
|
}
|
|
|
|
return desc as FileSystemSchematicDesc;
|
|
}
|
|
}
|