Skip to content

Commit 7175148

Browse files
committed
Refactor to task class
1 parent 49dea9f commit 7175148

File tree

11 files changed

+307
-174
lines changed

11 files changed

+307
-174
lines changed

gtt-cancel.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@ const program = require('commander');
22
const colors = require('colors');
33
const moment = require('moment');
44

5-
const Frame = require('./models/frame');
65
const Config = require('./include/file-config');
76
const Cli = require('./include/cli');
8-
const Fs = require('./include/filesystem');
7+
const Tasks = require('./include/tasks');
98

10-
program.parse(process.argv);
9+
program
10+
.option('--verbose', 'show verbose output')
11+
.parse(process.argv);
12+
13+
Cli.verbose = program.verbose;
1114

1215
let config = new Config(process.cwd());
16+
let tasks = new Tasks(config);
1317

14-
Fs.find(`"stop": false`, config.frameDir)
18+
tasks.cancel()
1519
.then(frames => {
16-
if (frames.length === 0) Cli.error('No projects started.');
17-
18-
frames.forEach(file => {
19-
let frame = Frame.fromFile(config, file);
20-
Fs.remove(file);
21-
console.log(`Cancel project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue}, started ${moment(frame.start).fromNow().green}`);
22-
});
20+
frames.forEach(frame => console.log(`Cancel project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue}, started ${moment(frame.start).fromNow().green}`))
2321
})
24-
.catch(error => Cli.error('Could not read frames.', error));
22+
.catch(error => Cli.error(error));

gtt-log.js

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,35 @@ const program = require('commander');
33
const colors = require('colors');
44
const moment = require('moment');
55

6-
const Frame = require('./models/frame');
76
const Config = require('./include/file-config');
7+
const Cli = require('./include/cli');
88
const Time = require('./models/time');
9-
const Fs = require('./include/filesystem');
9+
const Tasks = require('./include/tasks');
1010

11-
program.parse(process.argv);
11+
program.option('--verbose', 'show verbose output')
12+
.parse(process.argv);
1213

13-
let config = new Config(__dirname);
14+
Cli.verbose = program.verbose;
1415

15-
let frames = {};
16-
let times = {};
16+
let config = new Config(__dirname),
17+
tasks = new Tasks(config);
1718

1819
function toHumanReadable(input) {
1920
return Time.toHumanReadable(Math.ceil(input), config.get('hoursPerDay'), config.get('timeFormat'));
2021
}
2122

22-
Fs.readDir(config.frameDir).forEach(file => {
23-
let frame = Frame.fromFile(config, Fs.join(config.frameDir, file));
24-
if (frame.stop === false) return;
25-
let date = moment(frame.start).format('YYYY-MM-DD');
26-
27-
if (!frames[date]) frames[date] = [];
28-
if (!times[date]) times[date] = 0;
29-
30-
frames[date].push(frame);
31-
times[date] += Math.ceil(frame.duration);
32-
});
33-
34-
Object.keys(frames).sort().forEach(date => {
35-
if (!frames.hasOwnProperty(date)) return;
36-
37-
console.log(`${moment(date).format('MMMM Do YYYY')} (${toHumanReadable(times[date])})`.green);
38-
frames[date]
39-
.sort((a, b) => moment(a.start).isBefore(moment(b.start)) ? -1 : 1)
40-
.forEach(frame => {
41-
console.log(` ${frame.id} ${moment(frame.start).format('HH:mm').green} to ${moment(frame.stop).format('HH:mm').green}\t${toHumanReadable(frame.duration)}\t\t${frame.project.magenta}\t\t${(frame.resource.type + ' #' + frame.resource.id).blue}`)
42-
});
43-
});
23+
tasks.log()
24+
.then(({frames, times}) => {
25+
Object.keys(frames).sort().forEach(date => {
26+
if (!frames.hasOwnProperty(date)) return;
27+
28+
console.log(`${moment(date).format('MMMM Do YYYY')} (${toHumanReadable(times[date])})`.green);
29+
frames[date]
30+
.sort((a, b) => moment(a.start).isBefore(moment(b.start)) ? -1 : 1)
31+
.forEach(frame => {
32+
console.log(` ${frame.id} ${moment(frame.start).format('HH:mm').green} to ${moment(frame.stop).format('HH:mm').green}\t${toHumanReadable(frame.duration)}\t\t${frame.project.magenta}\t\t${(frame.resource.type + ' #' + frame.resource.id).blue}`)
33+
});
34+
});
35+
}
36+
)
37+
.catch(error => Cli.error(error));

gtt-report.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,7 @@ new Promise(resolve => {
166166
})
167167
.then(() => owner.getProjectsByGroup()
168168
.then(() => {
169-
owner.projects.forEach(project => {
170-
reports.push(new Report(config, project));
171-
});
169+
owner.projects.forEach(project => reports.push(new Report(config, project)));
172170
done();
173171
}))
174172
.catch(e => done(e));

gtt-start.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,30 @@ const colors = require('colors');
22
const moment = require('moment');
33
const program = require('commander');
44

