@@ -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,56 @@ 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 ) : string {
43
+ const relativeBase = requester ? dirname ( requester ) : process . cwd ( ) ;
43
44
let collectionPath : string | undefined = undefined ;
44
- if ( name . startsWith ( '.' ) || name . startsWith ( '/' ) ) {
45
- name = resolve ( name ) ;
45
+
46
+ if ( name . startsWith ( '.' ) ) {
47
+ name = resolve ( relativeBase , name ) ;
46
48
}
47
49
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 } ) ;
50
+ const resolveOptions = {
51
+ paths : requester ? [ dirname ( requester ) , ...( this . paths || [ ] ) ] : this . paths ,
52
+ } ;
53
+
54
+ // Try to resolve as a package
55
+ try {
56
+ const packageJsonPath = require . resolve ( join ( name , 'package.json' ) , resolveOptions ) ;
53
57
const { schematics } = require ( packageJsonPath ) ;
54
58
55
59
if ( ! schematics || typeof schematics !== 'string' ) {
56
60
throw new NodePackageDoesNotSupportSchematics ( name ) ;
57
61
}
58
62
59
- collectionPath = resolve ( dirname ( packageJsonPath ) , schematics ) ;
63
+ collectionPath = this . resolve ( schematics , packageJsonPath ) ;
64
+ } catch ( e ) {
65
+ if ( e . code !== 'MODULE_NOT_FOUND' ) {
66
+ throw e ;
67
+ }
68
+ }
69
+
70
+ // If not a package, try to resolve as a file
71
+ if ( ! collectionPath ) {
72
+ try {
73
+ collectionPath = require . resolve ( name , resolveOptions ) ;
74
+ } catch ( e ) {
75
+ if ( e . code !== 'MODULE_NOT_FOUND' ) {
76
+ throw e ;
77
+ }
78
+ }
79
+ }
80
+
81
+ // If not a package or a file, error
82
+ if ( ! collectionPath ) {
83
+ throw new CollectionCannotBeResolvedException ( name ) ;
60
84
}
61
85
86
+ return collectionPath ;
87
+ }
88
+
89
+ protected _resolveCollectionPath ( name : string ) : string {
90
+ const collectionPath = this . resolve ( name ) ;
91
+
62
92
try {
63
93
readJsonFile ( collectionPath ) ;
64
94
@@ -68,10 +98,10 @@ export class NodeModulesEngineHost extends FileSystemEngineHostBase {
68
98
e instanceof InvalidJsonCharacterException || e instanceof UnexpectedEndOfInputException
69
99
) {
70
100
throw new InvalidCollectionJsonException ( name , collectionPath , e ) ;
101
+ } else {
102
+ throw e ;
71
103
}
72
104
}
73
-
74
- throw new CollectionCannotBeResolvedException ( name ) ;
75
105
}
76
106
77
107
protected _resolveReferenceString ( refString : string , parentPath : string ) {
0 commit comments