@@ -16,15 +16,23 @@ import { highlight, info, relative, success, warn } from './utils/renderer'
1616// }
1717// }
1818export async function migratePostCSSConfig ( base : string ) {
19+ let ranMigration = false
1920 let didMigrate = false
2021 let didAddPostcssClient = false
2122 let didRemoveAutoprefixer = false
2223 let didRemovePostCSSImport = false
2324
25+ let packageJsonPath = path . resolve ( base , 'package.json' )
26+ let packageJson
27+ try {
28+ packageJson = JSON . parse ( await fs . readFile ( packageJsonPath , 'utf-8' ) )
29+ } catch { }
30+
2431 // Priority 1: Handle JS config files
2532 let jsConfigPath = await detectJSConfigPath ( base )
2633 if ( jsConfigPath ) {
2734 let result = await migratePostCSSJSConfig ( jsConfigPath )
35+ ranMigration = true
2836
2937 if ( result ) {
3038 didMigrate = true
@@ -35,50 +43,53 @@ export async function migratePostCSSConfig(base: string) {
3543 }
3644
3745 // Priority 2: Handle package.json config
38- let packageJsonPath = path . resolve ( base , 'package.json' )
39- let packageJson
40- try {
41- packageJson = JSON . parse ( await fs . readFile ( packageJsonPath , 'utf-8' ) )
42- } catch { }
43- if ( ! didMigrate && packageJson && 'postcss' in packageJson ) {
44- let result = await migratePostCSSJsonConfig ( packageJson . postcss )
46+ if ( ! ranMigration ) {
47+ if ( packageJson && 'postcss' in packageJson ) {
48+ let result = await migratePostCSSJsonConfig ( packageJson . postcss )
49+ ranMigration = true
4550
46- if ( result ) {
47- await fs . writeFile (
48- packageJsonPath ,
49- JSON . stringify ( { ...packageJson , postcss : result ?. json } , null , 2 ) ,
50- )
51+ if ( result ) {
52+ await fs . writeFile (
53+ packageJsonPath ,
54+ JSON . stringify ( { ...packageJson , postcss : result ?. json } , null , 2 ) ,
55+ )
5156
52- didMigrate = true
53- didAddPostcssClient = result . didAddPostcssClient
54- didRemoveAutoprefixer = result . didRemoveAutoprefixer
55- didRemovePostCSSImport = result . didRemovePostCSSImport
57+ didMigrate = true
58+ didAddPostcssClient = result . didAddPostcssClient
59+ didRemoveAutoprefixer = result . didRemoveAutoprefixer
60+ didRemovePostCSSImport = result . didRemovePostCSSImport
61+ }
5662 }
5763 }
5864
5965 // Priority 3: JSON based postcss config files
60- let jsonConfigPath = await detectJSONConfigPath ( base )
61- let jsonConfig : null | any = null
62- if ( ! didMigrate && jsonConfigPath ) {
63- try {
64- jsonConfig = JSON . parse ( await fs . readFile ( jsonConfigPath , 'utf-8' ) )
65- } catch { }
66- if ( jsonConfig ) {
67- let result = await migratePostCSSJsonConfig ( jsonConfig )
66+ if ( ! ranMigration ) {
67+ let jsonConfigPath = await detectJSONConfigPath ( base )
68+ let jsonConfig : null | any = null
69+ if ( jsonConfigPath ) {
70+ try {
71+ jsonConfig = JSON . parse ( await fs . readFile ( jsonConfigPath , 'utf-8' ) )
72+ } catch { }
73+ if ( jsonConfig ) {
74+ let result = await migratePostCSSJsonConfig ( jsonConfig )
75+ ranMigration = true
6876
69- if ( result ) {
70- await fs . writeFile ( jsonConfigPath , JSON . stringify ( result . json , null , 2 ) )
77+ if ( result ) {
78+ await fs . writeFile ( jsonConfigPath , JSON . stringify ( result . json , null , 2 ) )
7179
72- didMigrate = true
73- didAddPostcssClient = result . didAddPostcssClient
74- didRemoveAutoprefixer = result . didRemoveAutoprefixer
75- didRemovePostCSSImport = result . didRemovePostCSSImport
80+ didMigrate = true
81+ didAddPostcssClient = result . didAddPostcssClient
82+ didRemoveAutoprefixer = result . didRemoveAutoprefixer
83+ didRemovePostCSSImport = result . didRemovePostCSSImport
84+ }
7685 }
7786 }
7887 }
7988
80- if ( ! didMigrate ) {
81- info ( 'No PostCSS config found, skipping migration.' )
89+ if ( ! ranMigration ) {
90+ info ( 'No PostCSS config found, skipping migration.' , {
91+ prefix : '↳ ' ,
92+ } )
8293 return
8394 }
8495
@@ -96,16 +107,18 @@ export async function migratePostCSSConfig(base: string) {
96107 } catch { }
97108 }
98109 }
99- if ( didRemoveAutoprefixer || didRemovePostCSSImport ) {
110+
111+ if ( didRemoveAutoprefixer ) {
100112 try {
101- let packagesToRemove = [
102- didRemoveAutoprefixer ? 'autoprefixer' : null ,
103- didRemovePostCSSImport ? 'postcss-import' : null ,
104- ] . filter ( Boolean ) as string [ ]
105- await pkg ( base ) . remove ( packagesToRemove )
106- for ( let pkg of packagesToRemove ) {
107- success ( `Removed package: ${ highlight ( pkg ) } ` , { prefix : '↳ ' } )
108- }
113+ await pkg ( base ) . remove ( [ 'autoprefixer' ] )
114+ success ( `Removed package: ${ highlight ( 'autoprefixer' ) } ` , { prefix : '↳ ' } )
115+ } catch { }
116+ }
117+
118+ if ( didRemovePostCSSImport ) {
119+ try {
120+ await pkg ( base ) . remove ( [ 'postcss-import' ] )
121+ success ( `Removed package: ${ highlight ( 'postcss-import' ) } ` , { prefix : '↳ ' } )
109122 } catch { }
110123 }
111124
@@ -138,7 +151,9 @@ async function migratePostCSSJSConfig(configPath: string): Promise<{
138151
139152 let isSimpleConfig = await isSimplePostCSSConfig ( configPath )
140153 if ( ! isSimpleConfig ) {
141- warn ( 'The PostCSS config contains dynamic JavaScript and can not be automatically migrated.' )
154+ warn ( 'The PostCSS config contains dynamic JavaScript and can not be automatically migrated.' , {
155+ prefix : '↳ ' ,
156+ } )
142157 return null
143158 }
144159
0 commit comments