Skip to content

Commit f1fbe11

Browse files
committed
feat: with lockfile
1 parent ec60591 commit f1fbe11

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

src/manager/base.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export abstract class BaseFileManager implements FileManager {
1616
) {
1717
if (!this.hasBakFile) {
1818
cpSync(this.srcPath, this.bakPath)
19-
logger.info('Create', this.bakPath)
19+
logger.info(`Create backup file [${this.bakPath}]`)
2020
}
2121
}
2222

@@ -26,7 +26,7 @@ export abstract class BaseFileManager implements FileManager {
2626

2727
async reload() {
2828
if (!this.hasBakFile) {
29-
logger.warn(`bak file [${this.bakPath}] does not exist, skip reload`)
29+
logger.warn(`Backup file [${this.bakPath}] does not exist, skip reload`)
3030
} else {
3131
writeFileSync(this.srcPath, await this.patch(readFileSync(this.bakPath, 'utf-8')))
3232
logger.info(`Config reload [${this.srcPath}]`)
@@ -35,7 +35,7 @@ export abstract class BaseFileManager implements FileManager {
3535

3636
async rollback() {
3737
if (!this.hasBakFile) {
38-
logger.warn(`bak file [${this.bakPath}] does not exist, skip rollback`)
38+
logger.warn(`Backup file [${this.bakPath}] does not exist, skip rollback`)
3939
} else {
4040
const originJS = readFileSync(this.bakPath, 'utf-8')
4141
writeFileSync(this.srcPath, originJS)

src/manager/css.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ const banner = '/* Custom UI Style Start */'
1010
const footer = '/* Custom UI Style End */'
1111

1212
function generateBackgroundCSS() {
13-
const plt = process?.platform
14-
15-
const url = (
16-
config[`backgroundUrl${captialize(plt)}` as keyof ConfigShorthandMap] || config.backgroundUrl
17-
) as string
18-
13+
const url = config.backgroundUrl
1914
if (!url) {
2015
return ''
2116
}

src/utils.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
1+
import type { Promisable } from '@subframe7536/type-utils'
2+
import { existsSync, rmSync } from 'node:fs'
3+
import { readFileSync, writeFileSync } from 'atomically'
14
import { useLogger } from 'reactive-vscode'
25
import { commands, window } from 'vscode'
3-
import { displayName } from './generated/meta'
6+
import { displayName, name } from './generated/meta'
47

58
export const logger = useLogger(displayName)
69

7-
export async function runAndRestart(message: string, action: () => Promise<any>) {
8-
await action()
9-
const item = await window.showInformationMessage(message, { title: 'Restart vscode' })
10-
if (item) {
11-
commands.executeCommand('workbench.action.reloadWindow')
10+
const lockFileName = `__${name}__.lock`
11+
12+
async function runWithLock(fn: () => Promisable<void>) {
13+
let count = 5
14+
const check = () => existsSync(lockFileName)
15+
while (check() && count--) {
16+
await new Promise(resolve => setTimeout(resolve, 1000))
17+
}
18+
if (!count) {
19+
// If exists and expire time exceed 10 minutes, just remove it
20+
if (check() && Number(readFileSync(lockFileName, 'utf-8')) - Date.now() > 6e5) {
21+
rmSync(lockFileName)
22+
} else {
23+
await showMessage('File locked, cancel operation')
24+
return
25+
}
1226
}
27+
writeFileSync(lockFileName, String(Date.now()))
28+
try {
29+
await fn()
30+
} finally {
31+
rmSync(lockFileName)
32+
}
33+
}
34+
export async function runAndRestart(message: string, action: () => Promise<any>) {
35+
await runWithLock(async () => {
36+
await action()
37+
const item = await window.showInformationMessage(message, { title: 'Restart vscode' })
38+
if (item) {
39+
commands.executeCommand('workbench.action.reloadWindow')
40+
}
41+
})
1342
}
1443

1544
export async function showMessage(content: string) {

0 commit comments

Comments
 (0)