@@ -5,11 +5,13 @@ import fs from 'node:fs'
5
5
import { readFileSync , writeFileSync } from 'atomically'
6
6
7
7
import { log } from '../logger'
8
+ import { promptWarn } from '../utils'
8
9
9
10
export interface FileManager {
10
11
hasBakFile : boolean
11
12
reload : ( ) => Promise < void >
12
13
rollback : ( ) => Promise < void >
14
+ skipAll ?: ( ) => Promisable < string | false | undefined >
13
15
}
14
16
15
17
export abstract class BaseFileManager implements FileManager {
@@ -22,24 +24,41 @@ export abstract class BaseFileManager implements FileManager {
22
24
return fs . existsSync ( this . bakPath )
23
25
}
24
26
27
+ skipAll ?: ( ( ) => Promisable < string | undefined | false > ) | undefined
28
+
25
29
async reload ( ) {
26
- if ( ! this . hasBakFile ) {
27
- log . warn ( `Backup file [${ this . bakPath } ] does not exist, backuping...` )
28
- fs . cpSync ( this . srcPath , this . bakPath )
29
- log . info ( `Create backup file [${ this . bakPath } ]` )
30
- }
31
- const newContent = await this . patch ( readFileSync ( this . bakPath , 'utf-8' ) )
32
- writeFileSync ( this . srcPath , newContent )
33
- log . info ( `Config reload [${ this . srcPath } ]` )
30
+ await this . skipable ( async ( ) => {
31
+ if ( ! this . hasBakFile ) {
32
+ log . warn ( `Backup file [${ this . bakPath } ] does not exist, backuping...` )
33
+ fs . cpSync ( this . srcPath , this . bakPath )
34
+ log . info ( `Create backup file [${ this . bakPath } ]` )
35
+ }
36
+ const newContent = await this . patch ( readFileSync ( this . bakPath , 'utf-8' ) )
37
+ writeFileSync ( this . srcPath , newContent )
38
+ log . info ( `Config reload [${ this . srcPath } ]` )
39
+ } )
34
40
}
35
41
36
42
async rollback ( ) {
37
- if ( ! this . hasBakFile ) {
38
- log . warn ( `Backup file [${ this . bakPath } ] does not exist, skip rollback` )
39
- } else {
40
- writeFileSync ( this . srcPath , readFileSync ( this . bakPath , 'utf-8' ) )
41
- log . info ( `Config rollback [${ this . srcPath } ]` )
43
+ await this . skipable ( ( ) => {
44
+ if ( ! this . hasBakFile ) {
45
+ log . warn ( `Backup file [${ this . bakPath } ] does not exist, skip rollback` )
46
+ } else {
47
+ writeFileSync ( this . srcPath , readFileSync ( this . bakPath , 'utf-8' ) )
48
+ log . info ( `Config rollback [${ this . srcPath } ]` )
49
+ }
50
+ } )
51
+ }
52
+
53
+ async skipable ( fn : ( ) => Promisable < void > ) {
54
+ let skipMessage
55
+ // eslint-disable-next-line no-cond-assign
56
+ if ( skipMessage = await this . skipAll ?.( ) ) {
57
+ promptWarn ( skipMessage )
58
+ return
42
59
}
60
+
61
+ await fn ( )
43
62
}
44
63
45
64
abstract patch ( content : string ) : Promisable < string >
0 commit comments