@@ -16,15 +16,23 @@ import { highlight, info, relative, success, warn } from './utils/renderer'
16
16
// }
17
17
// }
18
18
export async function migratePostCSSConfig ( base : string ) {
19
+ let ranMigration = false
19
20
let didMigrate = false
20
21
let didAddPostcssClient = false
21
22
let didRemoveAutoprefixer = false
22
23
let didRemovePostCSSImport = false
23
24
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
+
24
31
// Priority 1: Handle JS config files
25
32
let jsConfigPath = await detectJSConfigPath ( base )
26
33
if ( jsConfigPath ) {
27
34
let result = await migratePostCSSJSConfig ( jsConfigPath )
35
+ ranMigration = true
28
36
29
37
if ( result ) {
30
38
didMigrate = true
@@ -35,50 +43,53 @@ export async function migratePostCSSConfig(base: string) {
35
43
}
36
44
37
45
// 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
45
50
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
+ )
51
56
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
+ }
56
62
}
57
63
}
58
64
59
65
// 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
68
76
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 ) )
71
79
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
+ }
76
85
}
77
86
}
78
87
}
79
88
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
+ } )
82
93
return
83
94
}
84
95
@@ -96,16 +107,18 @@ export async function migratePostCSSConfig(base: string) {
96
107
} catch { }
97
108
}
98
109
}
99
- if ( didRemoveAutoprefixer || didRemovePostCSSImport ) {
110
+
111
+ if ( didRemoveAutoprefixer ) {
100
112
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 : '↳ ' } )
109
122
} catch { }
110
123
}
111
124
@@ -138,7 +151,9 @@ async function migratePostCSSJSConfig(configPath: string): Promise<{
138
151
139
152
let isSimpleConfig = await isSimplePostCSSConfig ( configPath )
140
153
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
+ } )
142
157
return null
143
158
}
144
159
0 commit comments