forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfilesystem.js
More file actions
executable file
·61 lines (47 loc) · 1.47 KB
/
Copy pathfilesystem.js
File metadata and controls
executable file
·61 lines (47 loc) · 1.47 KB
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
import fs from 'fs';
import path from 'path';
import open from 'open';
import child_process from 'child_process';
class Filesystem {
static exists(file) {
return fs.existsSync(file);
}
static remove(file) {
return fs.unlinkSync(file);
}
static readText(file) {
return fs.readFileSync(file, 'utf8');
}
static writeText(file, data) {
return fs.writeFileSync(file, data);
}
static truncate(file) {
return fs.truncateSync(file);
}
static open(file) {
let editor = process.env.VISUAL;
if (editor || (editor = process.env.EDITOR)) {
return child_process.spawn(editor, [file], {
stdio: 'inherit'
});
} else {
return open(file);
}
}
static join(...args) {
return path.join(...args);
}
static newest(dir) {
let files = Filesystem.readDir(dir);
let ctime = file => fs.statSync(path.join(dir, file.name)).ctime;
return files.reduce((newest, file) => (ctime(file) > ctime(newest) ? file : newest), files[0] ?? -Infinity);
}
static all(dir) {
let ctime = file => fs.statSync(path.join(dir, file.name)).ctime;
return Filesystem.readDir(dir).sort((a, b) => ctime(a) - ctime(b));
}
static readDir(dir) {
return fs.readdirSync(dir, { withFileTypes: true }).filter(file=>file.isFile() && file.name.endsWith(".json"));
}
}
export default Filesystem;