-
Notifications
You must be signed in to change notification settings - Fork 2
/
PidFile.ts
44 lines (40 loc) · 1011 Bytes
/
PidFile.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
import { open, unlink, write } from 'fs';
import { pid } from 'process';
import { doesExist, Optional } from './Maybe';
type OptionalErrno = Optional<NodeJS.ErrnoException>;
/**
* Write the current process ID to a file at the given `path`.
*
* @public
*/
export async function writePid(path: string): Promise<void> {
return new Promise((res, rej) => {
open(path, 'wx', (openErr: OptionalErrno, fd: number) => {
if (doesExist(openErr)) {
rej(openErr);
} else {
write(fd, pid.toString(), 0, 'utf8', (writeErr: OptionalErrno) => {
if (doesExist(writeErr)) {
rej(writeErr);
} else {
res();
}
});
}
});
});
}
/**
* Remove the file at the given `path`.
*/
export async function removePid(path: string): Promise<void> {
return new Promise((res, rej) => {
unlink(path, (err: OptionalErrno) => {
if (doesExist(err)) {
rej(err);
} else {
res();
}
});
});
}