File tree Expand file tree Collapse file tree 5 files changed +50
-38
lines changed Expand file tree Collapse file tree 5 files changed +50
-38
lines changed Original file line number Diff line number Diff line change
1
+ import fs from 'node:fs'
2
+ import os from 'node:os'
3
+
4
+ export const cacheFilePath = `${ os . tmpdir ( ) } /__CUS_CACHE__`
5
+
6
+ type ConfigCacheItem = [ src : string , bak : string ]
7
+ export type ConfigCache = {
8
+ builtin : ConfigCacheItem [ ]
9
+ extension : ConfigCacheItem [ ]
10
+ }
11
+
12
+ let _cache : ConfigCache = {
13
+ builtin : [ ] ,
14
+ extension : [ ] ,
15
+ }
16
+ export function addBuiltinConfigCache ( src : string , bak : string ) {
17
+ _cache . builtin . push ( [ src , bak ] )
18
+ }
19
+
20
+ export function addExtensionConfigCache ( src : string , bak : string ) {
21
+ _cache . extension . push ( [ src , bak ] )
22
+ }
23
+
24
+ export function flushCache ( ) {
25
+ const data = JSON . stringify ( _cache , null , 2 )
26
+ fs . writeFileSync ( cacheFilePath , data , 'utf-8' )
27
+ }
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import fs from 'node:fs'
4
4
5
5
import { readFileSync , writeFileSync } from 'atomically'
6
6
7
+ import { addBuiltinConfigCache , addExtensionConfigCache } from '../cache'
7
8
import { log } from '../logger'
8
9
import { promptWarn } from '../utils'
9
10
@@ -19,7 +20,14 @@ export abstract class BaseFileManager implements FileManager {
19
20
constructor (
20
21
public srcPath : string ,
21
22
public bakPath : string ,
22
- ) { }
23
+ isExtension ?: boolean ,
24
+ ) {
25
+ if ( isExtension ) {
26
+ addExtensionConfigCache ( srcPath , bakPath )
27
+ } else {
28
+ addBuiltinConfigCache ( srcPath , bakPath )
29
+ }
30
+ }
23
31
24
32
get hasBakFile ( ) {
25
33
return fs . existsSync ( this . bakPath )
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import path from 'node:path'
4
4
5
5
import { extensions } from 'vscode'
6
6
7
+ import { flushCache } from '../cache'
7
8
import { config } from '../config'
8
9
import { log } from '../logger'
9
10
import { promptWarn } from '../utils'
@@ -53,7 +54,7 @@ function tryParseRegex(str: string): RegExp | string {
53
54
54
55
class ExtensionFileManager extends BaseFileManager {
55
56
constructor ( private config : ExtensionPatchConfig ) {
56
- super ( config . filePath , getBackupPath ( config . filePath ) )
57
+ super ( config . filePath , getBackupPath ( config . filePath ) , true )
57
58
}
58
59
59
60
patch ( content : string ) : Promisable < string > {
@@ -98,5 +99,6 @@ export function createExtensionFileManagers(skipWarnIfExtensionNotExist = false)
98
99
)
99
100
}
100
101
}
102
+ flushCache ( )
101
103
return managers
102
104
}
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import type { FileManager } from './base'
2
2
3
3
import { version } from 'vscode'
4
4
5
+ import { flushCache } from '../cache'
5
6
import { config } from '../config'
6
7
import { runAndRestart } from '../utils'
7
8
import { CssFileManager } from './css'
@@ -29,6 +30,7 @@ export function createFileManagers() {
29
30
new WebViewFileManager ( ) ,
30
31
new JsonFileManager ( ) , // MUST be the end of built-in file managers
31
32
]
33
+ flushCache ( )
32
34
33
35
return {
34
36
hasBakFile : ( ) => builtinManagers . every ( m => m . hasBakFile ) ,
Original file line number Diff line number Diff line change
1
+ import type { ConfigCache } from './cache'
2
+
1
3
import fs from 'node:fs'
2
4
3
- import { createExtensionFileManagers } from './manager/extension'
4
- import * as paths from './path'
5
+ import { cacheFilePath } from './cache'
5
6
6
7
function uninstall ( srcPath : string , bakPath : string ) {
7
8
if ( fs . existsSync ( srcPath ) ) {
8
9
fs . renameSync ( bakPath , srcPath )
9
10
}
10
11
}
11
12
12
- uninstall (
13
- paths . cssPath ,
14
- paths . cssBakPath ,
15
- )
16
-
17
- uninstall (
18
- paths . mainPath ,
19
- paths . mainBakPath ,
20
- )
21
-
22
- uninstall (
23
- paths . rendererPath ,
24
- paths . rendererBakPath ,
25
- )
26
-
27
- uninstall (
28
- paths . webviewHTMLPath ,
29
- paths . webviewHTMLBakPath ,
30
- )
31
-
32
- if ( paths . htmlPath ) {
33
- uninstall (
34
- paths . htmlPath ,
35
- paths . htmlBakPath ,
13
+ try {
14
+ const cache : ConfigCache = JSON . parse ( fs . readFileSync ( cacheFilePath , 'utf-8' ) )
15
+ Promise . all (
16
+ [ ...cache . builtin , ...cache . extension ]
17
+ . map ( async ( [ src , bak ] ) => uninstall ( src , bak ) ) ,
36
18
)
37
- }
38
-
39
- uninstall (
40
- paths . productJSONPath ,
41
- paths . productJSONBakPath ,
42
- )
43
-
44
- for ( const manager of createExtensionFileManagers ( true ) ) {
45
- uninstall ( manager . srcPath , manager . bakPath )
46
- }
19
+ } catch { }
You can’t perform that action at this time.
0 commit comments