@@ -11,82 +11,44 @@ import {
11
11
basename ,
12
12
dirname ,
13
13
experimental ,
14
- join ,
15
14
normalize ,
16
15
virtualFs ,
17
16
} from '@angular-devkit/core' ;
18
- import * as fs from 'fs' ;
19
- import { homedir } from 'os' ;
20
- import { Observable , of } from 'rxjs' ;
21
- import { concatMap , tap } from 'rxjs/operators' ;
22
17
import { findUp } from '../utilities/find-up' ;
23
18
24
19
25
- // TODO: error out instead of returning null when workspace cannot be found.
26
20
export class WorkspaceLoader {
27
- private _workspaceCacheMap = new Map < string , experimental . workspace . Workspace > ( ) ;
28
21
// TODO: add remaining fallbacks.
29
22
private _configFileNames = [
30
23
normalize ( '.angular.json' ) ,
31
24
normalize ( 'angular.json' ) ,
32
25
] ;
33
26
constructor ( private _host : virtualFs . Host ) { }
34
27
35
- loadGlobalWorkspace ( ) : Observable < experimental . workspace . Workspace | null > {
36
- return this . _getGlobalWorkspaceFilePath ( ) . pipe (
37
- concatMap ( globalWorkspacePath => this . _loadWorkspaceFromPath ( globalWorkspacePath ) ) ,
38
- ) ;
39
- }
40
-
41
- loadWorkspace ( projectPath ?: string ) : Observable < experimental . workspace . Workspace | null > {
42
- return this . _getProjectWorkspaceFilePath ( projectPath ) . pipe (
43
- concatMap ( globalWorkspacePath => this . _loadWorkspaceFromPath ( globalWorkspacePath ) ) ,
44
- ) ;
28
+ loadWorkspace ( projectPath ?: string ) : Promise < experimental . workspace . Workspace > {
29
+ return this . _loadWorkspaceFromPath ( this . _getProjectWorkspaceFilePath ( projectPath ) ) ;
45
30
}
46
31
47
32
// TODO: do this with the host instead of fs.
48
- private _getProjectWorkspaceFilePath ( projectPath ?: string ) : Observable < Path | null > {
33
+ private _getProjectWorkspaceFilePath ( projectPath ?: string ) : Path {
49
34
// Find the workspace file, either where specified, in the Angular CLI project
50
35
// (if it's in node_modules) or from the current process.
51
36
const workspaceFilePath = ( projectPath && findUp ( this . _configFileNames , projectPath ) )
52
37
|| findUp ( this . _configFileNames , process . cwd ( ) )
53
38
|| findUp ( this . _configFileNames , __dirname ) ;
54
39
55
40
if ( workspaceFilePath ) {
56
- return of ( normalize ( workspaceFilePath ) ) ;
41
+ return normalize ( workspaceFilePath ) ;
57
42
} else {
58
43
throw new Error ( `Local workspace file ('angular.json') could not be found.` ) ;
59
44
}
60
45
}
61
46
62
- // TODO: do this with the host instead of fs.
63
- private _getGlobalWorkspaceFilePath ( ) : Observable < Path | null > {
64
- for ( const fileName of this . _configFileNames ) {
65
- const workspaceFilePath = join ( normalize ( homedir ( ) ) , fileName ) ;
66
-
67
- if ( fs . existsSync ( workspaceFilePath ) ) {
68
- return of ( normalize ( workspaceFilePath ) ) ;
69
- }
70
- }
71
-
72
- return of ( null ) ;
73
- }
74
-
75
- private _loadWorkspaceFromPath ( workspacePath : Path | null ) {
76
- if ( ! workspacePath ) {
77
- return of ( null ) ;
78
- }
79
-
80
- if ( this . _workspaceCacheMap . has ( workspacePath ) ) {
81
- return of ( this . _workspaceCacheMap . get ( workspacePath ) || null ) ;
82
- }
83
-
47
+ private _loadWorkspaceFromPath ( workspacePath : Path ) {
84
48
const workspaceRoot = dirname ( workspacePath ) ;
85
49
const workspaceFileName = basename ( workspacePath ) ;
86
50
const workspace = new experimental . workspace . Workspace ( workspaceRoot , this . _host ) ;
87
51
88
- return workspace . loadWorkspaceFromHost ( workspaceFileName ) . pipe (
89
- tap ( workspace => this . _workspaceCacheMap . set ( workspacePath , workspace ) ) ,
90
- ) ;
52
+ return workspace . loadWorkspaceFromHost ( workspaceFileName ) . toPromise ( ) ;
91
53
}
92
54
}
0 commit comments