Skip to content

Commit

Permalink
fix: change lock file path
Browse files Browse the repository at this point in the history
  • Loading branch information
subframe7536 committed Oct 14, 2024
1 parent 1c45e0e commit 2df4e0e
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
import type { AnyFunction, Promisable } from '@subframe7536/type-utils'
import type { AnyFunction } from '@subframe7536/type-utils'
import { existsSync, rmSync } from 'node:fs'
import path from 'node:path'
import { readFileSync, writeFileSync } from 'atomically'
import { useLogger } from 'reactive-vscode'
import { commands, window } from 'vscode'
import { displayName, name } from './generated/meta'
import { baseDir } from './path'

export const logger = useLogger(displayName)

const lockFileName = `__${name}__.lock`
const lockFile = path.join(baseDir, `__${name}__.lock`)

export async function runAndRestart(message: string, action: () => Promise<any>) {
let count = 5
const check = () => existsSync(lockFileName)
const check = () => existsSync(lockFile)
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)
if (check() && Number(readFileSync(lockFile, 'utf-8')) - Date.now() > 6e5) {
rmSync(lockFile)
} else {
await showMessage('File locked, cancel operation')
return
}
}
writeFileSync(lockFileName, String(Date.now()))
try {
writeFileSync(lockFile, String(Date.now()))
await action()
const item = await window.showInformationMessage(message, { title: 'Restart vscode' })
if (item) {
commands.executeCommand('workbench.action.reloadWindow')
}
} catch (e) {
logger.error(e)
showMessage(`Fail to execute action, ${e}`)
} finally {
rmSync(lockFileName)
rmSync(lockFile)
}
}

Expand Down

0 comments on commit 2df4e0e

Please sign in to comment.