Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
finish first nodejs prototype
  • Loading branch information
kriskbx committed May 9, 2017
commit aa264a660b1cc13634b2bc6dbbffbec4baf33517
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.idea/
/node_modules
.gtt.json
.gtt.yml
.DS_STORE
9 changes: 6 additions & 3 deletions gtt-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ const Config = require('./include/file-config');
let config = new Config(__dirname);

program
.command('gtt edit')
.option('-l, --local', 'edit the local configuration file', false)
.option('-l, --local', 'edit the local configuration file')
.parse(process.argv);

open(program.local ? config.local : config.global) ;
if (program.local) {
config.assertLocalConfig();
}

open(program.local ? config.local : config.global);
101 changes: 81 additions & 20 deletions gtt-report.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const _ = require('underscore');
const program = require('commander');

const Cli = require('./include/cli');
const Config = require('./include/file-config');
const Report = require('./model/report');
const Report = require('./models/report');

const Output = {
table: require('./output/table')
};

// this collects options
function collect(val, arr) {
Expand All @@ -21,19 +26,25 @@ program
.option('-u --user <user>', 'only query times from the given user')
.option('-m --milestone <milestone>', 'include issues from the given milestone')
.option('-q --query <query>', 'query the given data types: issues, merge_requests', collect, null)
.option('-r --report <report>', 'include in the report: stats, issues, merge_requests, records', collect, null)
.option('-o --output <output>', 'use the given output', collect, null)
.option('-l --file <file>', 'save report to the given file', collect, null)
.option('--columns <columns>', 'include the given columns in the report', collect, null)
.option('--include_by_labels <labels>', 'only include issues that have the given labels', collect, null)
.option('--exclude_by_labels <labels>', 'exclude issues that have the given labels', collect, null)
.option('--include_labels <labels>', 'only include the given labels in the report', collect, null)
.option('--exclude_labels <labels>', 'exclude the given labels in the report', collect, null)
.option('--date_format <date>', 'use the given date format in the report', collect, null)
.option('--time_format <time>', 'use the given time format in the report', collect, null)
.option('--time_format <time>', 'use the given time format in the report')
.option('--no_headlines', 'hide headlines in the report')
.option('--no_warnings', 'hide warnings in the report')
.option('--record_columns <columns>', 'include the given columns in the record part of the report', collect, null)
.option('--issue_columns <columns>', 'include the given columns in the issue part of the report', collect, null)
.option('--merge_request_columns <columns>', 'include the given columns in the merge request part of the report', collect, null)
.option('--user_columns', 'include user columns in the report')
.parse(process.argv);

// init helpers
let config = new Config(__dirname);
let config = new Config(process.cwd());
let cli = new Cli(program.args);

// overwrite config with args and opts
Expand All @@ -45,69 +56,119 @@ config
.set('closed', program.closed)
.set('user', program.user)
.set('milestone', program.milestone)
.set('columns', program.columns)
.set('includeByLabels', program.include_by_labels)
.set('excludeByLabels', program.exclude_by_labels)
.set('includeLabels', program.include_labels)
.set('excludeLabels', program.exclude_labels)
.set('dateFormat', program.date_format)
.set('timeFormat', program.time_format)
.set('output', program.output)
.set('file', program.file);
.set('file', program.file)
.set('query', program.query)
.set('report', program.report)
.set('recordColumns', program.record_columns)
.set('issueColumns', program.issue_columns)
.set('mergeRequestColumns', program.merge_request_columns)
.set('noHeadlines', program.no_headlines)
.set('noWarnings', program.no_warnings)
.set('userColumns', program.user_columns);

// warnings
if (config.get('iids').length > 1 && config.get('query').length > 1) {
Cli.warn(`The ids argument is ignored when querying multiple data types`);
}
if ((config.get('report').includes('issues') && !config.get('query').includes('issues'))) {
Cli.warn(`Issues are included in the report but not queried.`);
}
if ((config.get('report').includes('merge_requests') && !config.get('query').includes('merge_requests'))) {
Cli.warn(`Merge Requests are included in the report but not queried.`);
}
if (!Output[config.get('output')]) {
Cli.error(`The output ${config.get('output')} doesn't exist`);
}

let report = new Report(config);
let report = new Report(config), output;

// get project
Cli.list(`🔍 Resolving project "${config.get('project')}"`);
Cli.list(`${Cli.look} Resolving project "${config.get('project')}"`);
report
.project()
.then(() => new Promise((resolve, reject) => {
if (!config.get('userColumns')) return resolve();

report.project.members()
.then(() => {
let columns = report.project.users.map(user => `time_${user}`);

config.set('issueColumns', _.uniq(config.get('issueColumns').concat(columns)));
config.set('mergeRequestColumns', _.uniq(config.get('mergeRequestColumns').concat(columns)));
resolve();
})
.catch(error => reject(error));
}))
.then(() => Cli.mark())
.catch(error => Cli.x(`could not fetch project.`, error))

// get issues
.then(() => new Promise((resolve) => {
.then(() => new Promise(resolve => {
if (!config.get('query').includes('issues')) return resolve();
Cli.list(`📦 Fetching issues`);

Cli.list(`${Cli.fetch} Fetching issues`);
report.issues()
.then(() => Cli.mark())
.catch(error => Cli.x(`could not fetch issues.`, error))
.then(() => resolve());
}))

// get merge requests
.then(() => new Promise((resolve) => {
.then(() => new Promise(resolve => {
if (!config.get('query').includes('merge_requests')) return resolve();
Cli.list(`📦 Fetching merge requests`);

Cli.list(`${Cli.fetch} Fetching merge requests`);
report.mergeRequests()
.then(() => Cli.mark())
.catch(error => Cli.x(`could not fetch merge requests.`, error))
.then(() => resolve());
}))

// process issues
.then(() => new Promise((resolve) => {
if (!config.get('query').includes('issues')) return resolve();
Cli.bar(`⚙️ Processing issues`, report.issues.length);
.then(() => new Promise(resolve => {
if (!config.get('query').includes('issues') || report.issues.length === 0) return resolve();

Cli.bar(`${Cli.process}️ Processing issues`, report.issues.length);
report.processIssues(() => Cli.advance())
.then(() => Cli.mark())
.catch(error => Cli.x(`could not process issues.`, error))
.then(() => resolve());
}))

//process merge requests
.then(() => new Promise((resolve) => {
if (!config.get('query').includes('merge_requests')) return resolve();
Cli.bar(`⚙️ Processing merge requests`, report.issues.length);
// process merge requests
.then(() => new Promise(resolve => {
if (!config.get('query').includes('merge_requests') || report.mergeRequests.length === 0) return resolve();

Cli.bar(`${Cli.process}️️ Processing merge requests`, report.mergeRequests.length);
report.processMergeRequests(() => Cli.advance())
.then(() => Cli.mark())
.catch(error => Cli.x(`could not process merge requests.`, error))
.then(() => resolve());
}))

// make report
.then(() => new Promise(resolve => {
Cli.list(`${Cli.output} Making report`);

output = new Output[config.get('output')](config, report);
output.make();
Cli.mark();
resolve();
}))
.catch(error => Cli.x(`could not make report.`, error))

// print report
.then(() => new Promise(resolve => {
output.toStdOut();
resolve();
}))
.catch(error => Cli.x(`could not print report.`, error))

// time for a beer
.then(() => Cli.done());
90 changes: 69 additions & 21 deletions include/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,44 @@ const cursor = require('cli-cursor');
const progress = require('progress');
spinner.set('|/-\\');

/**
* cli helper
*/
class cli {
/**
* pass arguments to cli helper
* @param args
*/
constructor(args) {
this.args = args;
this.data = [];
}

/*
* emojis
*/
static get look() {
return '🔍';
}

static get fetch() {
return '📦';
}

static get process() {
return '⚙';
}

static get output() {
return '📃';
}

static get party() {
return '🥑';
// return '🍺';
}

/**
* print done message
*/
static done() {
console.log();
console.log(`🍺 finished!`.green);
console.log(`\n${cli.party} Finished!`.green);
}

/**
Expand All @@ -37,28 +62,52 @@ class cli {
static bar(message, total) {
cli.resolve(false);

let options = {
total,
clear: true,
width: 40,
renderThrottle: 100
};

this.active = {
started: new Date(),
message: `\r${message}... `.bold.grey,
bar: new progress(`${message} (:current/:total) [:bar] :percent - :etas left`, options)
bar: new progress(`${message} (:current/:total) [:bar] :percent - :minutesm left`, {
total,
clear: true,
width: 40,
renderThrottle: 100
}),
interval: setInterval(() => {
if (!this.active.bar || this.active.bar.complete) return clearInterval(this.active.interval);
this.tick(0);
}, 1000)
};

this.active.bar.tick(0);
this.tick();
return cli.promise();
}

/**
* bar tick
* @param amount
*/
static tick(amount = 0) {
if (!this.active.bar || !this.active.started) return;

let left;

if(this.active.bar.curr > 0) {
let elapsed = Math.ceil((new Date() - this.active.started) / 1000);
left = ((elapsed / this.active.bar.curr) * (this.active.bar.total - this.active.bar.curr)) / 60;
left = left < 1 ? `<1` : Math.ceil(left);
} else {
left = 0;
}

this.active.bar.tick(amount, {
minutes: left
});
}

/**
* advance an existing bar
*/
static advance() {
if (!this.active.bar) return;
this.active.bar.tick(1);
this.tick(1);
}

/**
Expand All @@ -82,9 +131,9 @@ class cli {
* @returns {*}
*/
static mark() {
cli.resolve();
process.stdout.write(`${this.active.message}` + `✓\n`.green);

cli.resolve();
return cli.promise();
}

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

cli.resolve();
if (message) cli.error(message, error);
return cli.promise();
}
Expand All @@ -108,7 +157,6 @@ class cli {
static resolve(show = true) {
cursor.toggle(show);
if (this.active && this.active.interval) clearInterval(this.active.interval);
if (this.active) this.active = false;
}

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

return cli.promise();
process.exit(1);
}

/**
Expand Down
Loading