Skip to content

Commit de137f7

Browse files
committed
fix: correct uninstall script
1 parent 30568cb commit de137f7

File tree

5 files changed

+50
-38
lines changed

5 files changed

+50
-38
lines changed

src/cache.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

src/manager/base.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fs from 'node:fs'
44

55
import { readFileSync, writeFileSync } from 'atomically'
66

7+
import { addBuiltinConfigCache, addExtensionConfigCache } from '../cache'
78
import { log } from '../logger'
89
import { promptWarn } from '../utils'
910

@@ -19,7 +20,14 @@ export abstract class BaseFileManager implements FileManager {
1920
constructor(
2021
public srcPath: string,
2122
public bakPath: string,
22-
) { }
23+
isExtension?: boolean,
24+
) {
25+
if (isExtension) {
26+
addExtensionConfigCache(srcPath, bakPath)
27+
} else {
28+
addBuiltinConfigCache(srcPath, bakPath)
29+
}
30+
}
2331

2432
get hasBakFile() {
2533
return fs.existsSync(this.bakPath)

src/manager/extension.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'node:path'
44

55
import { extensions } from 'vscode'
66

7+
import { flushCache } from '../cache'
78
import { config } from '../config'
89
import { log } from '../logger'
910
import { promptWarn } from '../utils'
@@ -53,7 +54,7 @@ function tryParseRegex(str: string): RegExp | string {
5354

5455
class ExtensionFileManager extends BaseFileManager {
5556
constructor(private config: ExtensionPatchConfig) {
56-
super(config.filePath, getBackupPath(config.filePath))
57+
super(config.filePath, getBackupPath(config.filePath), true)
5758
}
5859

5960
patch(content: string): Promisable<string> {
@@ -98,5 +99,6 @@ export function createExtensionFileManagers(skipWarnIfExtensionNotExist = false)
9899
)
99100
}
100101
}
102+
flushCache()
101103
return managers
102104
}

src/manager/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FileManager } from './base'
22

33
import { version } from 'vscode'
44

5+
import { flushCache } from '../cache'
56
import { config } from '../config'
67
import { runAndRestart } from '../utils'
78
import { CssFileManager } from './css'
@@ -29,6 +30,7 @@ export function createFileManagers() {
2930
new WebViewFileManager(),
3031
new JsonFileManager(), // MUST be the end of built-in file managers
3132
]
33+
flushCache()
3234

3335
return {
3436
hasBakFile: () => builtinManagers.every(m => m.hasBakFile),

src/uninstall.ts

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,19 @@
1+
import type { ConfigCache } from './cache'
2+
13
import fs from 'node:fs'
24

3-
import { createExtensionFileManagers } from './manager/extension'
4-
import * as paths from './path'
5+
import { cacheFilePath } from './cache'
56

67
function uninstall(srcPath: string, bakPath: string) {
78
if (fs.existsSync(srcPath)) {
89
fs.renameSync(bakPath, srcPath)
910
}
1011
}
1112

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)),
3618
)
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 { }

0 commit comments

Comments
 (0)