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
196 lines (167 loc) · 5.27 KB
/
Copy pathManager.js
File metadata and controls
196 lines (167 loc) · 5.27 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import flat from 'flat'
import moment from 'moment'
import inquirer from 'inquirer'
import Task from './Task'
import { recognizeModifierTiming } from './utils'
import { STARTED, PAUSED, UNPAUSED, IN_PROGRESS, FINISHED, configElements } from './constants'
import { sumarize, outputVertical, cliError, cliSuccess } from './output'
import { migrateToV2 } from './dbMigrations'
export default class Manager {
repositories = ['tasks', 'config']
constructor(cfg) {
this.cfg = cfg
this.tasks = cfg.all.tasks
this.config = (cfg.all.config) ? cfg.all.config : {}
}
getTask(key) {
let task = (this.tasks[key]) ? this.tasks[key] : null
return new Task(task)
}
storeTask(key, task){
let update = {}
update[key] = task.get()
this.tasks = Object.assign({}, this.tasks, update)
this.cfg.set('tasks', this.tasks)
}
startTask(key, description) {
let t = this.getTask(key)
t.start(description)
.then(() => {
this.storeTask(key, t)
console.log(
outputVertical('Task:', key, STARTED, moment().toISOString())
)
}, cliError)
.catch(cliError)
}
pauseTask(key){
let t = this.getTask(key)
t.pause()
.then(()=>{
this.storeTask(key, t)
console.log(
outputVertical('Task:', key, PAUSED, moment().toISOString())
)
}, cliError)
.catch(cliError)
}
unpauseTask(key){
let t = this.getTask(key)
t.unpause()
.then(()=>{
this.storeTask(key, t)
console.log(
outputVertical('Task:', key, UNPAUSED, moment().toISOString())
)
}, cliError)
.catch(cliError)
}
stopTask(key, description) {
let t = this.getTask(key)
t.stop(description)
.then(()=>{
this.storeTask(key, t)
console.log(
outputVertical('Task:', key, FINISHED, moment().toISOString())
)
}, cliError)
.catch(cliError)
}
addDescription(key, text){
let t = this.getTask(key)
t.setDescription(text)
this.storeTask(key, t)
}
getTime(name) {
let t = this.getTask(name)
return t.getSeconds()
}
modifyTask(operation, name, stringTime){
let t = this.getTask(name)
t.makeOperationOverTime(operation, stringTime)
.then(()=>{
this.storeTask(name, t)
}, cliError)
.catch(cliError)
}
search(string) {
let keys = Object.keys(this.tasks)
let tasks = []
keys.forEach((key)=>{
if (string === 'all' || key.indexOf(string) > -1){
tasks.push({
name: key,
task: new Task(this.tasks[key])
})
}
})
return tasks
}
delete(string) {
let tasks = this.search(string)
console.log(
tasks.map(k => `${k.name} \n`).join('')
)
if (tasks.length === 0) {
cliSuccess('No tasks found to delete.')
return
}
inquirer.prompt([{
type: 'confirm',
name: 'cls',
message: `Are you sure you want to delete this tasks?`,
default: false
}]).then((answers) => {
if (answers.cls){
tasks.forEach(k => {
delete this.tasks[k.name]
this.cfg.set('tasks', this.tasks)
})
cliSuccess('Tasks deleted.')
}
})
.catch(cliError)
}
getTasksJson() {
return flat.unflatten(this.tasks)
}
getConfig(){
return this.config
}
configure(element, value){
if (configElements.indexOf(element) < 0){
return cliError(`Config key (${element}) not allowed, allowed keys: ${this.configElements.toString()} `)
}
let newCfg = {
[element]: value
}
this.config = Object.assign({}, this.config, newCfg)
this.cfg.set('config', this.config)
return this.config
}
update() {
if (!this.config || (this.config && this.config['config.version'] !== '2') ){
console.log('DB: Need to be updated')
migrateToV2(this.tasks)
.then(tasks => {
let newTasks = {}
tasks.forEach(t => newTasks[t.key] = t.task)
return newTasks
})
.then(migratedTasks => {
this.cfg.set('tasks', migratedTasks)
this.cfg.set('config', Object.assign(this.config, {
'config.version': '2'
}))
cliSuccess('Configuration migrated to version 2.')
}, cliError)
.catch(cliError)
} else {
cliSuccess('No need to update the DB.')
}
}
sumarize(key, rate, full=true){
let tasks = this.search(key)
sumarize(key, tasks, rate, full, this.config['format.output'])
}
}