1
+ import { existsSync , writeFileSync } from 'fs' ;
2
+ import * as os from 'os' ;
1
3
import * as path from 'path' ;
2
4
import {
3
5
JsonAstObject ,
@@ -19,44 +21,88 @@ function getSchemaLocation(): string {
19
21
20
22
export const workspaceSchemaPath = getSchemaLocation ( ) ;
21
23
22
- function configFilePath ( projectPath ?: string ) : string | null {
23
- const configNames = [ 'angular.json' , '.angular.json' ] ;
24
+ const configNames = [ 'angular.json' , '.angular.json' ] ;
25
+
26
+ function projectFilePath ( projectPath ?: string ) : string | null {
24
27
// Find the configuration, either where specified, in the Angular CLI project
25
28
// (if it's in node_modules) or from the current process.
26
29
return ( projectPath && findUp ( configNames , projectPath ) )
27
30
|| findUp ( configNames , process . cwd ( ) )
28
31
|| findUp ( configNames , __dirname ) ;
29
32
}
30
33
31
- let cachedWorkspace : experimental . workspace . Workspace | null | undefined = undefined ;
32
- export function getWorkspace ( ) : experimental . workspace . Workspace | null {
33
- if ( cachedWorkspace != undefined ) {
34
- return cachedWorkspace ;
34
+ function globalFilePath ( ) : string | null {
35
+ const home = os . homedir ( ) ;
36
+ if ( ! home ) {
37
+ return null ;
38
+ }
39
+
40
+ for ( const name of configNames ) {
41
+ const p = path . join ( home , name ) ;
42
+ if ( existsSync ( p ) ) {
43
+ return p ;
44
+ }
45
+ }
46
+
47
+ return null ;
48
+ }
49
+
50
+ const cachedWorkspaces = new Map < string , experimental . workspace . Workspace | null > ( ) ;
51
+
52
+ export function getWorkspace (
53
+ level : 'local' | 'global' = 'local' ,
54
+ ) : experimental . workspace . Workspace | null {
55
+ const cached = cachedWorkspaces . get ( level ) ;
56
+ if ( cached != undefined ) {
57
+ return cached ;
35
58
}
36
59
37
- const configPath = configFilePath ( ) ;
60
+ let configPath = level === 'local' ? projectFilePath ( ) : globalFilePath ( ) ;
38
61
39
62
if ( ! configPath ) {
40
- cachedWorkspace = null ;
41
- return null ;
63
+ if ( level === 'global' ) {
64
+ configPath = createGlobalSettings ( ) ;
65
+ } else {
66
+ cachedWorkspaces . set ( level , null ) ;
67
+ return null ;
68
+ }
42
69
}
43
70
44
- const root = path . dirname ( configPath ) ;
71
+ const root = normalize ( path . dirname ( configPath ) ) ;
72
+ const file = normalize ( path . basename ( configPath ) ) ;
45
73
const workspace = new experimental . workspace . Workspace (
46
- normalize ( root ) ,
74
+ root ,
47
75
new NodeJsSyncHost ( ) ,
48
76
) ;
49
77
50
- workspace . loadWorkspaceFromHost ( normalize ( path . basename ( configPath ) ) ) . subscribe ( ) ;
51
- cachedWorkspace = workspace ;
78
+ workspace . loadWorkspaceFromHost ( file ) . subscribe ( ) ;
79
+ cachedWorkspaces . set ( level , workspace ) ;
52
80
return workspace ;
53
81
}
54
82
55
- export function getWorkspaceRaw ( ) : [ JsonAstObject | null , string ] {
56
- const configPath = configFilePath ( ) ;
83
+ function createGlobalSettings ( ) : string {
84
+ const home = os . homedir ( ) ;
85
+ if ( ! home ) {
86
+ throw new Error ( 'No home directory found.' ) ;
87
+ }
88
+
89
+ const globalPath = path . join ( home , configNames [ 1 ] ) ;
90
+ writeFileSync ( globalPath , JSON . stringify ( { version : 1 } ) ) ;
91
+
92
+ return globalPath ;
93
+ }
94
+
95
+ export function getWorkspaceRaw (
96
+ level : 'local' | 'global' = 'local' ,
97
+ ) : [ JsonAstObject | null , string | null ] {
98
+ let configPath = level === 'local' ? projectFilePath ( ) : globalFilePath ( ) ;
57
99
58
100
if ( ! configPath ) {
59
- return null ;
101
+ if ( level === 'global' ) {
102
+ configPath = createGlobalSettings ( ) ;
103
+ } else {
104
+ return [ null , null ] ;
105
+ }
60
106
}
61
107
62
108
let content ;
@@ -89,19 +135,60 @@ export function validateWorkspace(json: JsonValue) {
89
135
return true ;
90
136
}
91
137
138
+ export function getProjectByCwd ( _workspace : experimental . workspace . Workspace ) : string | null {
139
+ // const cwd = process.cwd();
140
+ // TOOD: Implement project location logic
141
+ return null ;
142
+ }
143
+
92
144
export function getPackageManager ( ) : string {
93
- const workspace = getWorkspace ( ) ;
145
+ let workspace = getWorkspace ( ) ;
146
+
147
+ if ( workspace ) {
148
+ const project = getProjectByCwd ( workspace ) ;
149
+ if ( project && workspace . getProject ( project ) . cli ) {
150
+ const value = workspace . getProject ( project ) . cli [ 'packageManager' ] ;
151
+ if ( typeof value == 'string' ) {
152
+ return value ;
153
+ }
154
+ } else if ( workspace . getCli ( ) ) {
155
+ const value = workspace . getCli ( ) [ 'packageManager' ] ;
156
+ if ( typeof value == 'string' ) {
157
+ return value ;
158
+ }
159
+ }
160
+ }
161
+
162
+ workspace = getWorkspace ( 'global' ) ;
94
163
if ( workspace && workspace . getCli ( ) ) {
95
164
const value = workspace . getCli ( ) [ 'packageManager' ] ;
96
165
if ( typeof value == 'string' ) {
97
166
return value ;
98
167
}
99
168
}
169
+
100
170
return 'npm' ;
101
171
}
102
172
103
173
export function getDefaultSchematicCollection ( ) : string {
104
- const workspace = getWorkspace ( ) ;
174
+ let workspace = getWorkspace ( 'local' ) ;
175
+
176
+ if ( workspace ) {
177
+ const project = getProjectByCwd ( workspace ) ;
178
+ if ( project && workspace . getProject ( project ) . schematics ) {
179
+ const value = workspace . getProject ( project ) . schematics [ 'defaultCollection' ] ;
180
+ if ( typeof value == 'string' ) {
181
+ return value ;
182
+ }
183
+ } else if ( workspace . getSchematics ( ) ) {
184
+ const value = workspace . getSchematics ( ) [ 'defaultCollection' ] ;
185
+ if ( typeof value == 'string' ) {
186
+ return value ;
187
+ }
188
+ }
189
+ }
190
+
191
+ workspace = getWorkspace ( 'global' ) ;
105
192
if ( workspace && workspace . getSchematics ( ) ) {
106
193
const value = workspace . getSchematics ( ) [ 'defaultCollection' ] ;
107
194
if ( typeof value == 'string' ) {
@@ -113,13 +200,36 @@ export function getDefaultSchematicCollection(): string {
113
200
}
114
201
115
202
export function isWarningEnabled ( warning : string ) : boolean {
116
- const workspace = getWorkspace ( ) ;
203
+ let workspace = getWorkspace ( 'local' ) ;
204
+
205
+ if ( workspace ) {
206
+ const project = getProjectByCwd ( workspace ) ;
207
+ if ( project && workspace . getProject ( project ) . cli ) {
208
+ const warnings = workspace . getProject ( project ) . cli [ 'warnings' ] ;
209
+ if ( typeof warnings == 'object' && ! Array . isArray ( warnings ) ) {
210
+ const value = warnings [ warning ] ;
211
+ if ( typeof value == 'boolean' ) {
212
+ return value ;
213
+ }
214
+ }
215
+ } else if ( workspace . getCli ( ) ) {
216
+ const warnings = workspace . getCli ( ) [ 'warnings' ] ;
217
+ if ( typeof warnings == 'object' && ! Array . isArray ( warnings ) ) {
218
+ const value = warnings [ warning ] ;
219
+ if ( typeof value == 'boolean' ) {
220
+ return value ;
221
+ }
222
+ }
223
+ }
224
+ }
225
+
226
+ workspace = getWorkspace ( 'global' ) ;
117
227
if ( workspace && workspace . getCli ( ) ) {
118
228
const warnings = workspace . getCli ( ) [ 'warnings' ] ;
119
229
if ( typeof warnings == 'object' && ! Array . isArray ( warnings ) ) {
120
230
const value = warnings [ warning ] ;
121
- if ( value === false ) {
122
- return false ;
231
+ if ( typeof value == 'boolean' ) {
232
+ return value ;
123
233
}
124
234
}
125
235
}
0 commit comments