diff --git a/src/config.ts b/src/config.ts index b61d09a..431b9b2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -355,7 +355,8 @@ export namespace alloglot { export const couldNotSanitizeConfig = (err: any) => `Configuration is malformed: ${err}` export const creatingApiSearch = (langIds: Array) => `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...' @@ -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.' diff --git a/src/extension.ts b/src/extension.ts index ac9bdf5..dde5c2d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -53,39 +53,46 @@ export function activate(context: vscode.ExtensionContext): void { ) } -export function deactivate(): void { +export function deactivate(): Promise { 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 { + 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 { diff --git a/src/utils.ts b/src/utils.ts index 7a7cbfe..02548fc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 = () => {