11const DEV_MODE = process . env . HTK_DEV === 'true' ;
22
3- import * as Sentry from '@sentry/electron' ;
4- import { RewriteFrames } from '@sentry/integrations' ;
5-
6- if ( ! DEV_MODE ) {
7- Sentry . init ( {
8- dsn :
'https://[email protected] /1367048' , 9- integrations : [
10- new RewriteFrames ( {
11- // Make all paths relative to this root, because otherwise it can make
12- // errors unnecessarily distinct, especially on Windows.
13- root : process . platform === 'win32'
14- // Root must always be POSIX format, so we transform it on Windows:
15- ? __dirname
16- . replace ( / ^ [ A - Z ] : / , '' ) // remove Windows-style prefix
17- . replace ( / \\ / g, '/' ) // replace all `\\` instances with `/`
18- : __dirname
19- } )
20- ]
21- } ) ;
22- }
23-
24- function reportError ( error : Error | string ) {
25- console . log ( error ) ;
3+ // Set up error handling before everything else:
4+ import { reportError , addBreadcrumb } from './errors' ;
265
27- if ( typeof error === 'string' ) {
28- Sentry . captureMessage ( error ) ;
29- } else {
30- Sentry . captureException ( error ) ;
31- }
32- }
33-
34- import { spawn , exec , ChildProcess } from 'child_process' ;
6+ import { spawn , ChildProcess } from 'child_process' ;
357import * as os from 'os' ;
368import { promises as fs } from 'fs'
379import * as net from 'net' ;
@@ -44,6 +16,7 @@ import * as uuid from 'uuid/v4';
4416import * as yargs from 'yargs' ;
4517import * as semver from 'semver' ;
4618import * as rimraf from 'rimraf' ;
19+ const rmRF = promisify ( rimraf ) ;
4720import * as windowStateKeeper from 'electron-window-state' ;
4821import { getSystemProxy } from 'os-proxy-config' ;
4922
@@ -55,8 +28,7 @@ registerContextMenu({
5528import { reportStartupEvents } from './report-install-event' ;
5629import { getMenu , shouldAutoHideMenu } from './menu' ;
5730import { getDeferred , delay } from './util' ;
58-
59- const rmRF = promisify ( rimraf ) ;
31+ import { stopServer } from './stop-server' ;
6032
6133const packageJson = require ( '../package.json' ) ;
6234
@@ -166,34 +138,21 @@ if (!amMainInstance) {
166138 }
167139
168140 let serverKilled = false ;
169- app . on ( 'will-quit' , ( event ) => {
141+ app . on ( 'will-quit' , async ( event ) => {
170142 if ( server && ! serverKilled ) {
143+ // Don't shutdown until we've tried to kill the server
144+ event . preventDefault ( ) ;
145+
171146 serverKilled = true ;
147+
172148 try {
173- if ( isWindows ) {
174- // Don't shutdown until we've tried to kill the server
175- event . preventDefault ( ) ;
176-
177- // Forcefully kill the pid (the cmd) and child processes
178- exec ( `taskkill /pid ${ server . pid } /T /F` , ( error , stdout , stderr ) => {
179- if ( error ) {
180- console . log ( stdout ) ;
181- console . log ( stderr ) ;
182- reportError ( error ) ;
183- }
184-
185- // We've done our best - now shut down for real. Disable errors, otherwise
186- // we can receive reports for invisible errors during/just after exit.
187- app . quit ( ) ;
188- } ) ;
189- } else {
190- // Make sure we clean up the whole group (shell + node).
191- // https://azimi.me/2014/12/31/kill-child_process-node-js.html
192- process . kill ( - server . pid ! ) ;
193- }
149+ await stopServer ( server , AUTH_TOKEN ) ;
150+ // We've done our best - now shut down for real.
151+ app . quit ( ) ;
194152 } catch ( error ) {
195153 console . log ( 'Failed to kill server' , error ) ;
196154 reportError ( error ) ;
155+ app . quit ( ) ;
197156 }
198157 }
199158 } ) ;
@@ -398,13 +357,13 @@ if (!amMainInstance) {
398357 stderr . pipe ( process . stderr ) ;
399358
400359 server . stdout ! . on ( 'data' , ( data ) => {
401- Sentry . addBreadcrumb ( { category : 'server-stdout' , message : data . toString ( 'utf8' ) , level : < any > 'info' } ) ;
360+ addBreadcrumb ( { category : 'server-stdout' , message : data . toString ( 'utf8' ) , level : < any > 'info' } ) ;
402361 } ) ;
403362
404363 let lastError : string | undefined = undefined ;
405364 stderr . on ( 'data' , ( data ) => {
406365 const errorOutput = data . toString ( 'utf8' ) ;
407- Sentry . addBreadcrumb ( { category : 'server-stderr' , message : errorOutput , level : < any > 'warning' } ) ;
366+ addBreadcrumb ( { category : 'server-stderr' , message : errorOutput , level : < any > 'warning' } ) ;
408367
409368 // Remember the last '*Error:' line we saw.
410369 lastError = errorOutput
@@ -434,7 +393,7 @@ if (!amMainInstance) {
434393 error = new Error ( `Server shutdown unexpectedly with code ${ errorOrCode } ` ) ;
435394 }
436395
437- Sentry . addBreadcrumb ( { category : 'server-exit' , message : error . message , level : < any > 'error' , data : { serverRunTime } } ) ;
396+ addBreadcrumb ( { category : 'server-exit' , message : error . message , level : < any > 'error' , data : { serverRunTime } } ) ;
438397 reportError ( error ) ;
439398
440399 showErrorAlert (
@@ -480,7 +439,8 @@ if (!amMainInstance) {
480439 } ) ;
481440
482441 // Check we're happy using the default proxy settings
483- getSystemProxy ( ) . then ( ( proxyConfig ) => {
442+ getSystemProxy ( )
443+ . then ( ( proxyConfig ) => {
484444 let shouldDisableProxy = false ;
485445
486446 if ( proxyConfig ) {
0 commit comments