@@ -7,8 +7,10 @@ import {
7
7
JsonValue ,
8
8
experimental ,
9
9
normalize ,
10
+ parseJson ,
10
11
parseJsonAst ,
11
12
virtualFs ,
13
+ JsonObject ,
12
14
} from '@angular-devkit/core' ;
13
15
import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
14
16
import { findUp } from './find-up' ;
@@ -165,6 +167,58 @@ export function getPackageManager(): string {
165
167
return 'npm' ;
166
168
}
167
169
170
+ export function migrateLegacyGlobalConfig ( ) : boolean {
171
+ const homeDir = os . homedir ( ) ;
172
+ if ( homeDir ) {
173
+ const legacyGlobalConfigPath = path . join ( homeDir , '.angular-cli.json' ) ;
174
+ if ( existsSync ( legacyGlobalConfigPath ) ) {
175
+ const content = readFileSync ( legacyGlobalConfigPath , 'utf-8' ) ;
176
+ const legacy = parseJson ( content , JsonParseMode . Loose ) ;
177
+ if ( ! legacy || typeof legacy != 'object' || Array . isArray ( legacy ) ) {
178
+ return false ;
179
+ }
180
+
181
+ const cli : JsonObject = { } ;
182
+
183
+ if ( legacy . packageManager && typeof legacy . packageManager == 'string'
184
+ && legacy . packageManager !== 'default' ) {
185
+ cli [ 'packageManager' ] = legacy . packageManager ;
186
+ }
187
+
188
+ if ( legacy . defaults && typeof legacy . defaults == 'object' && ! Array . isArray ( legacy . defaults )
189
+ && legacy . defaults . schematics && typeof legacy . defaults . schematics == 'object'
190
+ && ! Array . isArray ( legacy . defaults . schematics )
191
+ && typeof legacy . defaults . schematics . collection == 'string' ) {
192
+ cli [ 'defaultCollection' ] = legacy . defaults . schematics . collection ;
193
+ }
194
+
195
+ if ( legacy . warnings && typeof legacy . warnings == 'object'
196
+ && ! Array . isArray ( legacy . warnings ) ) {
197
+
198
+ let warnings : JsonObject = { } ;
199
+ if ( typeof legacy . warnings . versionMismatch == 'boolean' ) {
200
+ warnings [ 'versionMismatch' ] = legacy . warnings . versionMismatch ;
201
+ }
202
+ if ( typeof legacy . warnings . typescriptMismatch == 'boolean' ) {
203
+ warnings [ 'typescriptMismatch' ] = legacy . warnings . typescriptMismatch ;
204
+ }
205
+
206
+ if ( Object . getOwnPropertyNames ( warnings ) . length > 0 ) {
207
+ cli [ 'warnings' ] = warnings ;
208
+ }
209
+ }
210
+
211
+ if ( Object . getOwnPropertyNames ( cli ) . length > 0 ) {
212
+ const globalPath = path . join ( homeDir , globalFileName ) ;
213
+ writeFileSync ( globalPath , JSON . stringify ( { version : 1 , cli } , null , 2 ) ) ;
214
+ return true ;
215
+ }
216
+ }
217
+ }
218
+
219
+ return false ;
220
+ }
221
+
168
222
// Fallback, check for packageManager in config file in v1.* global config.
169
223
function getLegacyPackageManager ( ) : string | null {
170
224
const homeDir = os . homedir ( ) ;
@@ -173,14 +227,14 @@ function getLegacyPackageManager(): string | null {
173
227
if ( existsSync ( legacyGlobalConfigPath ) ) {
174
228
const content = readFileSync ( legacyGlobalConfigPath , 'utf-8' ) ;
175
229
176
- const ast = parseJsonAst ( content , JsonParseMode . Loose ) ;
177
- if ( ast . kind != 'object' ) {
230
+ const legacy = parseJson ( content , JsonParseMode . Loose ) ;
231
+ if ( ! legacy || typeof legacy != 'object' || Array . isArray ( legacy ) ) {
178
232
return null ;
179
233
}
180
- const cfg = ast as JsonAstObject ;
181
- if ( cfg . value . packageManager && typeof cfg . value . packageManager === 'string' &&
182
- cfg . value . packageManager !== 'default' ) {
183
- return cfg . value . packageManager ;
234
+
235
+ if ( legacy . packageManager && typeof legacy . packageManager === 'string'
236
+ && legacy . packageManager !== 'default' ) {
237
+ return legacy . packageManager ;
184
238
}
185
239
}
186
240
}
0 commit comments