Skip to content

Commit aa264a6

Browse files
committed
finish first nodejs prototype
1 parent 29101b6 commit aa264a6

File tree

21 files changed

+1027
-212
lines changed

21 files changed

+1027
-212
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/.idea/
22
/node_modules
3-
.gtt.json
3+
.gtt.yml
44
.DS_STORE

gtt-edit.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ const Config = require('./include/file-config');
66
let config = new Config(__dirname);
77

88
program
9-
.command('gtt edit')
10-
.option('-l, --local', 'edit the local configuration file', false)
9+
.option('-l, --local', 'edit the local configuration file')
1110
.parse(process.argv);
1211

13-
open(program.local ? config.local : config.global) ;
12+
if (program.local) {
13+
config.assertLocalConfig();
14+
}
15+
16+
open(program.local ? config.local : config.global);

gtt-report.js

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
const _ = require('underscore');
12
const program = require('commander');
23

34
const Cli = require('./include/cli');
45
const Config = require('./include/file-config');
5-
const Report = require('./model/report');
6+
const Report = require('./models/report');
7+
8+
const Output = {
9+
table: require('./output/table')
10+
};
611

712
// this collects options
813
function collect(val, arr) {
@@ -21,19 +26,25 @@ program
2126
.option('-u --user <user>', 'only query times from the given user')
2227
.option('-m --milestone <milestone>', 'include issues from the given milestone')
2328
.option('-q --query <query>', 'query the given data types: issues, merge_requests', collect, null)
29+
.option('-r --report <report>', 'include in the report: stats, issues, merge_requests, records', collect, null)
2430
.option('-o --output <output>', 'use the given output', collect, null)
2531
.option('-l --file <file>', 'save report to the given file', collect, null)
26-
.option('--columns <columns>', 'include the given columns in the report', collect, null)
2732
.option('--include_by_labels <labels>', 'only include issues that have the given labels', collect, null)
2833
.option('--exclude_by_labels <labels>', 'exclude issues that have the given labels', collect, null)
2934
.option('--include_labels <labels>', 'only include the given labels in the report', collect, null)
3035
.option('--exclude_labels <labels>', 'exclude the given labels in the report', collect, null)
3136
.option('--date_format <date>', 'use the given date format in the report', collect, null)
32-
.option('--time_format <time>', 'use the given time format in the report', collect, null)
37+
.option('--time_format <time>', 'use the given time format in the report')
38+
.option('--no_headlines', 'hide headlines in the report')
39+
.option('--no_warnings', 'hide warnings in the report')
40+
.option('--record_columns <columns>', 'include the given columns in the record part of the report', collect, null)
41+
.option('--issue_columns <columns>', 'include the given columns in the issue part of the report', collect, null)
42+
.option('--merge_request_columns <columns>', 'include the given columns in the merge request part of the report', collect, null)
43+
.option('--user_columns', 'include user columns in the report')
3344
.parse(process.argv);
3445

3546
// init helpers
36-
let config = new Config(__dirname);
47+
let config = new Config(process.cwd());
3748
let cli = new Cli(program.args);
3849

3950
// overwrite config with args and opts
@@ -45,69 +56,119 @@ config
4556
.set('closed', program.closed)
4657
.set('user', program.user)
4758
.set('milestone', program.milestone)
48-
.set('columns', program.columns)
4959
.set('includeByLabels', program.include_by_labels)
5060
.set('excludeByLabels', program.exclude_by_labels)
5161
.set('includeLabels', program.include_labels)
5262
.set('excludeLabels', program.exclude_labels)
5363
.set('dateFormat', program.date_format)
5464
.set('timeFormat', program.time_format)
5565
.set('output', program.output)
56-
.set('file', program.file);
66+
.set('file', program.file)
67+
.set('query', program.query)
68+
.set('report', program.report)
69+
.set('recordColumns', program.record_columns)
70+
.set('issueColumns', program.issue_columns)
71+
.set('mergeRequestColumns', program.merge_request_columns)
72+
.set('noHeadlines', program.no_headlines)
73+
.set('noWarnings', program.no_warnings)
74+
.set('userColumns', program.user_columns);
5775

5876
// warnings
5977
if (config.get('iids').length > 1 && config.get('query').length > 1) {
6078
Cli.warn(`The ids argument is ignored when querying multiple data types`);
6179
}
80+
if ((config.get('report').includes('issues') && !config.get('query').includes('issues'))) {
81+
Cli.warn(`Issues are included in the report but not queried.`);
82+
}
83+
if ((config.get('report').includes('merge_requests') && !config.get('query').includes('merge_requests'))) {
84+
Cli.warn(`Merge Requests are included in the report but not queried.`);
85+
}
86+
if (!Output[config.get('output')]) {
87+
Cli.error(`The output ${config.get('output')} doesn't exist`);
88+
}
6289

63-
let report = new Report(config);
90+
let report = new Report(config), output;
6491

65-
// get project
66-
Cli.list(`🔍 Resolving project "${config.get('project')}"`);
92+
Cli.list(`${Cli.look} Resolving project "${config.get('project')}"`);
6793
report
6894
.project()
95+
.then(() => new Promise((resolve, reject) => {
96+
if (!config.get('userColumns')) return resolve();
97+
98+
report.project.members()
99+
.then(() => {
100+
let columns = report.project.users.map(user => `time_${user}`);
101+
102+
config.set('issueColumns', _.uniq(config.get('issueColumns').concat(columns)));
103+
config.set('mergeRequestColumns', _.uniq(config.get('mergeRequestColumns').concat(columns)));
104+
resolve();
105+
})
106+
.catch(error => reject(error));
107+
}))
69108
.then(() => Cli.mark())
70109
.catch(error => Cli.x(`could not fetch project.`, error))
71110

72111
// get issues
73-
.then(() => new Promise((resolve) => {
112+
.then(() => new Promise(resolve => {
74113
if (!config.get('query').includes('issues')) return resolve();
75-
Cli.list(`📦 Fetching issues`);
114+
115+
Cli.list(`${Cli.fetch} Fetching issues`);
76116
report.issues()
77117
.then(() => Cli.mark())
78118
.catch(error => Cli.x(`could not fetch issues.`, error))
79119
.then(() => resolve());
80120
}))
81121

82122
// get merge requests
83-
.then(() => new Promise((resolve) => {
123+
.then(() => new Promise(resolve => {
84124
if (!config.get('query').includes('merge_requests')) return resolve();
85-
Cli.list(`📦 Fetching merge requests`);
125+
126+
Cli.list(`${Cli.fetch} Fetching merge requests`);
86127
report.mergeRequests()
87128
.then(() => Cli.mark())
88129
.catch(error => Cli.x(`could not fetch merge requests.`, error))
89130
.then(() => resolve());
90131
}))
91132

92133
// process issues
93-
.then(() => new Promise((resolve) => {
94-
if (!config.get('query').includes('issues')) return resolve();
95-
Cli.bar(`⚙️ Processing issues`, report.issues.length);
134+
.then(() => new Promise(resolve => {
135+
if (!config.get('query').includes('issues') || report.issues.length === 0) return resolve();
136+
137+
Cli.bar(`${Cli.process}️ Processing issues`, report.issues.length);
96138
report.processIssues(() => Cli.advance())
97139
.then(() => Cli.mark())
98140
.catch(error => Cli.x(`could not process issues.`, error))
99141
.then(() => resolve());
100142
}))
101143

102-
//process merge requests
103-
.then(() => new Promise((resolve) => {
104-
if (!config.get('query').includes('merge_requests')) return resolve();
105-
Cli.bar(`⚙️ Processing merge requests`, report.issues.length);
144+
// process merge requests
145+
.then(() => new Promise(resolve => {
146+
if (!config.get('query').includes('merge_requests') || report.mergeRequests.length === 0) return resolve();
147+
148+
Cli.bar(`${Cli.process}️️ Processing merge requests`, report.mergeRequests.length);
106149
report.processMergeRequests(() => Cli.advance())
107150
.then(() => Cli.mark())
108151
.catch(error => Cli.x(`could not process merge requests.`, error))
109152
.then(() => resolve());
110153
}))
111154

155+
// make report
156+
.then(() => new Promise(resolve => {
157+
Cli.list(`${Cli.output} Making report`);
158+
159+
output = new Output[config.get('output')](config, report);
160+
output.make();
161+
Cli.mark();
162+
resolve();
163+
}))
164+
.catch(error => Cli.x(`could not make report.`, error))
165+
166+
// print report
167+
.then(() => new Promise(resolve => {
168+
output.toStdOut();
169+
resolve();
170+
}))
171+
.catch(error => Cli.x(`could not print report.`, error))
172+
112173
// time for a beer
113174
.then(() => Cli.done());

include/cli.js

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,44 @@ const cursor = require('cli-cursor');
55
const progress = require('progress');
66
spinner.set('|/-\\');
77

8+
/**
9+
* cli helper
10+
*/
811
class cli {
9-
/**
10-
* pass arguments to cli helper
11-
* @param args
12-
*/
1312
constructor(args) {
1413
this.args = args;
1514
this.data = [];
1615
}
1716

17+
/*
18+
* emojis
19+
*/
20+
static get look() {
21+
return '🔍';
22+
}
23+
24+
static get fetch() {
25+
return '📦';
26+
}
27+
28+
static get process() {
29+
return '⚙';
30+
}
31+
32+
static get output() {
33+
return '📃';
34+
}
35+
36+
static get party() {
37+
return '🥑';
38+
// return '🍺';
39+
}
40+
41+
/**
42+
* print done message
43+
*/
1844
static done() {
19-
console.log();
20-
console.log(`🍺 finished!`.green);
45+
console.log(`\n${cli.party} Finished!`.green);
2146
}
2247

2348
/**
@@ -37,28 +62,52 @@ class cli {
3762
static bar(message, total) {
3863
cli.resolve(false);
3964

40-
let options = {
41-
total,
42-
clear: true,
43-
width: 40,
44-
renderThrottle: 100
45-
};
46-
4765
this.active = {
66+
started: new Date(),
4867
message: `\r${message}... `.bold.grey,
49-
bar: new progress(`${message} (:current/:total) [:bar] :percent - :etas left`, options)
68+
bar: new progress(`${message} (:current/:total) [:bar] :percent - :minutesm left`, {
69+
total,
70+
clear: true,
71+
width: 40,
72+
renderThrottle: 100
73+
}),
74+
interval: setInterval(() => {
75+
if (!this.active.bar || this.active.bar.complete) return clearInterval(this.active.interval);
76+
this.tick(0);
77+
}, 1000)
5078
};
5179

52-
this.active.bar.tick(0);
80+
this.tick();
5381
return cli.promise();
5482
}
5583

84+
/**
85+
* bar tick
86+
* @param amount
87+
*/
88+
static tick(amount = 0) {
89+
if (!this.active.bar || !this.active.started) return;
90+
91+
let left;
92+
93+
if(this.active.bar.curr > 0) {
94+
let elapsed = Math.ceil((new Date() - this.active.started) / 1000);
95+
left = ((elapsed / this.active.bar.curr) * (this.active.bar.total - this.active.bar.curr)) / 60;
96+
left = left < 1 ? `<1` : Math.ceil(left);
97+
} else {
98+
left = 0;
99+
}
100+
101+
this.active.bar.tick(amount, {
102+
minutes: left
103+
});
104+
}
105+
56106
/**
57107
* advance an existing bar
58108
*/
59109
static advance() {
60-
if (!this.active.bar) return;
61-
this.active.bar.tick(1);
110+
this.tick(1);
62111
}
63112

64113
/**
@@ -82,9 +131,9 @@ class cli {
82131
* @returns {*}
83132
*/
84133
static mark() {
134+
cli.resolve();
85135
process.stdout.write(`${this.active.message}` + `✓\n`.green);
86136

87-
cli.resolve();
88137
return cli.promise();
89138
}
90139

@@ -95,9 +144,9 @@ class cli {
95144
* @returns {*}
96145
*/
97146
static x(message = false, error = false) {
147+
cli.resolve();
98148
process.stdout.write(`${this.active.message}` + `✗\n`.red);
99149

100-
cli.resolve();
101150
if (message) cli.error(message, error);
102151
return cli.promise();
103152
}
@@ -108,7 +157,6 @@ class cli {
108157
static resolve(show = true) {
109158
cursor.toggle(show);
110159
if (this.active && this.active.interval) clearInterval(this.active.interval);
111-
if (this.active) this.active = false;
112160
}
113161

114162
/**
@@ -123,7 +171,7 @@ class cli {
123171
console.log(` Error: ${message} `.bgRed.white);
124172
if (error) console.log(error);
125173

126-
return cli.promise();
174+
process.exit(1);
127175
}
128176

129177
/**

0 commit comments

Comments
 (0)