From c03fab0aae7f01f0babcf3786317f12cfc3c8a68 Mon Sep 17 00:00:00 2001 From: John Slegers Date: Tue, 23 Apr 2019 19:36:26 +0200 Subject: [PATCH 1/9] Add support for paths with spaces Allow paths that have spaces to be run correctly --- index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index b4758c4..31eb20a 100644 --- a/index.js +++ b/index.js @@ -7,10 +7,18 @@ var path = require('path') var spawn = require('child_process').spawn -var escapeRegex = /\\/g -var escapement = '\\\\' -var startScriptPath = path.join(__dirname, 'scripts/start_edge.ps1').replace(escapeRegex, escapement) -var stopScriptPath = path.join(__dirname, 'scripts/stop_edge.ps1').replace(escapeRegex, escapement) +var backslashRegex = /\\/g +var escapeBackslash = '\\\\' +var spaceRegex = / /g +var escapeSpace = '` ' +var startScriptPath = path + .join(__dirname, 'scripts/start_edge.ps1') + .replace(backslashRegex, escapeBackslash) + .replace(spaceRegex, escapeSpace) +var stopScriptPath = path + .join(__dirname, 'scripts/stop_edge.ps1') + .replace(backslashRegex, escapeBackslash) + .replace(spaceRegex, escapeSpace) // Constructor function EdgeBrowser (baseBrowserDecorator) { From d0e9790c4fbae947e061cc68f288288837968dc4 Mon Sep 17 00:00:00 2001 From: John Slegers Date: Tue, 23 Apr 2019 19:37:31 +0200 Subject: [PATCH 2/9] Add "signal" argument to "_onProcessExit" Add missing signal" argument --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 31eb20a..bf351f8 100644 --- a/index.js +++ b/index.js @@ -33,10 +33,10 @@ function EdgeBrowser (baseBrowserDecorator) { // Override onProcessExit to manage edge shutdown var baseOnProcessExit = this._onProcessExit - this._onProcessExit = function (code, errorOutput) { + this._onProcessExit = function (code, signal, errorOutput) { // In case of error return immediatly if (code > 0 || errorOutput.length > 0) { - baseOnProcessExit(code, errorOutput) + baseOnProcessExit(code, signal, errorOutput) } else { // Start stop process to close edge gracefully var stopProcess = spawn(self.DEFAULT_CMD.win32, [ stopScriptPath ]) @@ -48,7 +48,7 @@ function EdgeBrowser (baseBrowserDecorator) { stopProcess.on('error', self._onStderr) stopProcess.on('exit', function (code) { - baseOnProcessExit(code, errorOutput) + baseOnProcessExit(code, signal, errorOutput) }) } } From ca1728d90c4887e1fdabcaff0e0ff76d6ea9d260 Mon Sep 17 00:00:00 2001 From: John Slegers Date: Tue, 23 Apr 2019 20:29:03 +0200 Subject: [PATCH 3/9] Fix unit test --- test/launchers/launcher.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/launchers/launcher.spec.js b/test/launchers/launcher.spec.js index 14963a3..9d02aa7 100644 --- a/test/launchers/launcher.spec.js +++ b/test/launchers/launcher.spec.js @@ -182,7 +182,7 @@ describe('launcher', function () { }) injector = new di.Injector([module, EdgeLauncher]) launcher = injector.get('launcher:Edge') - launcher._onProcessExit(0, '') + launcher._onProcessExit(0, null, '') } }) From 0e66a36f150531452efee84081ac1d3045e7e370 Mon Sep 17 00:00:00 2001 From: John Slegers Date: Tue, 23 Apr 2019 20:35:57 +0200 Subject: [PATCH 4/9] Update node versions to test --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index b925f41..e989aad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,12 +2,11 @@ os: - windows language: node_js node_js: - - 4 - - 5 - 6 - 7 - 8 - 9 + - 10 install: npm install script: - npm test From c4a7b22b7303ec1437c7dfe396d26f343e9c9c95 Mon Sep 17 00:00:00 2001 From: John Slegers Date: Wed, 24 Apr 2019 18:05:35 +0200 Subject: [PATCH 5/9] Fix starting & stopping behavior --- appveyor.yml | 5 ++-- index.js | 45 +++++++++++++++------------------ package.json | 3 ++- scripts/start_edge.ps1 | 34 +++++++++++++++---------- scripts/stop_edge.ps1 | 35 ------------------------- test/launchers/launcher.spec.js | 14 ---------- 6 files changed, 45 insertions(+), 91 deletions(-) delete mode 100644 scripts/stop_edge.ps1 diff --git a/appveyor.yml b/appveyor.yml index c8e03ca..48a083f 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,9 +1,10 @@ environment: matrix: - - nodejs_version: "4" - - nodejs_version: "5" - nodejs_version: "6" - nodejs_version: "7" + - nodejs_version: "8" + - nodejs_version: "9" + - nodejs_version: "10" install: - ps: Install-Product node $env:nodejs_version - npm install diff --git a/index.js b/index.js index bf351f8..16f1227 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ // ------------ var path = require('path') -var spawn = require('child_process').spawn +var exec = require('child_process').exec var backslashRegex = /\\/g var escapeBackslash = '\\\\' @@ -15,16 +15,23 @@ var startScriptPath = path .join(__dirname, 'scripts/start_edge.ps1') .replace(backslashRegex, escapeBackslash) .replace(spaceRegex, escapeSpace) -var stopScriptPath = path - .join(__dirname, 'scripts/stop_edge.ps1') - .replace(backslashRegex, escapeBackslash) - .replace(spaceRegex, escapeSpace) // Constructor -function EdgeBrowser (baseBrowserDecorator) { +function EdgeBrowser (baseBrowserDecorator, logger) { baseBrowserDecorator(this) - var self = this + var log = logger.create('launcher') + + function killEdgeProcess (cb) { + exec('taskkill /t /f /im MicrosoftEdge.exe', function (err) { + if (err) { + log.error('Killing Edge process failed. ' + err) + } else { + log.debug('Killed Edge process') + } + cb() + }) + } // Use start_edge script path as powershell argument, and url as script argument this._getOptions = function (url) { @@ -34,23 +41,11 @@ function EdgeBrowser (baseBrowserDecorator) { // Override onProcessExit to manage edge shutdown var baseOnProcessExit = this._onProcessExit this._onProcessExit = function (code, signal, errorOutput) { - // In case of error return immediatly - if (code > 0 || errorOutput.length > 0) { - baseOnProcessExit(code, signal, errorOutput) - } else { - // Start stop process to close edge gracefully - var stopProcess = spawn(self.DEFAULT_CMD.win32, [ stopScriptPath ]) - - stopProcess.stdout.on('data', self._onStdout) - - stopProcess.stderr.on('data', self._onStderr) - - stopProcess.on('error', self._onStderr) - - stopProcess.on('exit', function (code) { + killEdgeProcess(function () { + if (baseOnProcessExit) { baseOnProcessExit(code, signal, errorOutput) - }) - } + } + }) } } @@ -62,11 +57,11 @@ EdgeBrowser.prototype = { ENV_CMD: 'EDGE_BIN' } -EdgeBrowser.$inject = ['baseBrowserDecorator'] +EdgeBrowser.$inject = ['baseBrowserDecorator', 'logger'] // Publish di module // ----------------- module.exports = { 'launcher:Edge': ['type', EdgeBrowser] -} +} \ No newline at end of file diff --git a/package.json b/package.json index 81c2810..9667a91 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "Andreas Krummsdorf ", "Marcos Cáceres ", "Nikita Khomyakov ", - "Christopher Currie " + "Christopher Currie ", + "John Slegers " ] } diff --git a/scripts/start_edge.ps1 b/scripts/start_edge.ps1 index 6b63aeb..7fb5b18 100644 --- a/scripts/start_edge.ps1 +++ b/scripts/start_edge.ps1 @@ -1,17 +1,22 @@ -<# -@author [Tristan Valcke]{@link https://github.com/Itee} -@license [MIT]{@link https://opensource.org/licenses/MIT} - -@description This script will attemp to start Microsoft Edge browser and wait the process to be detach -#> param([string]$url = "") -# Check if edge is already running and cancel run if one is found. -$MicrosoftEdgeProcess = Get-Process "MicrosoftEdge" -ErrorAction SilentlyContinue -$MicrosoftEdgeCPProcess = Get-Process "MicrosoftEdgeCP" -ErrorAction SilentlyContinue -if( $MicrosoftEdgeProcess -or $MicrosoftEdgeCPProcess ) { - Write-Output "An instance of Windows Edge browser is already running. Please close it and try again." - exit 1 +try { + $MicrosoftEdgePath = Join-Path $ENV:APPDATA "..\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe" -Resolve -ErrorAction SilentlyContinue + if ($MicrosoftEdgePath) { + # Delete Edge's data folder starting from the inner most file + Get-ChildItem -Path $MicrosoftEdgePath -Force -Recurse | + Sort-Object -Property FullName -Descending | + Remove-Item -Recurse -Force + } + +Get-AppXPackage -Name Microsoft.MicrosoftEdge | + Foreach { + Add-AppxPackage -DisableDevelopmentMode -Register "$( $_.InstallLocation )\AppXManifest.xml" -Verbose + } +} +catch +{ + Write-Output "An unexpected error occured while resetting Microsoft Edge." } # Start our Microsoft Edge instance @@ -19,6 +24,7 @@ try { Start-Process -FilePath "microsoft-edge:$url" Wait-Process -Name "MicrosoftEdge" -ErrorAction Stop + exit 0 } catch [ Microsoft.PowerShell.Commands.ProcessCommandException ] { @@ -27,6 +33,6 @@ catch [ Microsoft.PowerShell.Commands.ProcessCommandException ] } catch { - Write-Output "An unexpected error occure." + Write-Output "An unexpected error occured while starting Microsoft Edge." exit 1 -} +} \ No newline at end of file diff --git a/scripts/stop_edge.ps1 b/scripts/stop_edge.ps1 deleted file mode 100644 index 7150243..0000000 --- a/scripts/stop_edge.ps1 +++ /dev/null @@ -1,35 +0,0 @@ -<# -@author [Tristan Valcke]{@link https://github.com/Itee} -@license [MIT]{@link https://opensource.org/licenses/MIT} - -@description This script will attemp to close gracefully Microsoft Edge tabs then browser itself -#> - -$MAX_TRY = 5000 -$try = 0 -# We need to use force loop because edge process will pop again and again... -do{ - - # Close tabs sub process - $MicrosoftEdgeCPProcess = Get-Process "MicrosoftEdgeCP" -ErrorAction SilentlyContinue - if( $MicrosoftEdgeCPProcess ) { - $MicrosoftEdgeCPProcess.CloseMainWindow() | out-null - } - - # Close Edge browser - $MicrosoftEdgeProcess = Get-Process "MicrosoftEdge" -ErrorAction SilentlyContinue - if( $MicrosoftEdgeProcess ) { - $MicrosoftEdgeProcess.CloseMainWindow() | out-null - } - - $try++ - -} while( ( $MicrosoftEdgeCPProcess -or $MicrosoftEdgeProcess ) -and $try -lt $MAX_TRY ) - -if( $try -eq $MAX_TRY ){ - Write-Output "Unable to close Microsoft Edge browser and child process correctly." - exit 1 -} else { - exit 0 -} - diff --git a/test/launchers/launcher.spec.js b/test/launchers/launcher.spec.js index 9d02aa7..7023e3f 100644 --- a/test/launchers/launcher.spec.js +++ b/test/launchers/launcher.spec.js @@ -185,19 +185,5 @@ describe('launcher', function () { launcher._onProcessExit(0, null, '') } }) - - it('should spawn powershell stop_edge.ps1 script', function (done) { - onProcessExit() - - var powershellPath = path.normalize(childProcessCmd) - expect(powershellPath).to.be.a.file() - expect(powershellPath).to.include('powershell.exe') - - var scriptPath = path.normalize(childProcessArgs[0]) - expect(scriptPath).to.be.a.file() - expect(scriptPath).to.include('stop_edge.ps1') - - done() - }) }) }) From 415405bac5dcd88dc77737b8023aeafd6326fab5 Mon Sep 17 00:00:00 2001 From: John Slegers Date: Wed, 24 Apr 2019 18:12:34 +0200 Subject: [PATCH 6/9] Add missing newline --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 16f1227..961026b 100644 --- a/index.js +++ b/index.js @@ -64,4 +64,4 @@ EdgeBrowser.$inject = ['baseBrowserDecorator', 'logger'] module.exports = { 'launcher:Edge': ['type', EdgeBrowser] -} \ No newline at end of file +} From 9f3d1775307fc7dddaa107b68b21415bae7670d2 Mon Sep 17 00:00:00 2001 From: John Slegers Date: Wed, 24 Apr 2019 19:12:55 +0200 Subject: [PATCH 7/9] Update unit tests --- test/launchers/launcher.spec.js | 39 +++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/test/launchers/launcher.spec.js b/test/launchers/launcher.spec.js index 7023e3f..aebf4ad 100644 --- a/test/launchers/launcher.spec.js +++ b/test/launchers/launcher.spec.js @@ -46,7 +46,17 @@ describe('launcher', function () { captureTimeoutLauncherDecorator: ['factory', captureTimeoutDecorator], retryLauncherDecorator: ['factory', retryDecorator], processLauncherDecorator: ['factory', processDecorator], - baseBrowserDecorator: ['factory', baseBrowserDecoratorFactory] + baseBrowserDecorator: ['factory', baseBrowserDecoratorFactory], + logger: [ + 'value', { + create: function () { + return { + error: function () {}, + debug: function () {} + } + } + } + ] } }) @@ -166,24 +176,31 @@ describe('launcher', function () { }) describe('_onProcessExit', function () { - var childProcessCmd, childProcessArgs, onProcessExit + var childProcessCmd, onProcessExit beforeEach(function () { onProcessExit = function () { - EdgeLauncher = proxyquire('../../index', { - child_process: { - spawn: function (cmd, args) { - childProcessCmd = cmd - childProcessArgs = args - - return spawn(cmd, args) - } + var childProcessMock + childProcessMock = { + exec: function (cmd, s, cb) { + childProcessCmd = cmd + cb() } + } + + EdgeLauncher = proxyquire('..', { + child_process: childProcessMock }) injector = new di.Injector([module, EdgeLauncher]) launcher = injector.get('launcher:Edge') - launcher._onProcessExit(0, null, '') + launcher._onProcessExit(1, 2, 3) } }) + + it('should call taskkill', function (done) { + onProcessExit() + expect(childProcessCmd).to.equal('taskkill /t /f /im MicrosoftEdge.exe') + done() + }) }) }) From 8518b01943f21da959c9877821d384bf437ca244 Mon Sep 17 00:00:00 2001 From: John Slegers Date: Wed, 24 Apr 2019 19:24:33 +0200 Subject: [PATCH 8/9] Fix unit tests --- test/launchers/launcher.spec.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/test/launchers/launcher.spec.js b/test/launchers/launcher.spec.js index aebf4ad..9400ffb 100644 --- a/test/launchers/launcher.spec.js +++ b/test/launchers/launcher.spec.js @@ -177,23 +177,20 @@ describe('launcher', function () { describe('_onProcessExit', function () { var childProcessCmd, onProcessExit - + beforeEach(function () { onProcessExit = function () { - var childProcessMock - childProcessMock = { - exec: function (cmd, s, cb) { - childProcessCmd = cmd - cb() + EdgeLauncher = proxyquire('../../index', { + child_process: { + exec: function (cmd, s, cb) { + childProcessCmd = cmd + cb() + } } - } - - EdgeLauncher = proxyquire('..', { - child_process: childProcessMock }) injector = new di.Injector([module, EdgeLauncher]) launcher = injector.get('launcher:Edge') - launcher._onProcessExit(1, 2, 3) + launcher._onProcessExit(0, null, '') } }) From 0c78243f5a6ced80fc6910e94a2dcb269a0d7869 Mon Sep 17 00:00:00 2001 From: John Slegers Date: Wed, 24 Apr 2019 19:29:16 +0200 Subject: [PATCH 9/9] Fix unit test --- test/launchers/launcher.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/launchers/launcher.spec.js b/test/launchers/launcher.spec.js index 9400ffb..26c1ba9 100644 --- a/test/launchers/launcher.spec.js +++ b/test/launchers/launcher.spec.js @@ -182,7 +182,7 @@ describe('launcher', function () { onProcessExit = function () { EdgeLauncher = proxyquire('../../index', { child_process: { - exec: function (cmd, s, cb) { + exec: function (cmd, cb) { childProcessCmd = cmd cb() }