7
7
*/
8
8
9
9
import { json , workspaces } from '@angular-devkit/core' ;
10
- import { existsSync , promises as fs , writeFileSync } from 'fs' ;
10
+ import { existsSync , promises as fs } from 'fs' ;
11
11
import * as os from 'os' ;
12
12
import * as path from 'path' ;
13
13
import { PackageManager } from '../../lib/config/workspace-schema' ;
@@ -51,6 +51,7 @@ export const workspaceSchemaPath = path.join(__dirname, '../../lib/config/schema
51
51
52
52
const configNames = [ 'angular.json' , '.angular.json' ] ;
53
53
const globalFileName = '.angular-config.json' ;
54
+ const defaultGlobalFilePath = path . join ( os . homedir ( ) , globalFileName ) ;
54
55
55
56
function xdgConfigHome ( home : string , configFile ?: string ) : string {
56
57
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
@@ -106,9 +107,8 @@ function globalFilePath(): string | null {
106
107
return xdgConfigOld ;
107
108
}
108
109
109
- const p = path . join ( home , globalFileName ) ;
110
- if ( existsSync ( p ) ) {
111
- return p ;
110
+ if ( existsSync ( defaultGlobalFilePath ) ) {
111
+ return defaultGlobalFilePath ;
112
112
}
113
113
114
114
return null ;
@@ -147,7 +147,12 @@ export class AngularWorkspace {
147
147
}
148
148
149
149
save ( ) : Promise < void > {
150
- return workspaces . writeWorkspace ( this . workspace , createWorkspaceHost ( ) , this . filePath ) ;
150
+ return workspaces . writeWorkspace (
151
+ this . workspace ,
152
+ createWorkspaceHost ( ) ,
153
+ this . filePath ,
154
+ workspaces . WorkspaceFormat . JSON ,
155
+ ) ;
151
156
}
152
157
153
158
static async load ( workspaceFilePath : string ) : Promise < AngularWorkspace > {
@@ -162,22 +167,38 @@ export class AngularWorkspace {
162
167
}
163
168
164
169
const cachedWorkspaces = new Map < string , AngularWorkspace | undefined > ( ) ;
170
+
171
+ export async function getWorkspace ( level : 'global' ) : Promise < AngularWorkspace > ;
172
+ export async function getWorkspace ( level : 'local' ) : Promise < AngularWorkspace | undefined > ;
165
173
export async function getWorkspace (
166
- level : 'local' | 'global' = 'local' ,
174
+ level : 'local' | 'global' ,
175
+ ) : Promise < AngularWorkspace | undefined > ;
176
+
177
+ export async function getWorkspace (
178
+ level : 'local' | 'global' ,
167
179
) : Promise < AngularWorkspace | undefined > {
168
180
if ( cachedWorkspaces . has ( level ) ) {
169
181
return cachedWorkspaces . get ( level ) ;
170
182
}
171
183
172
- let configPath = level === 'local' ? projectFilePath ( ) : globalFilePath ( ) ;
184
+ const configPath = level === 'local' ? projectFilePath ( ) : globalFilePath ( ) ;
173
185
if ( ! configPath ) {
174
- if ( level === 'local' ) {
175
- cachedWorkspaces . set ( level , undefined ) ;
186
+ if ( level === 'global' ) {
187
+ // Unlike a local config, a global config is not mandatory.
188
+ // So we create an empty one in memory and keep it as such until it has been modified and saved.
189
+ const globalWorkspace = new AngularWorkspace (
190
+ { extensions : { } , projects : new workspaces . ProjectDefinitionCollection ( ) } ,
191
+ defaultGlobalFilePath ,
192
+ ) ;
193
+
194
+ cachedWorkspaces . set ( level , globalWorkspace ) ;
176
195
177
- return undefined ;
196
+ return globalWorkspace ;
178
197
}
179
198
180
- configPath = createGlobalSettings ( ) ;
199
+ cachedWorkspaces . set ( level , undefined ) ;
200
+
201
+ return undefined ;
181
202
}
182
203
183
204
try {
@@ -193,26 +214,24 @@ export async function getWorkspace(
193
214
}
194
215
}
195
216
196
- export function createGlobalSettings ( ) : string {
197
- const home = os . homedir ( ) ;
198
- if ( ! home ) {
199
- throw new Error ( 'No home directory found.' ) ;
200
- }
201
-
202
- const globalPath = path . join ( home , globalFileName ) ;
203
- writeFileSync ( globalPath , JSON . stringify ( { version : 1 } ) ) ;
204
-
205
- return globalPath ;
206
- }
207
-
208
- export function getWorkspaceRaw (
217
+ /**
218
+ * This method will load the workspace configuration in raw JSON format.
219
+ * When `level` is `global` and file doesn't exists, it will be created.
220
+ *
221
+ * NB: This method is intended to be used only for `ng config`.
222
+ */
223
+ export async function getWorkspaceRaw (
209
224
level : 'local' | 'global' = 'local' ,
210
- ) : [ JSONFile | null , string | null ] {
225
+ ) : Promise < [ JSONFile | null , string | null ] > {
211
226
let configPath = level === 'local' ? projectFilePath ( ) : globalFilePath ( ) ;
212
227
213
228
if ( ! configPath ) {
214
229
if ( level === 'global' ) {
215
- configPath = createGlobalSettings ( ) ;
230
+ configPath = defaultGlobalFilePath ;
231
+ // Config doesn't exist, force create it.
232
+
233
+ const globalWorkspace = await getWorkspace ( 'global' ) ;
234
+ await globalWorkspace . save ( ) ;
216
235
} else {
217
236
return [ null , null ] ;
218
237
}
0 commit comments