From c96f5d1291350cc60338d76ce79106291625bfd6 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 5 Mar 2018 07:51:06 -0800 Subject: [PATCH] Import and type windows-process-tree Fixes #43287 --- src/typings/windows-mutex.d.ts | 2 +- src/typings/windows-process-tree.d.ts | 4 +- .../electron-browser/windowsShellHelper.ts | 57 ++++++++++--------- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/typings/windows-mutex.d.ts b/src/typings/windows-mutex.d.ts index a3acc8f4430..07c5908def2 100644 --- a/src/typings/windows-mutex.d.ts +++ b/src/typings/windows-mutex.d.ts @@ -11,4 +11,4 @@ declare module 'windows-mutex' { } export function isActive(name: string): boolean; -} \ No newline at end of file +} diff --git a/src/typings/windows-process-tree.d.ts b/src/typings/windows-process-tree.d.ts index b50dbd2b31e..aec2670a5cd 100644 --- a/src/typings/windows-process-tree.d.ts +++ b/src/typings/windows-process-tree.d.ts @@ -9,6 +9,8 @@ declare module 'windows-process-tree' { name: string, children: ProcessTreeNode[] } + function get(rootPid: number, callback: (tree: ProcessTreeNode) => void): void; + export = get; -} \ No newline at end of file +} diff --git a/src/vs/workbench/parts/terminal/electron-browser/windowsShellHelper.ts b/src/vs/workbench/parts/terminal/electron-browser/windowsShellHelper.ts index 56930e6cd3d..803ad86dd9c 100644 --- a/src/vs/workbench/parts/terminal/electron-browser/windowsShellHelper.ts +++ b/src/vs/workbench/parts/terminal/electron-browser/windowsShellHelper.ts @@ -8,10 +8,11 @@ import { TPromise } from 'vs/base/common/winjs.base'; import { Emitter, debounceEvent } from 'vs/base/common/event'; import { ITerminalInstance } from 'vs/workbench/parts/terminal/common/terminal'; import { Terminal as XTermTerminal } from 'vscode-xterm'; +import WindowsProcessTreeType = require('windows-process-tree'); const SHELL_EXECUTABLES = ['cmd.exe', 'powershell.exe', 'bash.exe']; -let windowsProcessTree; +let windowsProcessTree: typeof WindowsProcessTreeType; export class WindowsShellHelper { private _onCheckShell: Emitter>; @@ -28,34 +29,38 @@ export class WindowsShellHelper { throw new Error(`WindowsShellHelper cannot be instantiated on ${platform.platform}`); } - if (!windowsProcessTree) { - windowsProcessTree = require.__$__nodeRequire('windows-process-tree'); - } - this._isDisposed = false; - this._onCheckShell = new Emitter>(); - // The debounce is necessary to prevent multiple processes from spawning when - // the enter key or output is spammed - debounceEvent(this._onCheckShell.event, (l, e) => e, 150, true)(() => { - setTimeout(() => { - this.checkShell(); - }, 50); - }); - // We want to fire a new check for the shell on a linefeed, but only - // when parsing has finished which is indicated by the cursormove event. - // If this is done on every linefeed, parsing ends up taking - // significantly longer due to resetting timers. Note that this is - // private API. - this._xterm.on('linefeed', () => this._newLineFeed = true); - this._xterm.on('cursormove', () => { - if (this._newLineFeed) { - this._onCheckShell.fire(); + (import('windows-process-tree')).then(mod => { + if (this._isDisposed) { + return; } - }); - // Fire a new check for the shell when any key is pressed. - this._xterm.on('keypress', () => this._onCheckShell.fire()); + windowsProcessTree = mod; + this._onCheckShell = new Emitter>(); + // The debounce is necessary to prevent multiple processes from spawning when + // the enter key or output is spammed + debounceEvent(this._onCheckShell.event, (l, e) => e, 150, true)(() => { + setTimeout(() => { + this.checkShell(); + }, 50); + }); + + // We want to fire a new check for the shell on a linefeed, but only + // when parsing has finished which is indicated by the cursormove event. + // If this is done on every linefeed, parsing ends up taking + // significantly longer due to resetting timers. Note that this is + // private API. + this._xterm.on('linefeed', () => this._newLineFeed = true); + this._xterm.on('cursormove', () => { + if (this._newLineFeed) { + this._onCheckShell.fire(); + } + }); + + // Fire a new check for the shell when any key is pressed. + this._xterm.on('keypress', () => this._onCheckShell.fire()); + }); } private checkShell(): void { @@ -118,4 +123,4 @@ export class WindowsShellHelper { }); return this._currentRequest; } -} \ No newline at end of file +} -- GitLab