@@ -100,53 +100,56 @@ export const codemodCamelToKebab = async ({ cwd }: { cwd: string }) => {
100100 cwd,
101101 ignore,
102102 } )
103+ console . log ( `Checking ${ postFiles . length } files for imports to update...` )
104+
103105 for ( const relPath of postFiles ) {
104106 const absPath = path . resolve ( cwd , relPath )
105107 let content = await fs . promises . readFile ( absPath , "utf8" )
108+ let changed = false
109+
110+ // helper to replace and track changes
111+ const replacer = ( _ : string , p2 : string ) => {
112+ const newPath = kebabifyPath ( p2 )
113+ if ( p2 !== newPath ) {
114+ changed = true
115+ return newPath
116+ }
117+ return p2
118+ }
106119
107120 // a1) update real imports: from "…"
108121 content = content . replace (
109122 / ( f r o m \s + [ ' " ` ] ) ( [ ^ ' " ` ] + ) ( [ ' " ` ] ) / g,
110- ( _full , p1 , p2 , p3 ) => {
111- const newPath = kebabifyPath ( p2 )
112- if ( p2 !== newPath ) console . log ( ` Fix: ${ p2 } -> ${ newPath } ` )
113- return `${ p1 } ${ newPath } ${ p3 } `
114- } ,
123+ ( _full , p1 , p2 , p3 ) => `${ p1 } ${ replacer ( _full , p2 ) } ${ p3 } ` ,
115124 )
116125
117126 // a2) update side-effect imports: import "…"
118127 content = content . replace (
119128 / ( i m p o r t \s + [ ' " ` ] ) ( [ ^ ' " ` ] + ) ( [ ' " ` ] ) / g,
120- ( _full , p1 , p2 , p3 ) => {
121- const newPath = kebabifyPath ( p2 )
122- if ( p2 !== newPath ) console . log ( ` Fix: ${ p2 } -> ${ newPath } ` )
123- return `${ p1 } ${ newPath } ${ p3 } `
124- } ,
129+ ( _full , p1 , p2 , p3 ) => `${ p1 } ${ replacer ( _full , p2 ) } ${ p3 } ` ,
125130 )
126131
127132 // a3) update dynamic imports & require: import("…"), require("…")
128133 content = content . replace (
129134 / ( (?: i m p o r t | r e q u i r e ) \s * \( \s * [ ' " ` ] ) ( [ ^ ' " ` ] + ) ( [ ' " ` ] \s * \) ) / g,
130- ( _full , p1 , p2 , p3 ) => {
131- const newPath = kebabifyPath ( p2 )
132- if ( p2 !== newPath ) console . log ( ` Fix: ${ p2 } -> ${ newPath } ` )
133- return `${ p1 } ${ newPath } ${ p3 } `
134- } ,
135+ ( _full , p1 , p2 , p3 ) => `${ p1 } ${ replacer ( _full , p2 ) } ${ p3 } ` ,
135136 )
136137
137138 // b) update Vitest/Jest mock imports: vi.mock("…"), jest.mock("…")
138139 content = content . replace (
139140 / ( (?: v i | j e s t ) \. (?: m o c k | i m p o r t A c t u a l | r e q u i r e A c t u a l ) \( \s * [ ' " ` ] ) ( [ ^ ' " ` ] + ) ( [ ' " ` ] ) / g,
140- ( _full , p1 , p2 , p3 ) => `${ p1 } ${ kebabifyPath ( p2 ) } ${ p3 } ` ,
141+ ( _full , p1 , p2 , p3 ) => `${ p1 } ${ replacer ( _full , p2 ) } ${ p3 } ` ,
141142 )
142143
143144 // c) update CSS url() paths: url("…")
144145 content = content . replace (
145146 / ( u r l \( \s * [ ' " ` ] ? ) ( [ ^ ) ' " ` ] + ) ( [ ' " ` ] ? \s * \) ) / g,
146- ( _full , p1 , p2 , p3 ) => `${ p1 } ${ kebabifyPath ( p2 ) } ${ p3 } ` ,
147+ ( _full , p1 , p2 , p3 ) => `${ p1 } ${ replacer ( _full , p2 ) } ${ p3 } ` ,
147148 )
148149
149- await fs . promises . writeFile ( absPath , content , "utf8" )
150+ if ( changed ) {
151+ await fs . promises . writeFile ( absPath , content , "utf8" )
152+ }
150153 }
151154
152155 console . log (
0 commit comments