Skip to content

Commit a7e1652

Browse files
committed
Rework sync method to be more verbose and fix bug with bigger amounts of frames
1 parent 66f409f commit a7e1652

File tree

4 files changed

+76
-41
lines changed

4 files changed

+76
-41
lines changed

gtt-sync.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ Cli.verbose = program.verbose;
1515
let config = new Config(process.cwd()).set('proxy', program.proxy),
1616
tasks = new Tasks(config);
1717

18-
tasks.syncResolve()
18+
tasks.syncInit()
19+
.then(() => tasks.sync.frames.length === 0 ? process.exit(0) : null)
1920
.then(() => {
20-
if (tasks.sync.frames.length === 0) process.exit(0);
21-
return Cli.bar(`${Cli.process} Syncing time records...`, tasks.sync.frames.length)
21+
Cli.bar(`${Cli.fetch} Fetching issues & merge requests...`, tasks.sync.frames.length);
22+
return tasks.syncResolve(Cli.advance);
23+
})
24+
.then(() => {
25+
Cli.bar(`${Cli.process} Processing issues & merge requests...`, tasks.sync.frames.length);
26+
return tasks.syncNotes(Cli.advance);
27+
})
28+
.then(() => {
29+
Cli.bar(`${Cli.update} Syncing time records...`, tasks.sync.frames.length);
30+
return tasks.syncUpdate(Cli.advance)
2231
})
23-
.then(() => tasks.syncNotes())
24-
.then(() => tasks.syncUpdate(Cli.advance))
2532
.catch(error => Cli.x(error));

include/cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class cli {
1818
/*
1919
* emojis
2020
*/
21+
static get update() {
22+
return '⏱';
23+
}
24+
2125
static get print() {
2226
return '🖨';
2327
}

include/tasks.js

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,29 @@ class tasks {
2020
}
2121

2222
/**
23-
* Filter frames that need an update and resolve merge_requests and issues
24-
* respectively.
23+
* Filter frames that need an update
2524
* @returns {Promise}
2625
*/
27-
syncResolve() {
26+
syncInit() {
2827
this.sync.frames = new FrameCollection(this.config);
2928

29+
// filter out frames, that don't need an update
30+
this.sync.frames.filter(frame => !(Math.ceil(frame.duration) === _.reduce(frame.notes, (n, m) => (n + m.time), 0)));
31+
32+
return new Promise(r => r());
33+
}
34+
35+
/**
36+
* Resolve merge_requests and issues
37+
* respectively.
38+
* @returns {Promise}
39+
*/
40+
syncResolve(callback) {
3041
this.sync.resources = {
3142
issue: {},
3243
merge_request: {}
3344
};
3445

35-
// filter out frames, that don't need an update
36-
this.sync.frames.filter(frame => !(Math.ceil(frame.duration) === _.reduce(frame.notes, (n, m) => (n + m.time), 0)));
37-
3846
// resolve issues and merge requests
3947
return this.sync.frames.forEach((frame, done) => {
4048
let project = frame.project,
@@ -48,26 +56,35 @@ class tasks {
4856
this.sync.resources[type][id] = new classes[type](this.config, {});
4957
this.sync.resources[type][id]
5058
.make(project, id)
51-
.then(() => done())
59+
.then(() => {
60+
if (callback) callback();
61+
done();
62+
})
5263
.catch(error => done(`Could not resolve ${type} ${id} on "${project}"`));
5364
})
5465
}
5566

5667
/**
5768
* Get notes for all frames.
5869
*/
59-
syncNotes() {
70+
syncNotes(callback) {
6071
return this.sync.frames.forEach((frame, done) => {
6172
let project = frame.project,
6273
type = frame.resource.type,
6374
id = frame.resource.id,
6475
notes;
6576

66-
if ((notes = this.sync.resources[type][id].notes) && notes.length > 0) return;
77+
if ((notes = this.sync.resources[type][id].notes) && notes.length > 0) {
78+
if (callback) callback();
79+
return done();
80+
}
6781

6882
this.sync.resources[type][id]
6983
.getNotes()
70-
.then(() => done())
84+
.then(() => {
85+
if (callback) callback();
86+
done();
87+
})
7188
.catch(error => done(`Could not get notes from ${type} ${id} on "${project}"`));
7289
});
7390
}
@@ -92,22 +109,24 @@ class tasks {
92109
}
93110

94111
_addTime(frame, time) {
95-
return new Promise((resolve, reject) => {
112+
return new Promise(async function(resolve, reject) {
96113
let resource = this.sync.resources[frame.resource.type][frame.resource.id];
97114

98-
resource
99-
.createTime(Math.ceil(time))
100-
.then(() => resource.getNotes())
101-
.then(() => {
102-
frame.notes.push({
103-
id: resource.notes[0].id,
104-
time: Math.ceil(time)
105-
});
106-
frame.write();
107-
resolve();
108-
})
109-
.catch(error => reject(error))
110-
});
115+
try {
116+
await resource.createTime(Math.ceil(time));
117+
await resource.getNotes();
118+
} catch(error) {
119+
return reject(error);
120+
}
121+
122+
frame.notes.push({
123+
id: resource.notes[0].id,
124+
time: Math.ceil(time)
125+
});
126+
127+
frame.write();
128+
resolve();
129+
}.bind(this));
111130
}
112131

113132
/**

models/frameCollection.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,38 @@ class frameCollection extends Base {
66
constructor(config) {
77
super(config);
88

9-
this.frames = Fs.readDir(config.frameDir);
9+
this.frames =
10+
Fs.readDir(config.frameDir)
11+
.map(file => {
12+
try {
13+
return Frame.fromFile(this.config, Fs.join(this.config.frameDir, file));
14+
} catch (e) {
15+
return false;
16+
}
17+
})
18+
.filter(frame => frame);
1019
}
1120

1221
filter(func) {
1322
let arr = [];
1423

15-
this.frames.forEach(file => {
16-
let frame = Frame.fromFile(this.config, Fs.join(this.config.frameDir, file));
24+
this.frames.forEach(frame => {
1725
if (frame.stop === false) {
18-
return;
26+
return false;
1927
}
2028

2129
if (func(frame)) {
22-
arr.push(file);
30+
arr.push(frame);
2331
}
24-
});
2532

26-
this.frames = arr;
33+
this.frames = arr;
34+
});
2735
}
2836

2937
forEach(iterator) {
30-
return this.parallel(this.frames, (file, done) => {
31-
let frame = Frame.fromFile(this.config, Fs.join(this.config.frameDir, file));
32-
if (frame.stop === false) return done();
38+
let promise = this.parallel(this.frames, iterator);
3339

34-
iterator(frame, done);
35-
});
40+
return promise;
3641
}
3742

3843
get length() {

0 commit comments

Comments
 (0)