-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.js
54 lines (46 loc) · 1.2 KB
/
utils.js
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
import {Stream} from "stream";
import {utimes, close, open} from "fs"
export class ArrayFormatter extends Stream {
constructor() {
super();
this.writable = false;
this.done = false;
}
write(s) {
if (!this.writable) {
this.writable = true;
this.emit('data', '[' + s);
} else {
this.emit('data', ',' + s)
}
return true;
}
end() {
if (this.done) return;
this.done = true;
if (!this.writable) {
this.emit('data', '['); // empty result
}
this.emit('data', ']');
this.emit('end');
}
}
export function esc_query(q) {
let word = q.replace(/^[\^'!=]+/, '');
word = word.replace(/[\$]+$/, '');
return word
}
export function touchp(path) {
return new Promise((resolve, reject) => {
const time = new Date();
utimes(path, time, time, err => {
if (err) {
return open(path, 'w', (err, fd) => {
if (err) return reject(err);
close(fd, err => (err ? reject(err) : resolve(fd)));
});
}
resolve();
});
});
}