Skip to content

Commit

Permalink
feat: with lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
subframe7536 committed Oct 13, 2024
1 parent ec60591 commit f1fbe11
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/manager/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export abstract class BaseFileManager implements FileManager {
) {
if (!this.hasBakFile) {
cpSync(this.srcPath, this.bakPath)
logger.info('Create', this.bakPath)
logger.info(`Create backup file [${this.bakPath}]`)
}
}

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

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

async rollback() {
if (!this.hasBakFile) {
logger.warn(`bak file [${this.bakPath}] does not exist, skip rollback`)
logger.warn(`Backup file [${this.bakPath}] does not exist, skip rollback`)
} else {
const originJS = readFileSync(this.bakPath, 'utf-8')
writeFileSync(this.srcPath, originJS)
Expand Down
7 changes: 1 addition & 6 deletions src/manager/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ const banner = '/* Custom UI Style Start */'
const footer = '/* Custom UI Style End */'

function generateBackgroundCSS() {
const plt = process?.platform

const url = (
config[`backgroundUrl${captialize(plt)}` as keyof ConfigShorthandMap] || config.backgroundUrl
) as string

const url = config.backgroundUrl
if (!url) {
return ''
}
Expand Down
41 changes: 35 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
import type { Promisable } from '@subframe7536/type-utils'
import { existsSync, rmSync } from 'node:fs'
import { readFileSync, writeFileSync } from 'atomically'
import { useLogger } from 'reactive-vscode'
import { commands, window } from 'vscode'
import { displayName } from './generated/meta'
import { displayName, name } from './generated/meta'

export const logger = useLogger(displayName)

export async function runAndRestart(message: string, action: () => Promise<any>) {
await action()
const item = await window.showInformationMessage(message, { title: 'Restart vscode' })
if (item) {
commands.executeCommand('workbench.action.reloadWindow')
const lockFileName = `__${name}__.lock`

async function runWithLock(fn: () => Promisable<void>) {
let count = 5
const check = () => existsSync(lockFileName)
while (check() && count--) {
await new Promise(resolve => setTimeout(resolve, 1000))
}
if (!count) {
// If exists and expire time exceed 10 minutes, just remove it
if (check() && Number(readFileSync(lockFileName, 'utf-8')) - Date.now() > 6e5) {
rmSync(lockFileName)
} else {
await showMessage('File locked, cancel operation')
return
}
}
writeFileSync(lockFileName, String(Date.now()))
try {
await fn()
} finally {
rmSync(lockFileName)
}
}
export async function runAndRestart(message: string, action: () => Promise<any>) {
await runWithLock(async () => {
await action()
const item = await window.showInformationMessage(message, { title: 'Restart vscode' })
if (item) {
commands.executeCommand('workbench.action.reloadWindow')
}
})
}

export async function showMessage(content: string) {
Expand Down

0 comments on commit f1fbe11

Please sign in to comment.