@@ -10,7 +10,7 @@ import {
10
10
InvalidJsonCharacterException ,
11
11
UnexpectedEndOfInputException ,
12
12
} from '@angular-devkit/core' ;
13
- import { dirname , extname , join , resolve } from 'path' ;
13
+ import { dirname , join , resolve } from 'path' ;
14
14
import { RuleFactory } from '../src' ;
15
15
import {
16
16
FileSystemCollectionDesc ,
@@ -39,26 +39,67 @@ export class NodePackageDoesNotSupportSchematics extends BaseException {
39
39
export class NodeModulesEngineHost extends FileSystemEngineHostBase {
40
40
constructor ( private readonly paths ?: string [ ] ) { super ( ) ; }
41
41
42
- protected _resolveCollectionPath ( name : string ) : string {
42
+ private resolve ( name : string , requester ?: string , references = new Set < string > ( ) ) : string {
43
+ if ( requester ) {
44
+ if ( references . has ( requester ) ) {
45
+ references . add ( requester ) ;
46
+ throw new Error (
47
+ 'Circular schematic reference detected: ' + JSON . stringify ( Array . from ( references ) ) ,
48
+ ) ;
49
+ } else {
50
+ references . add ( requester ) ;
51
+ }
52
+ }
53
+
54
+ const relativeBase = requester ? dirname ( requester ) : process . cwd ( ) ;
43
55
let collectionPath : string | undefined = undefined ;
44
- if ( name . startsWith ( '.' ) || name . startsWith ( '/' ) ) {
45
- name = resolve ( name ) ;
56
+
57
+ if ( name . startsWith ( '.' ) ) {
58
+ name = resolve ( relativeBase , name ) ;
46
59
}
47
60
48
- if ( extname ( name ) ) {
49
- // When having an extension let's just resolve the provided path.
50
- collectionPath = require . resolve ( name , { paths : this . paths } ) ;
51
- } else {
52
- const packageJsonPath = require . resolve ( join ( name , 'package.json' ) , { paths : this . paths } ) ;
61
+ const resolveOptions = {
62
+ paths : requester ? [ dirname ( requester ) , ...( this . paths || [ ] ) ] : this . paths ,
63
+ } ;
64
+
65
+ // Try to resolve as a package
66
+ try {
67
+ const packageJsonPath = require . resolve ( join ( name , 'package.json' ) , resolveOptions ) ;
53
68
const { schematics } = require ( packageJsonPath ) ;
54
69
55
70
if ( ! schematics || typeof schematics !== 'string' ) {
56
71
throw new NodePackageDoesNotSupportSchematics ( name ) ;
57
72
}
58
73
59
- collectionPath = resolve ( dirname ( packageJsonPath ) , schematics ) ;
74
+ collectionPath = this . resolve ( schematics , packageJsonPath , references ) ;
75
+ } catch ( e ) {
76
+ if ( e . code !== 'MODULE_NOT_FOUND' ) {
77
+ throw e ;
78
+ }
60
79
}
61
80
81
+ // If not a package, try to resolve as a file
82
+ if ( ! collectionPath ) {
83
+ try {
84
+ collectionPath = require . resolve ( name , resolveOptions ) ;
85
+ } catch ( e ) {
86
+ if ( e . code !== 'MODULE_NOT_FOUND' ) {
87
+ throw e ;
88
+ }
89
+ }
90
+ }
91
+
92
+ // If not a package or a file, error
93
+ if ( ! collectionPath ) {
94
+ throw new CollectionCannotBeResolvedException ( name ) ;
95
+ }
96
+
97
+ return collectionPath ;
98
+ }
99
+
100
+ protected _resolveCollectionPath ( name : string ) : string {
101
+ const collectionPath = this . resolve ( name ) ;
102
+
62
103
try {
63
104
readJsonFile ( collectionPath ) ;
64
105
@@ -68,10 +109,10 @@ export class NodeModulesEngineHost extends FileSystemEngineHostBase {
68
109
e instanceof InvalidJsonCharacterException || e instanceof UnexpectedEndOfInputException
69
110
) {
70
111
throw new InvalidCollectionJsonException ( name , collectionPath , e ) ;
112
+ } else {
113
+ throw e ;
71
114
}
72
115
}
73
-
74
- throw new CollectionCannotBeResolvedException ( name ) ;
75
116
}
76
117
77
118
protected _resolveReferenceString ( refString : string , parentPath : string ) {
0 commit comments