Added support for starting terminal with Windows PowerShell on Windows

Bryan Roe committed Jul 24, 2019 at 15:01 UTC 58aae1417bef8185436217f214cc56c84574a768
1 file changed +28 -6
agents/modules_meshcore/win-terminal.js
+28 -6
@@ -132,7 +132,12 @@ function windows_terminal() {
132 this._kernel32.SetConsoleWindowInfo(this._stdoutput, 1, rect);
133 }
134
135 - this.Start = function Start(CONSOLE_SCREEN_WIDTH, CONSOLE_SCREEN_HEIGHT)
135 + this.PowerShellCapable = function()
136 + {
137 + return (require('fs').existsSync(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'));
138 + }
139 +
140 + this.StartEx = function Start(CONSOLE_SCREEN_WIDTH, CONSOLE_SCREEN_HEIGHT, terminalTarget)
141 {
142 this.stopping = null;
143 if (this._kernel32.GetConsoleWindow().Val == 0) {
@@ -163,9 +168,10 @@ function windows_terminal() {
168 this._user32.ShowWindow(this._kernel32.GetConsoleWindow().Val, SW_HIDE);
169
170 this.ClearScreen();
166 - this._hookThread().then(function () {
171 + this._hookThread(terminalTarget).then(function ()
172 + {
173 // Hook Ready
168 - this.terminal.StartCommand();
174 + this.terminal.StartCommand(this.userArgs[0]);
175 }, console.log);
176 this._stream = new duplex({
177 'write': function (chunk, flush) {
@@ -208,6 +214,15 @@ function windows_terminal() {
214 this._stream._promise.terminal = this;
215 return (this._stream);
216 };
217 + this.Start = function Start(CONSOLE_SCREEN_WIDTH, CONSOLE_SCREEN_HEIGHT)
218 + {
219 + return (this.StartEx(CONSOLE_SCREEN_WIDTH, CONSOLE_SCREEN_HEIGHT, process.env['windir'] + '\\System32\\cmd.exe'));
220 + }
221 + this.StartPowerShell = function StartPowerShell(CONSOLE_SCREEN_WIDTH, CONSOLE_SCREEN_HEIGHT)
222 + {
223 + return (this.StartEx(CONSOLE_SCREEN_WIDTH, CONSOLE_SCREEN_HEIGHT, process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'));
224 + }
225 +
226 this._stop = function () {
227 if (this.stopping) { return (this.stopping); }
228 //console.log('Stopping Terminal...');
@@ -220,8 +235,14 @@ function windows_terminal() {
235 return (this.stopping);
236 }
237
223 - this._hookThread = function () {
238 + this._hookThread = function ()
239 + {
240 var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
241 + ret.userArgs = [];
242 + for (var a in arguments)
243 + {
244 + ret.userArgs.push(arguments[a]);
245 + }
246 ret.terminal = this;
247 this._ConsoleWinEventProc = GM.GetGenericGlobalCallback(7);
248 this._ConsoleWinEventProc.terminal = this;
@@ -458,8 +479,9 @@ function windows_terminal() {
479 }, 250, this, nWidth, nHeight);
480 }
481
461 - this.StartCommand = function StartCommand() {
462 - if (this._kernel32.CreateProcessA(GM.CreateVariable(process.env['windir'] + '\\system32\\cmd.exe'), 0, 0, 0, 1, CREATE_NEW_PROCESS_GROUP, 0, 0, si, pi).Val == 0) {
482 + this.StartCommand = function StartCommand(target) {
483 + if (this._kernel32.CreateProcessA(GM.CreateVariable(target), 0, 0, 0, 1, CREATE_NEW_PROCESS_GROUP, 0, 0, si, pi).Val == 0)
484 + {
485 console.log('Error Spawning CMD');
486 return;
487 }