5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { AbsoluteFsPath , FileSystem , PathSegment } from '../../../src/ngtsc/file_system' ;
8
+ import { AbsoluteFsPath } from '../../../src/ngtsc/file_system' ;
9
9
import { Logger } from '../../../src/ngtsc/logging' ;
10
10
import { EntryPointWithDependencies } from '../dependencies/dependency_host' ;
11
11
import { DependencyResolver , SortedEntryPointsInfo } from '../dependencies/dependency_resolver' ;
12
- import { NgccConfiguration } from '../packages/configuration' ;
13
- import { getEntryPointInfo , IGNORED_ENTRY_POINT , INCOMPATIBLE_ENTRY_POINT , isEntryPoint , NO_ENTRY_POINT } from '../packages/entry_point' ;
14
12
import { EntryPointManifest } from '../packages/entry_point_manifest' ;
15
13
import { PathMappings } from '../path_mappings' ;
16
- import { NGCC_DIRECTORY } from '../writing/new_entry_point_file_writer' ;
17
14
15
+ import { EntryPointCollector } from './entry_point_collector' ;
18
16
import { EntryPointFinder } from './interface' ;
19
17
import { getBasePaths , trackDuration } from './utils' ;
20
18
@@ -25,9 +23,11 @@ import {getBasePaths, trackDuration} from './utils';
25
23
export class DirectoryWalkerEntryPointFinder implements EntryPointFinder {
26
24
private basePaths = getBasePaths ( this . logger , this . sourceDirectory , this . pathMappings ) ;
27
25
constructor (
28
- private fs : FileSystem , private config : NgccConfiguration , private logger : Logger ,
29
- private resolver : DependencyResolver , private entryPointManifest : EntryPointManifest ,
30
- private sourceDirectory : AbsoluteFsPath , private pathMappings : PathMappings | undefined ) { }
26
+ private logger : Logger , private resolver : DependencyResolver ,
27
+ private entryPointCollector : EntryPointCollector ,
28
+ private entryPointManifest : EntryPointManifest , private sourceDirectory : AbsoluteFsPath ,
29
+ private pathMappings : PathMappings | undefined ) { }
30
+
31
31
/**
32
32
* Search the `sourceDirectory`, and sub-directories, using `pathMappings` as necessary, to find
33
33
* all package entry-points.
@@ -45,145 +45,16 @@ export class DirectoryWalkerEntryPointFinder implements EntryPointFinder {
45
45
/**
46
46
* Search the `basePath` for possible Angular packages and entry-points.
47
47
*
48
- * @param basePath The path at which to start the search
48
+ * @param basePath The path at which to start the search.
49
49
* @returns an array of `EntryPoint`s that were found within `basePath`.
50
50
*/
51
51
walkBasePathForPackages ( basePath : AbsoluteFsPath ) : EntryPointWithDependencies [ ] {
52
52
this . logger . debug (
53
53
`No manifest found for ${ basePath } so walking the directories for entry-points.` ) ;
54
54
const entryPoints = trackDuration (
55
- ( ) => this . walkDirectoryForPackages ( basePath ) ,
55
+ ( ) => this . entryPointCollector . walkDirectoryForPackages ( basePath ) ,
56
56
duration => this . logger . debug ( `Walking ${ basePath } for entry-points took ${ duration } s.` ) ) ;
57
57
this . entryPointManifest . writeEntryPointManifest ( basePath , entryPoints ) ;
58
58
return entryPoints ;
59
59
}
60
-
61
- /**
62
- * Look for Angular packages that need to be compiled, starting at the source directory.
63
- * The function will recurse into directories that start with `@...`, e.g. `@angular/...`.
64
- *
65
- * @param sourceDirectory An absolute path to the root directory where searching begins.
66
- * @returns an array of `EntryPoint`s that were found within `sourceDirectory`.
67
- */
68
- walkDirectoryForPackages ( sourceDirectory : AbsoluteFsPath ) : EntryPointWithDependencies [ ] {
69
- // Try to get a primary entry point from this directory
70
- const primaryEntryPoint =
71
- getEntryPointInfo ( this . fs , this . config , this . logger , sourceDirectory , sourceDirectory ) ;
72
-
73
- // If there is an entry-point but it is not compatible with ngcc (it has a bad package.json or
74
- // invalid typings) then exit. It is unlikely that such an entry point has a dependency on an
75
- // Angular library.
76
- if ( primaryEntryPoint === INCOMPATIBLE_ENTRY_POINT ) {
77
- return [ ] ;
78
- }
79
-
80
- const entryPoints : EntryPointWithDependencies [ ] = [ ] ;
81
- if ( primaryEntryPoint !== NO_ENTRY_POINT ) {
82
- if ( primaryEntryPoint !== IGNORED_ENTRY_POINT ) {
83
- entryPoints . push ( this . resolver . getEntryPointWithDependencies ( primaryEntryPoint ) ) ;
84
- }
85
- this . collectSecondaryEntryPoints (
86
- entryPoints , sourceDirectory , sourceDirectory , this . fs . readdir ( sourceDirectory ) ) ;
87
-
88
- // Also check for any nested node_modules in this package but only if at least one of the
89
- // entry-points was compiled by Angular.
90
- if ( entryPoints . some ( e => e . entryPoint . compiledByAngular ) ) {
91
- const nestedNodeModulesPath = this . fs . join ( sourceDirectory , 'node_modules' ) ;
92
- if ( this . fs . exists ( nestedNodeModulesPath ) ) {
93
- entryPoints . push ( ...this . walkDirectoryForPackages ( nestedNodeModulesPath ) ) ;
94
- }
95
- }
96
-
97
- return entryPoints ;
98
- }
99
-
100
- // The `sourceDirectory` was not a package (i.e. there was no package.json)
101
- // So search its sub-directories for Angular packages and entry-points
102
- for ( const path of this . fs . readdir ( sourceDirectory ) ) {
103
- if ( isIgnorablePath ( path ) ) {
104
- // Ignore hidden files, node_modules and ngcc directory
105
- continue ;
106
- }
107
-
108
- const absolutePath = this . fs . resolve ( sourceDirectory , path ) ;
109
- const stat = this . fs . lstat ( absolutePath ) ;
110
- if ( stat . isSymbolicLink ( ) || ! stat . isDirectory ( ) ) {
111
- // Ignore symbolic links and non-directories
112
- continue ;
113
- }
114
-
115
- entryPoints . push ( ...this . walkDirectoryForPackages ( this . fs . join ( sourceDirectory , path ) ) ) ;
116
- }
117
-
118
- return entryPoints ;
119
- }
120
-
121
- /**
122
- * Search the `directory` looking for any secondary entry-points for a package, adding any that
123
- * are found to the `entryPoints` array.
124
- *
125
- * @param entryPoints An array where we will add any entry-points found in this directory
126
- * @param packagePath The absolute path to the package that may contain entry-points
127
- * @param directory The current directory being searched
128
- * @param paths The paths contained in the current `directory`.
129
- */
130
- private collectSecondaryEntryPoints (
131
- entryPoints : EntryPointWithDependencies [ ] , packagePath : AbsoluteFsPath ,
132
- directory : AbsoluteFsPath , paths : PathSegment [ ] ) : void {
133
- for ( const path of paths ) {
134
- if ( isIgnorablePath ( path ) ) {
135
- // Ignore hidden files, node_modules and ngcc directory
136
- continue ;
137
- }
138
-
139
- const absolutePath = this . fs . resolve ( directory , path ) ;
140
- const stat = this . fs . lstat ( absolutePath ) ;
141
- if ( stat . isSymbolicLink ( ) ) {
142
- // Ignore symbolic links
143
- continue ;
144
- }
145
-
146
- const isDirectory = stat . isDirectory ( ) ;
147
- if ( ! path . endsWith ( '.js' ) && ! isDirectory ) {
148
- // Ignore files that do not end in `.js`
149
- continue ;
150
- }
151
-
152
- // If the path is a JS file then strip its extension and see if we can match an
153
- // entry-point (even if it is an ignored one).
154
- const possibleEntryPointPath = isDirectory ? absolutePath : stripJsExtension ( absolutePath ) ;
155
- const subEntryPoint =
156
- getEntryPointInfo ( this . fs , this . config , this . logger , packagePath , possibleEntryPointPath ) ;
157
- if ( isEntryPoint ( subEntryPoint ) ) {
158
- entryPoints . push ( this . resolver . getEntryPointWithDependencies ( subEntryPoint ) ) ;
159
- }
160
-
161
- if ( ! isDirectory ) {
162
- // This path is not a directory so we are done.
163
- continue ;
164
- }
165
-
166
- // If not an entry-point itself, this directory may contain entry-points of its own.
167
- const canContainEntryPoints =
168
- subEntryPoint === NO_ENTRY_POINT || subEntryPoint === INCOMPATIBLE_ENTRY_POINT ;
169
- const childPaths = this . fs . readdir ( absolutePath ) ;
170
- if ( canContainEntryPoints &&
171
- childPaths . some (
172
- childPath => childPath . endsWith ( '.js' ) &&
173
- this . fs . stat ( this . fs . resolve ( absolutePath , childPath ) ) . isFile ( ) ) ) {
174
- // We do not consider non-entry-point directories that contain JS files as they are very
175
- // unlikely to be containers for sub-entry-points.
176
- continue ;
177
- }
178
- this . collectSecondaryEntryPoints ( entryPoints , packagePath , absolutePath , childPaths ) ;
179
- }
180
- }
181
- }
182
-
183
- function stripJsExtension < T extends string > ( filePath : T ) : T {
184
- return filePath . replace ( / \. j s $ / , '' ) as T ;
185
- }
186
-
187
- function isIgnorablePath ( path : PathSegment ) : boolean {
188
- return path . startsWith ( '.' ) || path === 'node_modules' || path === NGCC_DIRECTORY ;
189
60
}
0 commit comments