Skip to content

Commit 089c088

Browse files
committed
feat: new installation prompt, always overwrite
1 parent 673b82d commit 089c088

File tree

7 files changed

+43
-47
lines changed

7 files changed

+43
-47
lines changed

src/index.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,33 @@ import { createFileManagers } from './manager'
55
import { debounce, showMessage } from './utils'
66

77
const { activate, deactivate } = defineExtension(() => {
8-
const { reload, rollback } = createFileManagers()
8+
const { hasBakFile, reload, rollback } = createFileManagers()
99

10-
useCommand(Meta.commands.reload, () => {
11-
reload('UI style changed')
12-
})
13-
useCommand(Meta.commands.rollback, () => {
14-
rollback('UI style rollback')
15-
})
10+
if (!hasBakFile()) {
11+
showMessage(
12+
'Seems like first time use or new version is installed, reload the configuration now?',
13+
'Reload',
14+
'Cancel',
15+
)
16+
.then<any>(item => item === 'Reload' && reload('UI style changed'))
17+
}
18+
19+
useCommand(Meta.commands.reload, () => reload('UI style changed'))
20+
useCommand(Meta.commands.rollback, () => rollback('UI style rollback'))
1621

1722
const watchAndReload = debounce(
18-
(fontChanged: boolean) => showMessage('Configuration changed, apply?', 'Apply', 'Cancel')
19-
.then<any>(item => item === 'Apply' && reload('UI style changed', fontChanged)),
23+
() => showMessage('Configuration changed, apply?', 'Apply', 'Cancel')
24+
.then<any>(item => item === 'Apply' && reload('UI style changed')),
2025
1500,
2126
)
2227
const startWatch = () => {
2328
const cleanup1 = watch(
2429
() => editorConfig.fontFamily,
25-
() => !config['font.monospace'] && watchAndReload(true),
30+
() => !config['font.monospace'] && watchAndReload(),
2631
)
2732
const cleanup2 = watch(
2833
config,
29-
(conf, oldConf) => watchAndReload(
30-
conf['font.monospace'] !== oldConf['font.monospace'] || conf['font.sansSerif'] !== oldConf['font.sansSerif'],
31-
),
34+
() => watchAndReload(),
3235
)
3336
return () => {
3437
cleanup1()

src/manager/base.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,30 @@ import { logger } from '../utils'
55

66
export interface FileManager {
77
hasBakFile: boolean
8-
reload: (fontChanged: boolean) => Promise<void>
8+
reload: () => Promise<void>
99
rollback: () => Promise<void>
1010
}
1111

1212
export abstract class BaseFileManager implements FileManager {
1313
constructor(
1414
private srcPath: string,
1515
private bakPath: string,
16-
) {
17-
if (!this.hasBakFile) {
18-
cpSync(this.srcPath, this.bakPath)
19-
logger.info(`Create backup file [${this.bakPath}]`)
20-
}
21-
}
16+
) { }
2217

2318
get hasBakFile() {
2419
return existsSync(this.bakPath)
2520
}
2621

27-
async reload(fontChanged: boolean) {
22+
async reload() {
2823
if (!this.hasBakFile) {
29-
logger.warn(`Backup file [${this.bakPath}] does not exist, skip reload`)
30-
} else {
31-
const newContent = await this.patch(fontChanged, () => readFileSync(this.bakPath, 'utf-8'))
32-
if (newContent) {
33-
writeFileSync(this.srcPath, newContent)
34-
logger.info(`Config reload [${this.srcPath}]`)
35-
}
24+
logger.warn(`Backup file [${this.bakPath}] does not exist, backuping...`)
25+
cpSync(this.srcPath, this.bakPath)
26+
logger.info(`Create backup file [${this.bakPath}]`)
27+
}
28+
const newContent = await this.patch(readFileSync(this.bakPath, 'utf-8'))
29+
if (newContent) {
30+
writeFileSync(this.srcPath, newContent)
31+
logger.info(`Config reload [${this.srcPath}]`)
3632
}
3733
}
3834

@@ -45,5 +41,5 @@ export abstract class BaseFileManager implements FileManager {
4541
}
4642
}
4743

48-
abstract patch(fontChanged: boolean, content: () => string): Promisable<string | undefined>
44+
abstract patch(content: string): Promisable<string>
4945
}

src/manager/css.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ export class CssFileManager extends BaseFileManager {
5858
super(cssPath, cssBakPath)
5959
}
6060

61-
patch(_fontChanged: boolean, content: () => string): Promisable<string | undefined> {
62-
return `${content()}
61+
patch(content: string): Promisable<string> {
62+
return `${content}
6363
${banner}
6464
${generateBackgroundCSS()}
6565
${generateFontCSS()}

src/manager/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ export function createFileManagers() {
1313
new WebViewFileManager(),
1414
]
1515
return {
16-
reload: (text: string, fontChanged = true) => runAndRestart(
16+
hasBakFile: () => managers.every(m => m.hasBakFile),
17+
reload: (text: string) => runAndRestart(
1718
text,
18-
() => Promise.all(managers.map(m => m.reload(fontChanged))),
19+
() => Promise.all(managers.map(m => m.reload())),
1920
),
2021
rollback: (text: string) => runAndRestart(
2122
text,

src/manager/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export class MainFileManager extends BaseFileManager {
1010
super(mainPath, mainBakPath)
1111
}
1212

13-
patch(_fontChanged: boolean, content: () => string): Promisable<string | undefined> {
14-
const backgroundColor = config.electron.backgroundColor || 'r.getBackgroundColor()'
15-
return content().replace(
13+
patch(content: string): Promisable<string> {
14+
config.electron.backgroundColor ??= 'r.getBackgroundColor()'
15+
return content.replace(
1616
entry,
17-
`${JSON.stringify(config.electron).slice(1, -1)},backgroundColor:${backgroundColor}`,
17+
JSON.stringify(config.electron).slice(1, -1),
1818
)
1919
}
2020
}

src/manager/renderer.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,23 @@ export class RendererFileManager extends BaseFileManager {
2222
super(rendererPath, rendererBakPath)
2323
}
2424

25-
patch(fontChanged: boolean, content: () => string): Promisable<string | undefined> {
26-
if (!fontChanged) {
27-
return undefined
28-
}
29-
let _content = content()
25+
patch(content: string): Promisable<string> {
3026
let { monospace, sansSerif } = getFamilies()
3127
if (monospace) {
3228
monospace = escapeQuote(monospace)
33-
_content = _content
29+
content = content
3430
.replaceAll(VSC_DFAULT_MONO_FONT.win, monospace)
3531
.replaceAll(VSC_DFAULT_MONO_FONT.mac, monospace)
3632
.replaceAll(VSC_DFAULT_MONO_FONT.linux, monospace)
3733
.replaceAll(VSC_NOTEBOOK_MONO_FONT, monospace)
3834
}
3935
if (sansSerif) {
4036
sansSerif = escapeQuote(sansSerif)
41-
_content = _content
37+
content = content
4238
.replaceAll(VSC_DFAULT_SANS_FONT.win, sansSerif)
4339
.replaceAll(VSC_DFAULT_SANS_FONT.mac, sansSerif)
4440
.replaceAll(VSC_DFAULT_SANS_FONT.linux, sansSerif)
4541
}
46-
return _content
42+
return content
4743
}
4844
}

src/manager/webview.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ export class WebViewFileManager extends BaseFileManager {
4242
super(webviewHTMLPath, webviewHTMLBakPath)
4343
}
4444

45-
patch(_fontChanged: boolean, content: () => string): Promisable<string | undefined> {
45+
patch(content: string): Promisable<string> {
4646
return fixSha256(
47-
content().replace(
47+
content.replace(
4848
entry,
4949
`${entry}.replace('</body>', '</body><style>${getCSS()}</style>')`,
5050
),

0 commit comments

Comments
 (0)