@@ -10,8 +10,7 @@ import {
10
10
InvalidJsonCharacterException ,
11
11
UnexpectedEndOfInputException ,
12
12
} 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' ;
15
14
import { RuleFactory } from '../src' ;
16
15
import {
17
16
FileSystemCollectionDesc ,
@@ -27,7 +26,6 @@ import {
27
26
} from './file-system-engine-host-base' ;
28
27
import { readJsonFile } from './file-system-utility' ;
29
28
30
-
31
29
export class NodePackageDoesNotSupportSchematics extends BaseException {
32
30
constructor ( name : string ) {
33
31
super ( `Package ${ JSON . stringify ( name ) } was found but does not support schematics.` ) ;
@@ -41,71 +39,30 @@ export class NodePackageDoesNotSupportSchematics extends BaseException {
41
39
export class NodeModulesEngineHost extends FileSystemEngineHostBase {
42
40
constructor ( ) { super ( ) ; }
43
41
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
-
79
42
protected _resolveCollectionPath ( name : string ) : string {
80
43
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 ) ;
87
46
}
88
47
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 ) ;
95
54
96
- const pkgJsonSchematics = require ( packageJsonPath ) [ 'schematics' ] ;
97
- if ( ! pkgJsonSchematics || typeof pkgJsonSchematics != 'string' ) {
55
+ if ( ! schematics || typeof schematics !== 'string' ) {
98
56
throw new NodePackageDoesNotSupportSchematics ( name ) ;
99
57
}
100
- collectionPath = this . _resolvePath ( pkgJsonSchematics , dirname ( packageJsonPath ) ) ;
58
+
59
+ collectionPath = resolve ( dirname ( packageJsonPath ) , schematics ) ;
101
60
}
102
61
103
62
try {
104
- if ( collectionPath ) {
105
- readJsonFile ( collectionPath ) ;
63
+ readJsonFile ( collectionPath ) ;
106
64
107
- return collectionPath ;
108
- }
65
+ return collectionPath ;
109
66
} catch ( e ) {
110
67
if (
111
68
e instanceof InvalidJsonCharacterException || e instanceof UnexpectedEndOfInputException
0 commit comments