1
- const gaze = require ( "gaze" ) ;
1
+ import * as choki from "chokidar" ;
2
2
import * as path from "path" ;
3
3
import * as os from "os" ;
4
4
5
- const hostInfo : IHostInfo = $injector . resolve ( "hostInfo" ) ;
6
-
7
5
class CancellationService implements ICancellationService {
8
- private watches : IDictionary < IWatcherInstance > = { } ;
6
+ private watches : IDictionary < choki . FSWatcher > = { } ;
9
7
10
8
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
+
14
16
}
15
17
16
18
public async begin ( name : string ) : Promise < void > {
19
+ if ( ! this . $hostInfo . isWindows ) {
20
+ return ;
21
+ }
22
+
17
23
const triggerFile = CancellationService . makeKillSwitchFileName ( name ) ;
18
24
19
25
if ( ! this . $fs . exists ( triggerFile ) ) {
20
26
this . $fs . writeFile ( triggerFile , "" ) ;
21
-
22
- if ( ! hostInfo . isWindows ) {
23
- this . $fs . chmod ( triggerFile , "0777" ) ;
24
- }
25
27
}
26
28
27
29
this . $logger . trace ( "Starting watch on killswitch %s" , triggerFile ) ;
28
30
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 ) ;
38
35
} ) ;
39
- } ) ;
40
-
41
- const watcher = await watcherInitialized ;
42
36
43
37
if ( watcher ) {
44
38
this . watches [ name ] = watcher ;
@@ -47,8 +41,10 @@ class CancellationService implements ICancellationService {
47
41
48
42
public end ( name : string ) : void {
49
43
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
+ }
52
48
}
53
49
54
50
public dispose ( ) : void {
@@ -64,22 +60,4 @@ class CancellationService implements ICancellationService {
64
60
}
65
61
}
66
62
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