forked from danibram/time-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.js
More file actions
99 lines (81 loc) · 2.34 KB
/
Copy pathManager.js
File metadata and controls
99 lines (81 loc) · 2.34 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
import flat from 'flat'
import moment from 'moment'
import Configstore from 'configstore'
import pkg from '../package.json'
import {recognizeModifierTiming} from './Utils'
export class Manager {
constructor(cfg) {
this.tasks = cfg.all.tasks
}
getTask(name) {
return (this.tasks[name]) ? this.tasks[name] : {}
}
storeTasks() {
config.set('tasks', this.tasks)
}
setTask(name, obj) {
let update = {}
update[name] = obj
this.tasks = Object.assign({}, this.tasks, update)
this.storeTasks()
}
start(name, description) {
let t = this.getTask(name)
if (!t.start) {
t.start = moment().toDate()
if (description) t.description = description
this.setTask(name, t)
}
}
stop(name, description) {
let t = this.getTask(name)
if (!t.stop) {
t.stop = moment().toDate()
if (description) t.description = description
this.setTask(name, t)
}
}
getTime(name) {
let t = this.getTask(name)
if (t.stop) {
return moment(t.stop).diff(moment(t.start), 'seconds')
} else {
return moment().diff(moment(t.start), 'seconds')
}
}
modifyTask(operation, name, stringTime){
let t = this.getTask(name)
if (t.stop){
let newStop = moment(t.stop)
let parsed = recognizeModifierTiming(stringTime)
parsed.map((t)=>{
newStop = newStop[operation](t.value, t.momentKey)
})
if (operation === 'subtract' && newStop.isBefore(t.start)){
console.error('You cant subtract more time')
return
}
t.stop = newStop
this.setTask(name, t)
}
return t.stop
}
search(string) {
let keys = Object.keys(this.tasks)
let tasks = []
keys.map((key)=>{
if (string === 'all' || key.indexOf(string) > -1){
tasks.push({
name: key,
task: this.tasks[key]
})
}
})
return tasks
}
getTasksJson() {
return flat.unflatten(this.tasks)
}
}
const config = new Configstore(pkg.name, {tasks:{}})
export default new Manager(config)