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
·131 lines (106 loc) · 3.09 KB
/
frame.js
File metadata and controls
executable file
·131 lines (106 loc) · 3.09 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const fs = require('fs');
const path = require('path');
const moment = require('moment-timezone');
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.timezone = config.get('timezone');
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;
frame.timezone = json.timezone;
frame.validate();
return frame;
}
static fromFile(config, file) {
return frame.fromJson(config, JSON.parse(fs.readFileSync(file)));
}
startMe() {
this._start = this._getCurrentDate();
this.write();
return this;
}
stopMe() {
this._stop = this._getCurrentDate();
this.write();
return this;
}
validate() {
moment.suppressDeprecationWarnings = true;
if(!moment(this._start).isValid())
throw `Error: Start date is not in a valid ISO date format!`;
if(this._stop && !moment(this._stop).isValid())
throw `Error: Stop date is not in a valid ISO date format!`;
moment.suppressDeprecationWarnings = false;
}
_getCurrentDate() {
if(this.timezone)
return moment().tz(this.timezone).format();
return moment();
}
/**
* 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,
timezone: this.timezone
}, null, "\t"));
}
get file() {
return path.join(this.config.frameDir, this.id + '.json');
}
get duration() {
return moment(this.stop).diff(this.start) / 1000;
}
get date() {
return this.start;
}
get start() {
return this.timezone ? moment(this._start).tz(this.timezone) : moment(this._start);
}
get stop() {
return this.timezone ? moment(this._stop).tz(this.timezone) : (this._stop ? moment(this._stop) : false );
}
/**
* generate a unique id
* @returns {number}
*/
static generateId() {
return hashids.encode(new Date().getTime());
}
}
module.exports = frame;