@@ -282,26 +282,44 @@ class Updater {
282282 if ( this . latestVersionInfo ) {
283283 await this . fs . writeJson ( this . path . join ( src , "bin/version.json" ) , this . latestVersionInfo )
284284 }
285- await this . fs . ensureDir ( this . path . dirname ( dst ) )
285+ if ( ! ( await this . utils . existPath ( dst ) ) ) {
286+ throw new Error ( "Target plugin directory does not exist" )
287+ }
288+
289+ const moveContent = async ( fromDir , toDir ) => {
290+ await this . fs . ensureDir ( toDir )
291+ const items = await this . fs . readdir ( fromDir )
292+ for ( const item of items ) {
293+ const srcPath = this . path . join ( fromDir , item )
294+ const destPath = this . path . join ( toDir , item )
295+ await this . fs . move ( srcPath , destPath , { overwrite : true } )
296+ }
297+ }
286298
287- let backedUp = false
299+ await this . fs . emptyDir ( backup )
288300 try {
289- if ( await this . utils . existPath ( dst ) ) {
290- await this . fs . move ( dst , backup , { overwrite : true } )
291- backedUp = true
301+ await moveContent ( dst , backup )
302+ } catch ( err ) {
303+ console . error ( "Backup failed" , err )
304+ try {
305+ await moveContent ( backup , dst )
306+ } catch ( restoreErr ) {
307+ console . error ( "Failed to restore partial backup" , restoreErr )
292308 }
293- await this . fs . move ( src , dst , { overwrite : true } )
309+ throw err
310+ }
311+
312+ try {
313+ await this . fs . copy ( src , dst , { overwrite : true , preserveTimestamps : true } )
294314 } catch ( error ) {
295315 console . error ( "Critical Error during sync! Rolling back..." , error )
296- if ( backedUp ) {
297- try {
298- await this . fs . remove ( dst )
299- await this . fs . move ( backup , dst )
300- console . log ( "Rollback successful." )
301- } catch ( rollbackError ) {
302- console . error ( "FATAL: Rollback failed! Please restore manually." , rollbackError )
303- throw rollbackError
304- }
316+ try {
317+ await this . fs . emptyDir ( dst )
318+ await moveContent ( backup , dst )
319+ console . log ( "Rollback successful." )
320+ } catch ( rollbackError ) {
321+ console . error ( "FATAL: Rollback failed!" , rollbackError )
322+ throw rollbackError
305323 }
306324 throw error
307325 }
0 commit comments