5-
const Frame = require('./models/frame');
65
const Config = require('./include/file-config');
76
const Cli = require('./include/cli');
8-
const Fs = require('./include/filesystem');
7+
const Tasks = require('./include/tasks');
98

109
program
1110
.arguments('[project] [id]')
1211
.option('-t, --type <type>', 'specify resource type: issue, merge_request')
12+
.option('--verbose', 'show verbose output')
1313
.parse(process.argv);
1414

15-
let config = new Config(process.cwd());
15+
Cli.verbose = program.verbose;
1616

17-
if (program.args.length < 2 && !config.get('project')) Cli.error('No project set');
18-
if (program.args.length === 2) config.set('project', program.args[0]);
17+
let config = new Config(process.cwd()),
18+
tasks = new Tasks(config),
19+
type = program.type ? program.type : 'issue',
20+
id = program.args.length === 1 ? parseInt(program.args[0]) : parseInt(program.args[1]),
21+
project = program.args.length === 2 ? project = program.args[0] : null;
1922

20-
let id = program.args.length === 1 ? parseInt(program.args[0]) : parseInt(program.args[1]);
21-
if (!id) Cli.error('Wrong or missing issue/merge_request id');
23+
if (program.args.length < 2 && !config.get('project'))
24+
Cli.error('No project set');
2225

23-
let type = program.type ? program.type : 'issue';
26+
if (!id)
27+
Cli.error('Wrong or missing issue/merge_request id');
2428

25-
Fs.find(`"stop": false`, config.frameDir)
26-
.then(frames => {
27-
if (frames.length > 0) Cli.error("Already running. Please stop it first with 'gtt stop'.");
28-
29-
new Frame(config, id, type).startMe();
30-
console.log(`Starting project ${config.get('project').magenta} ${type.blue} ${('#' + id).blue} at ${moment().format('HH:mm').green}`);
31-
})
32-
.catch(error => Cli.error('Could not write frame.', error));
29+
tasks.start(project, type, id)
30+
.then(frame => console.log(`Starting project ${config.get('project').magenta} ${type.blue} ${('#' + id).blue} at ${moment().format('HH:mm').green}`))
31+
.catch(error => Cli.error(error));

gtt-status.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@ const program = require('commander');
22
const colors = require('colors');
33
const moment = require('moment');
44

5-
const Frame = require('./models/frame');
65
const Config = require('./include/file-config');
76
const Cli = require('./include/cli');
8-
const Fs = require('./include/filesystem');
7+
const Tasks = require('./include/tasks');
98

10-
program.parse(process.argv);
119

12-
let config = new Config(__dirname);
10+
program
11+
.option('--verbose', 'show verbose output')
12+
.parse(process.argv);
1313

14-
Fs.find(`"stop": false`, config.frameDir)
14+
Cli.verbose = program.verbose;
15+
16+
let config = new Config(__dirname),
17+
tasks = new Tasks(config);
18+
19+
tasks.status()
1520
.then(frames => {
1621
if (frames.length === 0) {
1722
console.log('No projects are started right now.');
1823
return;
1924
}
2025

21-
frames.forEach(file => {
22-
let frame = Frame.fromFile(config, file);
23-
console.log(`Project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue} is running, started ${moment(frame.start).fromNow().green} (id: ${frame.id})`);
24-
});
26+
frames.forEach(frame => console.log(`Project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue} is running, started ${moment(frame.start).fromNow().green} (id: ${frame.id})`));
2527
})
2628
.catch(error => Cli.error('Could not read frames.', error));

gtt-stop.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@ const program = require('commander');
22
const colors = require('colors');
33
const moment = require('moment');
44

5-
const Frame = require('./models/frame');
65
const Config = require('./include/file-config');
76
const Cli = require('./include/cli');
8-
const Fs = require('./include/filesystem');
7+
const Tasks = require('./include/tasks');
98

10-
program.parse(process.argv);
9+
program
10+
.option('--verbose', 'show verbose output')
11+
.parse(process.argv);
1112

12-
let config = new Config(__dirname);
13+
Cli.verbose = program.verbose;
1314

