@@ -10,8 +10,7 @@ import {
1010 InvalidJsonCharacterException ,
1111 UnexpectedEndOfInputException ,
1212} from '@angular-devkit/core' ;
13- import * as core from '@angular-devkit/core/node' ;
14- import { dirname , join , resolve as resolvePath } from 'path' ;
13+ import { dirname , extname , join , resolve } from 'path' ;
1514import { RuleFactory } from '../src' ;
1615import {
1716 FileSystemCollectionDesc ,
@@ -27,7 +26,6 @@ import {
2726} from './file-system-engine-host-base' ;
2827import { readJsonFile } from './file-system-utility' ;
2928
30-
3129export class NodePackageDoesNotSupportSchematics extends BaseException {
3230 constructor ( name : string ) {
3331 super ( `Package ${ JSON . stringify ( name ) } was found but does not support schematics.` ) ;
@@ -41,71 +39,30 @@ export class NodePackageDoesNotSupportSchematics extends BaseException {
4139export class NodeModulesEngineHost extends FileSystemEngineHostBase {
4240 constructor ( ) { super ( ) ; }
4341
44- protected _resolvePackageJson ( name : string , basedir = process . cwd ( ) ) {
45- return core . resolve ( name , {
46- basedir,
47- checkLocal : true ,
48- checkGlobal : true ,
49- resolvePackageJson : true ,
50- } ) ;
51- }
52-
53- protected _resolvePath ( name : string , basedir = process . cwd ( ) ) {
54- // Allow relative / absolute paths.
55- if ( name . startsWith ( '.' ) || name . startsWith ( '/' ) ) {
56- return resolvePath ( basedir , name ) ;
57- } else {
58- // If it's a file inside a package, resolve the package then return the file...
59- if ( name . split ( '/' ) . length > ( name [ 0 ] == '@' ? 2 : 1 ) ) {
60- const rest = name . split ( '/' ) ;
61- const packageName = rest . shift ( ) + ( name [ 0 ] == '@' ? '/' + rest . shift ( ) : '' ) ;
62-
63- return resolvePath ( core . resolve ( packageName , {
64- basedir,
65- checkLocal : true ,
66- checkGlobal : true ,
67- resolvePackageJson : true ,
68- } ) , '..' , ...rest ) ;
69- }
70-
71- return core . resolve ( name , {
72- basedir,
73- checkLocal : true ,
74- checkGlobal : true ,
75- } ) ;
76- }
77- }
78-
7942 protected _resolveCollectionPath ( name : string ) : string {
8043 let collectionPath : string | undefined = undefined ;
81-
82- if ( name . replace ( / \\ / g, '/' ) . split ( '/' ) . length > ( name [ 0 ] == '@' ? 2 : 1 ) ) {
83- try {
84- collectionPath = this . _resolvePath ( name , process . cwd ( ) ) ;
85- } catch {
86- }
44+ if ( name . startsWith ( '.' ) || name . startsWith ( '/' ) ) {
45+ name = resolve ( name ) ;
8746 }
8847
89- if ( ! collectionPath ) {
90- let packageJsonPath = this . _resolvePackageJson ( name , process . cwd ( ) ) ;
91- // If it's a file, use it as is. Otherwise append package.json to it.
92- if ( ! core . fs . isFile ( packageJsonPath ) ) {
93- packageJsonPath = join ( packageJsonPath , 'package.json' ) ;
94- }
48+ if ( extname ( name ) ) {
49+ // When having an extension let's just resolve the provided path.
50+ collectionPath = require . resolve ( name ) ;
51+ } else {
52+ const packageJsonPath = require . resolve ( join ( name , 'package.json' ) ) ;
53+ const { schematics } = require ( packageJsonPath ) ;
9554
96- const pkgJsonSchematics = require ( packageJsonPath ) [ 'schematics' ] ;
97- if ( ! pkgJsonSchematics || typeof pkgJsonSchematics != 'string' ) {
55+ if ( ! schematics || typeof schematics !== 'string' ) {
9856 throw new NodePackageDoesNotSupportSchematics ( name ) ;
9957 }
100- collectionPath = this . _resolvePath ( pkgJsonSchematics , dirname ( packageJsonPath ) ) ;
58+
59+ collectionPath = resolve ( dirname ( packageJsonPath ) , schematics ) ;
10160 }
10261
10362 try {
104- if ( collectionPath ) {
105- readJsonFile ( collectionPath ) ;
63+ readJsonFile ( collectionPath ) ;
10664
107- return collectionPath ;
108- }
65+ return collectionPath ;
10966 } catch ( e ) {
11067 if (
11168 e instanceof InvalidJsonCharacterException || e instanceof UnexpectedEndOfInputException
0 commit comments