Skip to content

Commit

Permalink
fix: MacOS restart and error catch
Browse files Browse the repository at this point in the history
  • Loading branch information
subframe7536 committed Dec 16, 2024
1 parent 0458bab commit e106187
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from 'node:fs'
import fs from 'node:fs'
import path from 'node:path'
import vscode from 'vscode'
import { name as bakExt } from './generated/meta'
Expand Down Expand Up @@ -68,7 +68,7 @@ function getMainPath(baseExt: string, backupExt?: string) {
'electron-main',
`main.${ext}`,
)
return existsSync(defaultPath) ? defaultPath : path.join(baseDir, `main.${ext}`)
return fs.existsSync(defaultPath) ? defaultPath : path.join(baseDir, `main.${ext}`)
}

export const mainPath = getMainPath('js')
Expand Down
7 changes: 4 additions & 3 deletions src/restart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// 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 fs from 'node:fs'
import path from 'node:path'
import { readFileSync } from 'atomically'
import { productJSONPath } from './path'

export async function restartApp(): Promise<void> {
Expand All @@ -21,7 +22,7 @@ export async function restartApp(): Promise<void> {

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

if (files.length === 1) {
return path.join(appHomeDir, files[0])
Expand All @@ -40,7 +41,7 @@ function getAppBinary(appHomeDir: string): string {
}

async function restartMacOS() {
const { nameLong } = JSON.parse(productJSONPath) as { nameLong: string }
const { nameLong } = JSON.parse(readFileSync(productJSONPath, 'utf-8')) as { nameLong: string }

const match = /(.*\.app)\/Contents\/Frameworks\//.exec(process.execPath)
const appPath = match ? match[1] : `/Applications/${nameLong}.app`
Expand Down
2 changes: 1 addition & 1 deletion src/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, writeFileSync } from 'node:fs'
import { readFileSync, writeFileSync } from 'atomically'
import { cssBakPath, cssPath, mainBakPath, mainPath, rendererBakPath, rendererPath, webviewHTMLBakPath, webviewHTMLPath } from './path'

function uninstall(srcPath: string, bakPath: string) {
Expand Down
7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ export async function runAndRestart(message: string, action: () => Promise<any>)
const item = await showMessage(message, 'Reload Window', 'Cancel')
if (item === 'Reload Window') {
if (checkIsVSCodeUsingESM()) {
await restartApp()
try {
await restartApp()
} catch (error) {
log.error('Fail to restart VSCode', (error as Error).message, (error as Error).stack)
showMessage(`Fail to restart VSCode, ${error}`)
}
} else {
commands.executeCommand('workbench.action.reloadWindow')
}
Expand Down

0 comments on commit e106187

Please sign in to comment.