1- const gaze = require ( "gaze" ) ;
1+ import * as choki from "chokidar" ;
22import * as path from "path" ;
33import * as os from "os" ;
44
5- const hostInfo : IHostInfo = $injector . resolve ( "hostInfo" ) ;
6-
75class CancellationService implements ICancellationService {
8- private watches : IDictionary < IWatcherInstance > = { } ;
6+ private watches : IDictionary < choki . FSWatcher > = { } ;
97
108 constructor ( private $fs : IFileSystem ,
11- private $logger : ILogger ) {
12- this . $fs . createDirectory ( CancellationService . killSwitchDir ) ;
13- this . $fs . chmod ( CancellationService . killSwitchDir , "0777" ) ;
9+ private $logger : ILogger ,
10+ private $hostInfo : IHostInfo ) {
11+
12+ if ( this . $hostInfo . isWindows ) {
13+ this . $fs . createDirectory ( CancellationService . killSwitchDir ) ;
14+ }
15+
1416 }
1517
1618 public async begin ( name : string ) : Promise < void > {
19+ if ( ! this . $hostInfo . isWindows ) {
20+ return ;
21+ }
22+
1723 const triggerFile = CancellationService . makeKillSwitchFileName ( name ) ;
1824
1925 if ( ! this . $fs . exists ( triggerFile ) ) {
2026 this . $fs . writeFile ( triggerFile , "" ) ;
21-
22- if ( ! hostInfo . isWindows ) {
23- this . $fs . chmod ( triggerFile , "0777" ) ;
24- }
2527 }
2628
2729 this . $logger . trace ( "Starting watch on killswitch %s" , triggerFile ) ;
2830
29- const watcherInitialized = new Promise < IWatcherInstance > ( ( resolve , reject ) => {
30- gaze ( triggerFile , function ( err : any , watcher : any ) {
31- this . on ( "deleted" , ( filePath : string ) => process . exit ( ) ) ;
32-
33- if ( err ) {
34- reject ( err ) ;
35- } else {
36- resolve ( watcher ) ;
37- }
31+ const watcher = choki . watch ( triggerFile , { ignoreInitial : true } )
32+ . on ( "unlink" , ( filePath : string ) => {
33+ this . $logger . info ( `Exiting process as the file ${ filePath } has been deleted. Probably reinstalling CLI while there's a working instance.` ) ;
34+ process . exit ( ErrorCodes . DELETED_KILL_FILE ) ;
3835 } ) ;
39- } ) ;
40-
41- const watcher = await watcherInitialized ;
4236
4337 if ( watcher ) {
4438 this . watches [ name ] = watcher ;
@@ -47,8 +41,10 @@ class CancellationService implements ICancellationService {
4741
4842 public end ( name : string ) : void {
4943 const watcher = this . watches [ name ] ;
50- delete this . watches [ name ] ;
51- watcher . close ( ) ;
44+ if ( watcher ) {
45+ delete this . watches [ name ] ;
46+ watcher . close ( ) ;
47+ }
5248 }
5349
5450 public dispose ( ) : void {
@@ -64,22 +60,4 @@ class CancellationService implements ICancellationService {
6460 }
6561}
6662
67- class CancellationServiceDummy implements ICancellationService {
68- dispose ( ) : void {
69- /* intentionally left blank */
70- }
71-
72- async begin ( name : string ) : Promise < void > {
73- return ;
74- }
75-
76- end ( name : string ) : void {
77- /* intentionally left blank */
78- }
79- }
80-
81- if ( hostInfo . isWindows ) {
82- $injector . register ( "cancellation" , CancellationService ) ;
83- } else {
84- $injector . register ( "cancellation" , CancellationServiceDummy ) ;
85- }
63+ $injector . register ( "cancellation" , CancellationService ) ;
0 commit comments