Skip to content

Commit

Permalink
fix restart command (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
friedbrice authored Mar 13, 2024
1 parent 1cad264 commit 1f317b7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ export namespace alloglot {
export const couldNotSanitizeConfig = (err: any) => `Configuration is malformed: ${err}`
export const creatingApiSearch = (langIds: Array<string>) => `Creating API search command for languages: ${langIds}`
export const creatingTagsSource = (path: string) => `Creating tags source for path: ${path}`
export const deactivatingAlloglot = `Deactivating Alloglot...`
export const deactivatedAlloglot = 'Deactivated Alloglot.'
export const deactivatingAlloglot = 'Deactivating Alloglot...'
export const deactivateCommandDone = (cmd: string) => `Deactivation command has completed: ${cmd}`
export const deactivateCommandFailed = (err: any) => `Deactivation command has completed: ${err}`
export const disposingAlloglot = 'Disposing Alloglot...'
Expand All @@ -380,6 +381,7 @@ export namespace alloglot {
export const ranCommand = (cmd: string) => `Ran “${cmd}”.`
export const readingFallbackConfig = (path: string) => `Reading fallback configuration from path: ${path}`
export const readingWorkspaceSettings = 'Reading configuration from workspace settings'
export const readyToRestart = 'Ready to restart Alloglot.'
export const registeredCompletionsProvider = 'Registered completions provider.'
export const registeredDefinitionsProvider = 'Registered definitions provider.'
export const registeredImportsProvider = 'Registered imports provider.'
Expand Down
47 changes: 27 additions & 20 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,46 @@ export function activate(context: vscode.ExtensionContext): void {
)
}

export function deactivate(): void {
export function deactivate(): Promise<void> {
const command = globalConfig?.deactivateCommand
globalConfig = undefined
const basedir = vscode.workspace.workspaceFolders?.[0].uri

function cleanup(): void {
globalOutput?.appendLine(alloglot.ui.deactivatingAlloglot)
globalContext && globalContext.subscriptions.forEach(sub => sub.dispose())
globalContext = undefined
function cleanup(): Promise<void> {
return new Promise((resolve, reject) => {
try {
globalOutput?.appendLine(alloglot.ui.deactivatingAlloglot)
globalContext && globalContext.subscriptions.forEach(sub => sub.dispose())
globalContext = undefined
globalOutput?.appendLine(alloglot.ui.deactivatedAlloglot)
resolve()
} catch (err) {
reject(err)
}
})
}

if (command) {
const proc = AsyncProcess.make({ output: globalOutput, command, basedir }, () => { })

proc.then(() => {
globalOutput?.appendLine(alloglot.ui.deactivateCommandDone(command))
cleanup()
})

proc.catch(err => {
globalOutput?.appendLine(alloglot.ui.deactivateCommandFailed(err))
cleanup()
})

return AsyncProcess.make({ output: globalOutput, command, basedir }, () => undefined)
.then(() => {
globalOutput?.appendLine(alloglot.ui.deactivateCommandDone(command))
return cleanup()
})
.catch(err => {
globalOutput?.appendLine(alloglot.ui.deactivateCommandFailed(err))
return cleanup()
})
} else {
cleanup()
return cleanup()
}
}

function restart(output: vscode.OutputChannel, context: vscode.ExtensionContext): void {
output.appendLine(alloglot.ui.restartingAlloglot)
deactivate()
activate(context)
deactivate().then(() => {
output.appendLine(alloglot.ui.readyToRestart)
activate(context)
})
}

function makeActivationCommand(parentOutput: IHierarchicalOutputChannel, command: string | undefined): vscode.Disposable {
Expand Down
35 changes: 20 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,26 @@ export namespace AsyncProcess {
const asyncProc: any = new Promise((resolve, reject) => {
output?.appendLine(alloglot.ui.runningCommand(command, cwd))

const proc = exec(command, { cwd, signal }, (error, stdout, stderr) => {
if (error) {
output?.appendLine(alloglot.ui.errorRunningCommand(command, error))
reject(error)
}

stderr && output?.appendLine(alloglot.ui.commandLogs(command, stderr))
!stdout && output?.appendLine(alloglot.ui.commandNoOutput(command))

resolve(f(stdout))
})

proc.stdout?.on('data', chunk => output?.append(stripAnsi(chunk)))
stdin && proc.stdin?.write(stdin)
proc.stdin?.end()
try {
const proc = exec(command, { cwd, signal }, (error, stdout, stderr) => {
if (error) {
output?.appendLine(alloglot.ui.errorRunningCommand(command, error))
reject(error)
}

stderr && output?.appendLine(alloglot.ui.commandLogs(command, stderr))
!stdout && output?.appendLine(alloglot.ui.commandNoOutput(command))

resolve(f(stdout))
})

proc.stdout?.on('data', chunk => output?.append(stripAnsi(chunk)))
stdin && proc.stdin?.write(stdin)
proc.stdin?.end()
} catch (err) {
output?.appendLine(alloglot.ui.errorRunningCommand(command, err))
reject(err)
}
})

asyncProc.dispose = () => {
Expand Down

0 comments on commit 1f317b7

Please sign in to comment.