Skip to content

Commit

Permalink
feat: new installation prompt, always overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
subframe7536 committed Oct 30, 2024
1 parent 673b82d commit 089c088
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 47 deletions.
29 changes: 16 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>(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<any>(item => item === 'Apply' && reload('UI style changed', fontChanged)),
() => showMessage('Configuration changed, apply?', 'Apply', 'Cancel')
.then<any>(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()
Expand Down
28 changes: 12 additions & 16 deletions src/manager/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,30 @@ import { logger } from '../utils'

export interface FileManager {
hasBakFile: boolean
reload: (fontChanged: boolean) => Promise<void>
reload: () => Promise<void>
rollback: () => Promise<void>
}

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}]`)
}
}

Expand All @@ -45,5 +41,5 @@ export abstract class BaseFileManager implements FileManager {
}
}

abstract patch(fontChanged: boolean, content: () => string): Promisable<string | undefined>
abstract patch(content: string): Promisable<string>
}
4 changes: 2 additions & 2 deletions src/manager/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export class CssFileManager extends BaseFileManager {
super(cssPath, cssBakPath)
}

patch(_fontChanged: boolean, content: () => string): Promisable<string | undefined> {
return `${content()}
patch(content: string): Promisable<string> {
return `${content}
${banner}
${generateBackgroundCSS()}
${generateFontCSS()}
Expand Down
5 changes: 3 additions & 2 deletions src/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/manager/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export class MainFileManager extends BaseFileManager {
super(mainPath, mainBakPath)
}

patch(_fontChanged: boolean, content: () => string): Promisable<string | undefined> {
const backgroundColor = config.electron.backgroundColor || 'r.getBackgroundColor()'
return content().replace(
patch(content: string): Promisable<string> {
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),
)
}
}
12 changes: 4 additions & 8 deletions src/manager/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,23 @@ export class RendererFileManager extends BaseFileManager {
super(rendererPath, rendererBakPath)
}

patch(fontChanged: boolean, content: () => string): Promisable<string | undefined> {
if (!fontChanged) {
return undefined
}
let _content = content()
patch(content: string): Promisable<string> {
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)
.replaceAll(VSC_NOTEBOOK_MONO_FONT, monospace)
}
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
}
}
4 changes: 2 additions & 2 deletions src/manager/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export class WebViewFileManager extends BaseFileManager {
super(webviewHTMLPath, webviewHTMLBakPath)
}

patch(_fontChanged: boolean, content: () => string): Promisable<string | undefined> {
patch(content: string): Promisable<string> {
return fixSha256(
content().replace(
content.replace(
entry,
`${entry}.replace('</body>', '</body><style>${getCSS()}</style>')`,
),
Expand Down

0 comments on commit 089c088

Please sign in to comment.