This repository was archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcancellation.ts
85 lines (66 loc) · 2.07 KB
/
cancellation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const gaze = require("gaze");
import * as path from "path";
import * as os from "os";
const hostInfo: IHostInfo = $injector.resolve("hostInfo");
class CancellationService implements ICancellationService {
private watches: IDictionary<IWatcherInstance> = {};
constructor(private $fs: IFileSystem,
private $logger: ILogger) {
this.$fs.createDirectory(CancellationService.killSwitchDir);
this.$fs.chmod(CancellationService.killSwitchDir, "0777");
}
public async begin(name: string): Promise<void> {
const triggerFile = CancellationService.makeKillSwitchFileName(name);
if (!this.$fs.exists(triggerFile)) {
this.$fs.writeFile(triggerFile, "");
if (!hostInfo.isWindows) {
this.$fs.chmod(triggerFile, "0777");
}
}
this.$logger.trace("Starting watch on killswitch %s", triggerFile);
const watcherInitialized = new Promise<IWatcherInstance>((resolve, reject) => {
gaze(triggerFile, function (err: any, watcher: any) {
this.on("deleted", (filePath: string) => process.exit());
if (err) {
reject(err);
} else {
resolve(watcher);
}
});
});
const watcher = await watcherInitialized;
if (watcher) {
this.watches[name] = watcher;
}
}
public end(name: string): void {
const watcher = this.watches[name];
delete this.watches[name];
watcher.close();
}
public dispose(): void {
_(this.watches).keys().each(name => this.end(name));
}
private static get killSwitchDir(): string {
return path.join(os.tmpdir(), process.env.SUDO_USER || process.env.USER || process.env.USERNAME || '', "KillSwitches");
}
private static makeKillSwitchFileName(name: string): string {
return path.join(CancellationService.killSwitchDir, name);
}
}
class CancellationServiceDummy implements ICancellationService {
dispose(): void {
/* intentionally left blank */
}
async begin(name: string): Promise<void> {
return;
}
end(name: string): void {
/* intentionally left blank */
}
}
if (hostInfo.isWindows) {
$injector.register("cancellation", CancellationService);
} else {
$injector.register("cancellation", CancellationServiceDummy);
}