forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe.js
More file actions
executable file
·96 lines (80 loc) · 2.1 KB
/
frame.js
File metadata and controls
executable file
·96 lines (80 loc) · 2.1 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const fs = require('fs');
const path = require('path');
const moment = require('moment');
const Hashids = require('hashids');
const hashids = new Hashids();
class frame {
/**
* constructor.
* @param config
* @param id
* @param type
*/
constructor(config, id, type) {
this.config = config;
this.project = config.get('project');
this.resource = {id, type};
if(typeof id === 'string' || id instanceof String)
this.resource.new = true;
this.id = frame.generateId();
this.start = false;
this.stop = false;
this.notes = [];
}
static fromJson(config, json) {
let frame = new this(config, json.resource.id, json.resource.type);
frame.project = json.project;
frame.id = json.id;
frame.start = json.start;
frame.stop = json.stop;
frame.notes = json.notes;
return frame;
}
static fromFile(config, file) {
return frame.fromJson(config, JSON.parse(fs.readFileSync(file)));
}
startMe() {
this.start = new Date();
this.write();
return this;
}
stopMe() {
this.stop = new Date();
this.write();
return this;
}
/**
* assert file exists
*/
assertFile() {
if (!fs.existsSync(this.file)) fs.appendFileSync(this.file, '');
}
/**
* write data to file
*/
write() {
if (fs.existsSync(this.file)) fs.unlinkSync(this.file);
fs.appendFileSync(this.file, JSON.stringify({
id: this.id,
project: this.project,
resource: this.resource,
notes: this.notes,
start: this.start,
stop: this.stop
}, null, "\t"));
}
get file() {
return path.join(this.config.frameDir, this.id + '.json');
}
get duration() {
return moment(this.stop).diff(this.start) / 1000;
}
/**
* generate a unique id
* @returns {number}
*/
static generateId() {
return hashids.encode(new Date().getTime());
}
}
module.exports = frame;