Skip to content

Commit 3f49200

Browse files
committed
Add project argument to resume command. Refactor resume method to be more readable. Add sort method to frameCollection
1 parent 4d2dc51 commit 3f49200

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

src/gtt-resume.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ const Cli = require('./include/cli');
77
const Tasks = require('./include/tasks');
88

99
program
10-
.option('--verbose', 'show verbose output')
11-
.parse(process.argv);
10+
.arguments('[project]')
11+
.option('--verbose', 'show verbose output')
12+
.parse(process.argv);
1213

1314
Cli.verbose = program.verbose;
1415

15-
let config = new Config(process.cwd()),
16-
tasks = new Tasks(config);
16+
let config = new Config(process.cwd()).set('project', program.args[0]),
17+
tasks = new Tasks(config);
1718

19+
if (!config.get('project'))
20+
Cli.error('No project set');
1821

1922
tasks.resume()
2023
.then(frame => console.log(`Starting project ${config.get('project').magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue} at ${moment().format('HH:mm').green}`))

src/gtt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ program
99
.command('create [project] [title]', 'start monitoring time for the given project and create a new issue or merge request with the given title')
1010
.command('status', 'shows if time monitoring is running')
1111
.command('stop', 'stop monitoring time')
12-
.command('resume', 'resume monitoring time for last stopped record')
12+
.command('resume [project]', 'resume monitoring time for last stopped record')
1313
.command('cancel', 'cancel and discard active monitoring time')
1414
.command('log', 'log recorded time records')
1515
.command('sync', 'sync local time records to GitLab')

src/include/tasks.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class tasks {
115115
resource.createTime(Math.ceil(time))
116116
.then(() => resource.getNotes())
117117
.then(() => {
118-
if(frame.resource.new) {
118+
if (frame.resource.new) {
119119
delete frame.resource.new;
120120
frame.resource.id = resource.data.iid;
121121
}
@@ -178,22 +178,18 @@ class tasks {
178178
*
179179
* @returns {Promise}
180180
*/
181-
resume() {
181+
resume() {
182182
return new Promise((resolve, reject) => {
183-
let project;
184-
let frames = new FrameCollection(this.config).frames
185-
if (project = this.config.get('project')) {
186-
frames = frames.filter(frame => frame.project == project)
187-
}
188-
const last = frames.filter(frame => frame.stop)
189-
.sort((a, b) => -1 * a.stop.localeCompare(b.stop))[0]
190-
if (last) {
191-
this.start(last.project, last.resource.type, last.resource.id)
192-
.then(frames => resolve(frames))
193-
.catch(error => reject(error))
194-
} else {
195-
reject("No recent entry found for project")
196-
}
183+
let frames = new FrameCollection(this.config);
184+
185+
frames
186+
.filter(frame => frame.project === this.config.get('project'))
187+
.sort((a, b) => moment(a.stop).isBefore(moment(b.stop)) ? 1 : -1);
188+
189+
let last = frames.frames[0];
190+
this.start(last.project, last.resource.type, last.resource.id)
191+
.then(frame => resolve(frame))
192+
.catch(error => reject(error));
197193
});
198194
}
199195

src/models/frameCollection.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class frameCollection extends Base {
1818
.filter(frame => frame);
1919
}
2020

21+
sort(func) {
22+
this.frames.sort(func);
23+
24+
return this;
25+
}
26+
2127
filter(func) {
2228
let arr = [];
2329

@@ -29,9 +35,11 @@ class frameCollection extends Base {
2935
if (func(frame)) {
3036
arr.push(frame);
3137
}
32-
33-
this.frames = arr;
3438
});
39+
40+
this.frames = arr;
41+
42+
return this;
3543
}
3644

3745
forEach(iterator) {

0 commit comments

Comments
 (0)