@@ -7,8 +7,10 @@ import {
77 JsonValue ,
88 experimental ,
99 normalize ,
10+ parseJson ,
1011 parseJsonAst ,
1112 virtualFs ,
13+ JsonObject ,
1214} from '@angular-devkit/core' ;
1315import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
1416import { findUp } from './find-up' ;
@@ -165,6 +167,58 @@ export function getPackageManager(): string {
165167 return 'npm' ;
166168}
167169
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+
168222// Fallback, check for packageManager in config file in v1.* global config.
169223function getLegacyPackageManager ( ) : string | null {
170224 const homeDir = os . homedir ( ) ;
@@ -173,14 +227,14 @@ function getLegacyPackageManager(): string | null {
173227 if ( existsSync ( legacyGlobalConfigPath ) ) {
174228 const content = readFileSync ( legacyGlobalConfigPath , 'utf-8' ) ;
175229
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 ) ) {
178232 return null ;
179233 }
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 ;
184238 }
185239 }
186240 }
0 commit comments