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
·54 lines (44 loc) · 1.36 KB
/
filesystem.js
File metadata and controls
executable file
·54 lines (44 loc) · 1.36 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
import _ from 'underscore';
import fs from 'fs';
import path from 'path';
import open from 'open';
import find from 'find-in-files';
import child_process from 'child_process';
class filesystem {
static find(pattern, dir) {
return new Promise((resolve, reject) => {
find.find(pattern, dir)
.then(results => resolve(_.keys(results)))
.catch(error => reject(error));
});
}
static exists(file) {
return fs.existsSync(file);
}
static remove(file) {
return fs.unlinkSync(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) {
return _.max(filesystem.readDir(dir), file => (fs.statSync(path.join(dir, file.name)).ctime));
}
static all(dir) {
return _.sortBy(filesystem.readDir(dir), file => (fs.statSync(path.join(dir, file.name)).ctime));
}
static readDir(dir) {
return fs.readdirSync(dir, { withFileTypes: true }).filter(file=>file.isFile() && file.name.endsWith(".json"));
}
}
export default filesystem;