Skip to content

Commit

Permalink
feat: auto restart #11
Browse files Browse the repository at this point in the history
  • Loading branch information
subframe7536 committed Dec 16, 2024
1 parent 18b3f62 commit b4229a2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function createFileManagers() {
logWindowOptionsChanged()
await runAndRestart(
text,
async () => Promise.all(managers.map(m => m.reload()))
() => Promise.all(managers.map(m => m.reload()))
// ensure other files are already modified
.then(() => productJsonManager.reload())
,
Expand Down
97 changes: 97 additions & 0 deletions src/restart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Reference from https://github.com/zokugun/vscode-sync-settings/blob/master/src/utils/restart-app.ts
import { spawn } from 'node:child_process'
import { readdirSync } from 'node:fs'
import path from 'node:path'
import { productJSONPath } from './path'

export async function restartApp(): Promise<void> {
let sp
switch (process.platform) {
case 'darwin':
sp = await restartMacOS()
break
case 'win32':
sp = await restartWindows()
break
default:
sp = await restartLinux()
}
sp.unref()
}

function getAppBinary(appHomeDir: string): string {
// remove tunnel
let files = readdirSync(appHomeDir).filter(file => !file.includes('-tunnel'))

if (files.length === 1) {
return path.join(appHomeDir, files[0])
}

if (process.platform === 'win32') {
// select *.cmd
files = files.filter(file => file.endsWith('.cmd'))

if (files.length === 1) {
return path.join(appHomeDir, files[0])
}
}

throw new Error('Can determine binary path')
}

async function restartMacOS() {
const { nameLong } = JSON.parse(productJSONPath) as { nameLong: string }

const match = /(.*\.app)\/Contents\/Frameworks\//.exec(process.execPath)
const appPath = match ? match[1] : `/Applications/${nameLong}.app`
const binary = getAppBinary(`${appPath}/Contents/Resources/app/bin/`)

return spawn(
'osascript',
[
'-e',
`quit app "${nameLong}"`,
'-e',
'delay 1',
'-e',
`do shell script quoted form of "${binary}"`,
],
{
detached: true,
stdio: 'ignore',
},
)
}

async function restartWindows() {
const appHomeDir = path.dirname(process.execPath)
const exeName = path.basename(process.execPath)
const binary = getAppBinary(`${appHomeDir}\\bin\\`)

return spawn(
process.env.comspec ?? 'cmd',
[`/C taskkill /F /IM ${exeName} >nul && timeout /T 1 && "${binary}"`],
{
detached: true,
stdio: 'ignore',
windowsVerbatimArguments: true,
},
)
}

async function restartLinux() {
const appHomeDir = path.dirname(process.execPath)
const binary = getAppBinary(`${appHomeDir}/bin/`)

return spawn(
'/bin/sh',
[
'-c',
`killall "${process.execPath}" && sleep 1 && killall -9 "${process.execPath}" && sleep 1 && "${binary}"`,
],
{
detached: true,
stdio: 'ignore',
},
)
}
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useLogger } from 'reactive-vscode'
import { commands, window } from 'vscode'
import { displayName, name } from './generated/meta'
import { baseDir } from './path'
import { restartApp } from './restart'

export const log = useLogger(displayName)

Expand Down Expand Up @@ -42,7 +43,8 @@ export async function runAndRestart(message: string, action: () => Promise<any>)
if (success) {
const item = await showMessage(message, 'Reload Window', 'Cancel')
if (item === 'Reload Window') {
commands.executeCommand('workbench.action.reloadWindow')
// commands.executeCommand('workbench.action.reloadWindow')
await restartApp()
}
}
} catch (e) {
Expand Down

0 comments on commit b4229a2

Please sign in to comment.