14-
Fs.find(`"stop": false`, config.frameDir)
15-
.then(frames => {
16-
if (frames.length === 0) Cli.error('No projects started.');
15+
let config = new Config(__dirname),
16+
tasks = new Tasks(config);
1717

18-
frames.forEach(file => {
19-
let frame = Frame.fromFile(config, file).stopMe();
20-
console.log(`Stopping project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue}, started ${moment(frame.start).fromNow().green} (id: ${frame.id})`);
21-
});
18+
tasks.stop()
19+
.then(frames => {
20+
frames.forEach(frame => console.log(`Stopping project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue}, started ${moment(frame.start).fromNow().green} (id: ${frame.id})`));
2221
})
23-
.catch(error => Cli.error('Could not read frames.', error));
22+
.catch(error => Cli.error(error));

gtt-sync.js

Lines changed: 13 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,25 @@
1-
const _ = require('underscore');
21
const moment = require('moment');
32
const program = require('commander');
43

5-
const FrameCollection = require('./models/frameCollection');
6-
const Issue = require('./models/issue');
7-
const MergeRequest = require('./models/mergeRequest');
84
const Config = require('./include/file-config');
95
const Cli = require('./include/cli');
6+
const Tasks = require('./include/tasks');
107

118
program
129
.option('-p --proxy <proxy>', 'use a proxy server with the given url')
10+
.option('--verbose', 'show verbose output')
1311
.parse(process.argv);
1412

15-
let config = new Config(process.cwd());
16-
let frames = new FrameCollection(config);
13+
Cli.verbose = program.verbose;
1714

18-
config.set('proxy', program.proxy);
15+
let config = new Config(process.cwd()).set('proxy', program.proxy),
16+
tasks = new Tasks(config);
1917

20-
let classes = {
21-
issue: Issue,
22-
merge_request: MergeRequest
23-
};
24-
let resources = {
25-
issue: {},
26-
merge_request: {}
27-
};
28-
29-
function createTime(frame, time) {
30-
return new Promise((resolve, reject) => {
31-
resources[frame.resource.type][frame.resource.id].createTime(Math.ceil(time))
32-
.then(() => resources[frame.resource.type][frame.resource.id].getNotes())
33-
.then(() => {
34-
frame.notes.push({
35-
id: resources[frame.resource.type][frame.resource.id].notes[0].id,
36-
time: Math.ceil(time)
37-
});
38-
frame.write();
39-
resolve();
40-
})
41-
.catch(error => reject(error))
42-
});
43-
}
44-
45-
// filter out frames, that don't need an update
46-
frames.filter(frame => {
47-
return !(Math.ceil(frame.duration) === _.reduce(frame.notes, (n, m) => (n + m.time), 0));
48-
});
49-
50-
if (frames.length === 0) process.exit(0);
51-
52-
Cli.bar(`${Cli.process} Syncing time records...`, frames.length);
53-
54-
frames.forEach((frame, done) => {
55-
if (resources[frame.resource.type][frame.resource.id] !== undefined
56-
&& resources[frame.resource.type][frame.resource.id].data.project_id) return done();
57-
58-
resources[frame.resource.type][frame.resource.id] = new classes[frame.resource.type](config, {});
59-
resources[frame.resource.type][frame.resource.id]
60-
.make(frame.project, frame.resource.id)
61-
.then(() => done())
62-
.catch(error => Cli.x(`Could not resolve issue/merge_request ${frame.resource.id} on "${frame.project}"`, error));
63-
})
64-
65-
.then(() => frames.forEach((frame, done) => {
66-
new Promise(resolve => resolve())
67-
68-
// set notes if not already set
69-
.then(() => new Promise((resolve, reject) => {
70-
let notes;
71-
if ((notes = resources[frame.resource.type][frame.resource.id].notes) && notes.length > 0) return;
72-
resources[frame.resource.type][frame.resource.id]
73-
.getNotes()
74-
.then(() => resolve())
75-
.catch(error => reject(error));
76-
}))
77-
.catch(error => Cli.x(`Could not get notes from "${frame.project}"`, error))
78-
79-
// create note if completely missing
80-
.then(() => new Promise((resolve, reject) => {
81-
if (frame.notes.length > 0) return resolve();
82-
createTime(frame, frame.duration)
83-
.then(() => {
84-
Cli.advance();
85-
done();
86-
})
87-
.catch(error => reject(error))
88-
}))
89-
.catch(error => Cli.error('Could not create time spent.', error))
90-
91-
// check for mismatches and update times
92-
.then(() => new Promise((resolve, reject) => {
93-
let diff = Math.ceil(frame.duration) - parseInt(_.reduce(frame.notes, (n, m) => (n + m.time), 0));
94-
createTime(frame, diff)
95-
.then(() => {
96-
Cli.advance();
97-
done();
98-
})
99-
.catch(error => reject(error))
100-
}))
101-
.catch(error => Cli.error('Could not create time spent.', error))
102-
}));
18+
tasks.syncResolve()
19+
.then(() => {
20+
if (tasks.sync.frames.length === 0) process.exit(0);
21+
return Cli.bar(`${Cli.process} Syncing time records...`, tasks.sync.frames.length)
22+
})
23+
.then(() => tasks.syncNotes())
24+
.then(() => tasks.syncUpdate(Cli.advance))
25+
.catch(error => Cli.x(error));

include/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class cli {
177177
*/
178178
static mark() {
179179
cli.resolve();
180-
cli.out(`${cli.active.message}` + `✓\n`.green);
180+
if (cli.active) cli.out(`${cli.active.message}` + `✓\n`.green);
181181

182182
return cli.promise();
183183
}
@@ -190,7 +190,7 @@ class cli {
190190
*/
191191
static x(message = false, error = false) {
192192
cli.resolve();
193-
cli.out(`${cli.active.message}` + `✗\n`.red);
193+
if (cli.active) cli.out(`${cli.active.message}` + `✗\n`.red);
194194

195195
if (message) cli.error(message, error);
196196
return cli.promise();

0 commit comments

Comments
 (0)