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
306 lines (272 loc) · 8.82 KB
/
Copy pathManager.js
File metadata and controls
306 lines (272 loc) · 8.82 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _flat = require('flat');
var _flat2 = _interopRequireDefault(_flat);
var _moment = require('moment');
var _moment2 = _interopRequireDefault(_moment);
var _inquirer = require('inquirer');
var _inquirer2 = _interopRequireDefault(_inquirer);
var _Task = require('./Task');
var _Task2 = _interopRequireDefault(_Task);
var _utils = require('./utils');
var _constants = require('./constants');
var _output = require('./output');
var _dbMigrations = require('./dbMigrations');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Manager = function () {
function Manager(cfg) {
_classCallCheck(this, Manager);
this.repositories = ['tasks', 'config'];
this.cfg = cfg;
this.tasks = cfg.all.tasks;
this.config = cfg.all.config ? cfg.all.config : {};
}
_createClass(Manager, [{
key: 'getTask',
value: function getTask(key) {
var task = this.tasks[key] ? this.tasks[key] : null;
return new _Task2.default(task);
}
}, {
key: 'getAllTasks',
value: function getAllTasks(props) {
var _this = this;
var myTasks = [];
if (!props) props = {};
Object.keys(this.tasks).forEach(function (key) {
if (props.filter) {
var _st = _this.tasks[key].status;
if (props.filter === 'unfinished') {
if (_st === _constants.FINISHED) return;
} else if (props.filter === 'running') {
if (_st === _constants.FINISHED || _st === _constants.PAUSED) return;
} else if (props.filter === 'paused') {
if (_st !== _constants.PAUSED) return;
}
}
if (props.only_key) {
myTasks.push(key);
return;
}
var _task = { key: key };
if (props.status) _task.status = _this.tasks[key].status;
if (props.lastLog) _task.lastLog = _this.tasks[key].log[_this.tasks[key].log.length - 1];
myTasks.push(_task);
});
return myTasks;
}
}, {
key: 'storeTask',
value: function storeTask(key, task) {
var update = {};
update[key] = task.get();
this.tasks = Object.assign({}, this.tasks, update);
this.cfg.set('tasks', this.tasks);
}
}, {
key: 'startTask',
value: function startTask(key, description) {
var _this2 = this;
var t = this.getTask(key);
t.start(description).then(function () {
_this2.storeTask(key, t);
console.log((0, _output.outputVertical)('Task:', key, _constants.STARTED, (0, _moment2.default)().toISOString()));
}, _output.cliError).catch(_output.cliError);
}
}, {
key: 'pauseTasks',
value: function pauseTasks(key) {
var _this3 = this;
if (key) {
this.pauseTask(key);
return;
}
var running_tasks = this.getAllTasks({ filter: 'running', only_key: true });
running_tasks.forEach(function (el) {
return _this3.pauseTask(el);
});
console.log('Pausing', running_tasks.length, 'Task(s)');
}
}, {
key: 'pauseTask',
value: function pauseTask(key) {
var _this4 = this;
var t = this.getTask(key);
t.pause().then(function () {
_this4.storeTask(key, t);
console.log((0, _output.outputVertical)('Task:', key, _constants.PAUSED, (0, _moment2.default)().toISOString()));
}, _output.cliError).catch(_output.cliError);
}
}, {
key: 'unpauseTasks',
value: function unpauseTasks(key) {
var _this5 = this;
if (key) {
this.unpauseTask(key);
return;
}
var paused_tasks = this.getAllTasks({ filter: 'paused', only_key: true });
paused_tasks.forEach(function (el) {
return _this5.unpauseTask(el);
});
console.log('Unpausing', paused_tasks.length, 'Task(s)');
}
}, {
key: 'unpauseTask',
value: function unpauseTask(key) {
var _this6 = this;
var t = this.getTask(key);
t.unpause().then(function () {
_this6.storeTask(key, t);
console.log((0, _output.outputVertical)('Task:', key, _constants.UNPAUSED, (0, _moment2.default)().toISOString()));
}, _output.cliError).catch(_output.cliError);
}
}, {
key: 'stopTask',
value: function stopTask(key, description) {
var _this7 = this;
var t = this.getTask(key);
t.stop(description).then(function () {
_this7.storeTask(key, t);
console.log((0, _output.outputVertical)('Task:', key, _constants.FINISHED, (0, _moment2.default)().toISOString()));
}, _output.cliError).catch(_output.cliError);
}
}, {
key: 'addDescription',
value: function addDescription(key, text) {
var t = this.getTask(key);
t.setDescription(text);
this.storeTask(key, t);
}
}, {
key: 'getTime',
value: function getTime(name) {
var t = this.getTask(name);
return t.getSeconds();
}
}, {
key: 'modifyTask',
value: function modifyTask(operation, name, stringTime) {
var _this8 = this;
var t = this.getTask(name);
t.makeOperationOverTime(operation, stringTime).then(function () {
_this8.storeTask(name, t);
}, _output.cliError).catch(_output.cliError);
}
}, {
key: 'search',
value: function search(string) {
var _this9 = this;
var keys = Object.keys(this.tasks);
var tasks = [];
keys.forEach(function (key) {
if (string === 'all' || key.indexOf(string) > -1) {
tasks.push({
name: key,
task: new _Task2.default(_this9.tasks[key])
});
}
});
return tasks;
}
}, {
key: 'delete',
value: function _delete(string) {
var _this10 = this;
var tasks = this.search(string);
console.log(tasks.map(function (k) {
return k.name + ' \n';
}).join(''));
if (tasks.length === 0) {
(0, _output.cliSuccess)('No tasks found to delete.');
return;
}
_inquirer2.default.prompt([{
type: 'confirm',
name: 'cls',
message: 'Are you sure you want to delete this tasks?',
default: false
}]).then(function (answers) {
if (answers.cls) {
tasks.forEach(function (k) {
delete _this10.tasks[k.name];
_this10.cfg.set('tasks', _this10.tasks);
});
(0, _output.cliSuccess)('Tasks deleted.');
}
}).catch(_output.cliError);
}
}, {
key: 'getTasksJson',
value: function getTasksJson() {
return _flat2.default.unflatten(this.tasks);
}
}, {
key: 'getConfig',
value: function getConfig() {
return this.config;
}
}, {
key: 'configure',
value: function configure(element, value) {
if (_constants.configElements.indexOf(element) < 0) return (0, _output.cliError)('Config key (' + element + ') not allowed, allowed keys: ' + this.configElements.toString() + ' ');
var newCfg = _defineProperty({}, element, value);
this.config = Object.assign({}, this.config, newCfg);
this.cfg.set('config', this.config);
return this.config;
}
}, {
key: 'update',
value: function update() {
var _this11 = this;
if (!this.config || this.config && this.config['config.version'] !== '2') {
console.log('DB: Need to be updated');
(0, _dbMigrations.migrateToV2)(this.tasks).then(function (tasks) {
var newTasks = {};
tasks.forEach(function (t) {
return newTasks[t.key] = t.task;
});
return newTasks;
}).then(function (migratedTasks) {
_this11.cfg.set('tasks', migratedTasks);
_this11.cfg.set('config', Object.assign(_this11.config, {
'config.version': '2'
}));
(0, _output.cliSuccess)('Configuration migrated to version 2.');
}, _output.cliError).catch(_output.cliError);
} else {
(0, _output.cliSuccess)('No need to update the DB.');
}
}
}, {
key: 'summarize',
value: function summarize(args) {
if (typeof args.full === 'undefined') args.full = true;
(0, _output.summarize)({
search: args.key,
tasks: this.search(args.key),
rate: args.rate,
full: args.full,
timespan: args.timespan,
format: this.config['format.output']
});
}
}, {
key: 'getStatus',
value: function getStatus() {
var keys = this.getAllTasks({ view: 'list', status: true, lastLog: true });
keys.forEach(function (el) {
console.log(el.key, el.status, el.lastLog);
});
}
}]);
return Manager;
}();
exports.default = Manager;
//# sourceMappingURL=Manager.js.map