From 089c088b1491b662b96b6cae30d3909eb448914c Mon Sep 17 00:00:00 2001 From: subframe7536 <1667077010@qq.com> Date: Wed, 30 Oct 2024 10:59:41 +0800 Subject: [PATCH] feat: new installation prompt, always overwrite --- src/index.ts | 29 ++++++++++++++++------------- src/manager/base.ts | 28 ++++++++++++---------------- src/manager/css.ts | 4 ++-- src/manager/index.ts | 5 +++-- src/manager/main.ts | 8 ++++---- src/manager/renderer.ts | 12 ++++-------- src/manager/webview.ts | 4 ++-- 7 files changed, 43 insertions(+), 47 deletions(-) diff --git a/src/index.ts b/src/index.ts index 20e47d6..d020a31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,30 +5,33 @@ import { createFileManagers } from './manager' import { debounce, showMessage } from './utils' const { activate, deactivate } = defineExtension(() => { - const { reload, rollback } = createFileManagers() + const { hasBakFile, reload, rollback } = createFileManagers() - useCommand(Meta.commands.reload, () => { - reload('UI style changed') - }) - useCommand(Meta.commands.rollback, () => { - rollback('UI style rollback') - }) + if (!hasBakFile()) { + showMessage( + 'Seems like first time use or new version is installed, reload the configuration now?', + 'Reload', + 'Cancel', + ) + .then(item => item === 'Reload' && reload('UI style changed')) + } + + useCommand(Meta.commands.reload, () => reload('UI style changed')) + useCommand(Meta.commands.rollback, () => rollback('UI style rollback')) const watchAndReload = debounce( - (fontChanged: boolean) => showMessage('Configuration changed, apply?', 'Apply', 'Cancel') - .then(item => item === 'Apply' && reload('UI style changed', fontChanged)), + () => showMessage('Configuration changed, apply?', 'Apply', 'Cancel') + .then(item => item === 'Apply' && reload('UI style changed')), 1500, ) const startWatch = () => { const cleanup1 = watch( () => editorConfig.fontFamily, - () => !config['font.monospace'] && watchAndReload(true), + () => !config['font.monospace'] && watchAndReload(), ) const cleanup2 = watch( config, - (conf, oldConf) => watchAndReload( - conf['font.monospace'] !== oldConf['font.monospace'] || conf['font.sansSerif'] !== oldConf['font.sansSerif'], - ), + () => watchAndReload(), ) return () => { cleanup1() diff --git a/src/manager/base.ts b/src/manager/base.ts index fc56377..2a751da 100644 --- a/src/manager/base.ts +++ b/src/manager/base.ts @@ -5,7 +5,7 @@ import { logger } from '../utils' export interface FileManager { hasBakFile: boolean - reload: (fontChanged: boolean) => Promise + reload: () => Promise rollback: () => Promise } @@ -13,26 +13,22 @@ export abstract class BaseFileManager implements FileManager { constructor( private srcPath: string, private bakPath: string, - ) { - if (!this.hasBakFile) { - cpSync(this.srcPath, this.bakPath) - logger.info(`Create backup file [${this.bakPath}]`) - } - } + ) { } get hasBakFile() { return existsSync(this.bakPath) } - async reload(fontChanged: boolean) { + async reload() { if (!this.hasBakFile) { - logger.warn(`Backup file [${this.bakPath}] does not exist, skip reload`) - } else { - const newContent = await this.patch(fontChanged, () => readFileSync(this.bakPath, 'utf-8')) - if (newContent) { - writeFileSync(this.srcPath, newContent) - logger.info(`Config reload [${this.srcPath}]`) - } + logger.warn(`Backup file [${this.bakPath}] does not exist, backuping...`) + cpSync(this.srcPath, this.bakPath) + logger.info(`Create backup file [${this.bakPath}]`) + } + const newContent = await this.patch(readFileSync(this.bakPath, 'utf-8')) + if (newContent) { + writeFileSync(this.srcPath, newContent) + logger.info(`Config reload [${this.srcPath}]`) } } @@ -45,5 +41,5 @@ export abstract class BaseFileManager implements FileManager { } } - abstract patch(fontChanged: boolean, content: () => string): Promisable + abstract patch(content: string): Promisable } diff --git a/src/manager/css.ts b/src/manager/css.ts index 1a67f5e..e7de409 100644 --- a/src/manager/css.ts +++ b/src/manager/css.ts @@ -58,8 +58,8 @@ export class CssFileManager extends BaseFileManager { super(cssPath, cssBakPath) } - patch(_fontChanged: boolean, content: () => string): Promisable { - return `${content()} + patch(content: string): Promisable { + return `${content} ${banner} ${generateBackgroundCSS()} ${generateFontCSS()} diff --git a/src/manager/index.ts b/src/manager/index.ts index 5bbc8df..7fbe46b 100644 --- a/src/manager/index.ts +++ b/src/manager/index.ts @@ -13,9 +13,10 @@ export function createFileManagers() { new WebViewFileManager(), ] return { - reload: (text: string, fontChanged = true) => runAndRestart( + hasBakFile: () => managers.every(m => m.hasBakFile), + reload: (text: string) => runAndRestart( text, - () => Promise.all(managers.map(m => m.reload(fontChanged))), + () => Promise.all(managers.map(m => m.reload())), ), rollback: (text: string) => runAndRestart( text, diff --git a/src/manager/main.ts b/src/manager/main.ts index ee6e7fd..91cb9aa 100644 --- a/src/manager/main.ts +++ b/src/manager/main.ts @@ -10,11 +10,11 @@ export class MainFileManager extends BaseFileManager { super(mainPath, mainBakPath) } - patch(_fontChanged: boolean, content: () => string): Promisable { - const backgroundColor = config.electron.backgroundColor || 'r.getBackgroundColor()' - return content().replace( + patch(content: string): Promisable { + config.electron.backgroundColor ??= 'r.getBackgroundColor()' + return content.replace( entry, - `${JSON.stringify(config.electron).slice(1, -1)},backgroundColor:${backgroundColor}`, + JSON.stringify(config.electron).slice(1, -1), ) } } diff --git a/src/manager/renderer.ts b/src/manager/renderer.ts index d7c1270..b83d091 100644 --- a/src/manager/renderer.ts +++ b/src/manager/renderer.ts @@ -22,15 +22,11 @@ export class RendererFileManager extends BaseFileManager { super(rendererPath, rendererBakPath) } - patch(fontChanged: boolean, content: () => string): Promisable { - if (!fontChanged) { - return undefined - } - let _content = content() + patch(content: string): Promisable { let { monospace, sansSerif } = getFamilies() if (monospace) { monospace = escapeQuote(monospace) - _content = _content + content = content .replaceAll(VSC_DFAULT_MONO_FONT.win, monospace) .replaceAll(VSC_DFAULT_MONO_FONT.mac, monospace) .replaceAll(VSC_DFAULT_MONO_FONT.linux, monospace) @@ -38,11 +34,11 @@ export class RendererFileManager extends BaseFileManager { } if (sansSerif) { sansSerif = escapeQuote(sansSerif) - _content = _content + content = content .replaceAll(VSC_DFAULT_SANS_FONT.win, sansSerif) .replaceAll(VSC_DFAULT_SANS_FONT.mac, sansSerif) .replaceAll(VSC_DFAULT_SANS_FONT.linux, sansSerif) } - return _content + return content } } diff --git a/src/manager/webview.ts b/src/manager/webview.ts index ec57400..1056783 100644 --- a/src/manager/webview.ts +++ b/src/manager/webview.ts @@ -42,9 +42,9 @@ export class WebViewFileManager extends BaseFileManager { super(webviewHTMLPath, webviewHTMLBakPath) } - patch(_fontChanged: boolean, content: () => string): Promisable { + patch(content: string): Promisable { return fixSha256( - content().replace( + content.replace( entry, `${entry}.replace('', '')`, ),