Skip to content

Commit

Permalink
fix(@angular-devkit/schematics): fully resolve schematic entries with…
Browse files Browse the repository at this point in the history
…in packages

Fixes: angular#17085
  • Loading branch information
clydin committed Feb 26, 2020
1 parent d29d403 commit edaa942
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions packages/angular_devkit/schematics/tools/node-module-engine-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
InvalidJsonCharacterException,
UnexpectedEndOfInputException,
} from '@angular-devkit/core';
import { dirname, extname, join, resolve } from 'path';
import { dirname, join, resolve } from 'path';
import { RuleFactory } from '../src';
import {
FileSystemCollectionDesc,
Expand Down Expand Up @@ -39,26 +39,56 @@ export class NodePackageDoesNotSupportSchematics extends BaseException {
export class NodeModulesEngineHost extends FileSystemEngineHostBase {
constructor(private readonly paths?: string[]) { super(); }

protected _resolveCollectionPath(name: string): string {
private resolve(name: string, requester?: string): string {
const relativeBase = requester ? dirname(requester) : process.cwd();
let collectionPath: string | undefined = undefined;
if (name.startsWith('.') || name.startsWith('/')) {
name = resolve(name);

if (name.startsWith('.')) {
name = resolve(relativeBase, name);
}

if (extname(name)) {
// When having an extension let's just resolve the provided path.
collectionPath = require.resolve(name, { paths: this.paths });
} else {
const packageJsonPath = require.resolve(join(name, 'package.json'), { paths: this.paths });
const resolveOptions = {
paths: requester ? [dirname(requester), ...(this.paths || [])] : this.paths,
};

// Try to resolve as a package
try {
const packageJsonPath = require.resolve(join(name, 'package.json'), resolveOptions);
const { schematics } = require(packageJsonPath);

if (!schematics || typeof schematics !== 'string') {
throw new NodePackageDoesNotSupportSchematics(name);
}

collectionPath = resolve(dirname(packageJsonPath), schematics);
collectionPath = this.resolve(schematics, packageJsonPath);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}

// If not a package, try to resolve as a file
if (!collectionPath) {
try {
collectionPath = require.resolve(name, resolveOptions);
} catch (e) {
if (e.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): string {
const collectionPath = this.resolve(name);

try {
readJsonFile(collectionPath);

Expand All @@ -68,10 +98,10 @@ export class NodeModulesEngineHost extends FileSystemEngineHostBase {
e instanceof InvalidJsonCharacterException || e instanceof UnexpectedEndOfInputException
) {
throw new InvalidCollectionJsonException(name, collectionPath, e);
} else {
throw e;
}
}

throw new CollectionCannotBeResolvedException(name);
}

protected _resolveReferenceString(refString: string, parentPath: string) {
Expand Down

0 comments on commit edaa942

Please sign in to comment.