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 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 b4758c4..961026b 100644 --- a/index.js +++ b/index.js @@ -5,18 +5,33 @@ // ------------ var path = require('path') -var spawn = require('child_process').spawn +var exec = require('child_process').exec -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) // 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) { @@ -25,24 +40,12 @@ function EdgeBrowser (baseBrowserDecorator) { // Override onProcessExit to manage edge shutdown var baseOnProcessExit = this._onProcessExit - this._onProcessExit = function (code, errorOutput) { - // In case of error return immediatly - if (code > 0 || errorOutput.length > 0) { - baseOnProcessExit(code, 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) { - baseOnProcessExit(code, errorOutput) - }) - } + this._onProcessExit = function (code, signal, errorOutput) { + killEdgeProcess(function () { + if (baseOnProcessExit) { + baseOnProcessExit(code, signal, errorOutput) + } + }) } } @@ -54,7 +57,7 @@ EdgeBrowser.prototype = { ENV_CMD: 'EDGE_BIN' } -EdgeBrowser.$inject = ['baseBrowserDecorator'] +EdgeBrowser.$inject = ['baseBrowserDecorator', 'logger'] // Publish di module // ----------------- 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 14963a3..26c1ba9 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,37 +176,27 @@ 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) { + exec: function (cmd, cb) { childProcessCmd = cmd - childProcessArgs = args - - return spawn(cmd, args) + cb() } } }) injector = new di.Injector([module, EdgeLauncher]) launcher = injector.get('launcher:Edge') - launcher._onProcessExit(0, '') + launcher._onProcessExit(0, null, '') } }) - it('should spawn powershell stop_edge.ps1 script', function (done) { + it('should call taskkill', 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') - + expect(childProcessCmd).to.equal('taskkill /t /f /im MicrosoftEdge.exe') done() }) })