Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 46 additions & 0 deletions src/core/args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Args helper
*/
class Args {
constructor(args) {
this.args = args;
this.data = [];
}

/**
* parse the args and return the project argument
* @returns {*}
*/
project() {
if (!this.args[0] && !this.data.project) return null;

if (this.data.project) return this.data.project;

let projects = [...new Set(this.args.filter(arg => isNaN(new Number(arg))))];
this.args = this.args.filter(arg => !projects.includes(arg));

if(projects.length == 0)
return null;

return this.data.project = projects;
}

/**
* parse the args and return an array of issues
* @returns {*}
*/
iids() {
if (this.data.iids) return this.data.iids;

this.data.iids = [...new Set(this.args.map((issue) => {
if (issue.indexOf(',') === -1) return issue;
return issue.split(',');
}).flat())];

if (this.data.iids.length === 0) return null;

return this.data.iids;
}
}

export default Args;
40 changes: 0 additions & 40 deletions src/core/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ const spinner = { next: () => spinnerFrames[spinnerIndex++ % spinnerFrames.lengt
* Cli helper
*/
class Cli {
constructor(args) {
this.args = args;
this.data = [];
}

/*
* emojis
*/
Expand Down Expand Up @@ -223,41 +218,6 @@ class Cli {
resolve();
});
}

/**
* parse the args and return the project argument
* @returns {*}
*/
project() {
if (!this.args[0] && !this.data.project) return null;

if (this.data.project) return this.data.project;

let projects = [...new Set(this.args.filter(arg => isNaN(new Number(arg))))];
this.args = this.args.filter(arg => !projects.includes(arg));

if(projects.length == 0)
return null;

return this.data.project = projects;
}

/**
* parse the args and return an array of issues
* @returns {*}
*/
iids() {
if (this.data.iids) return this.data.iids;

this.data.iids = [...new Set(this.args.map((issue) => {
if (issue.indexOf(',') === -1) return issue;
return issue.split(',');
}).flat())];

if (this.data.iids.length === 0) return null;

return this.data.iids;
}
}

export default Cli;
10 changes: 4 additions & 6 deletions src/core/file-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { load as yamlLoad } from 'js-yaml';
import hash from 'hash-sum';
import Fs from './filesystem.js';
import envPaths from 'env-paths';
import Cli from './cli.js';

const readYaml = file => yamlLoad(fs.readFileSync(file, 'utf8'));

Expand Down Expand Up @@ -40,8 +41,7 @@ class FileConfig extends Config {
try {
return readYaml(this.global);
} catch (e) {
console.log(`Error parsing configuration: "${this.global}"`);
process.exit(1);
Cli.error(`Error parsing configuration: "${this.global}"`, e);
}
}

Expand All @@ -60,15 +60,13 @@ class FileConfig extends Config {
try {
local = Object.assign(readYaml(local.extend), local);
} catch (e) {
console.log(`Error parsing configuration: "${local.extend}"`);
process.exit(1);
Cli.error(`Error parsing configuration: "${local.extend}"`, e);
}
}

return local;
} catch (e) {
console.log(`Error parsing configuration: "${this.local}"`);
process.exit(1);
Cli.error(`Error parsing configuration: "${this.local}"`, e);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/reporting/commands/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import pc from 'picocolors';
import {Command} from 'commander';
import dayjs from '../../core/dayjs.js';
import Cli from '../../core/cli.js';
import Args from '../../core/args.js';
import Config from '../../core/file-config.js';
import Report from '../api/report.js';
import Owner from '../../core/owner.js';
Expand Down Expand Up @@ -81,14 +82,14 @@ function report() {

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

// overwrite config with args and opts
config
.set('url', program.opts().url)
.set('token', program.opts().token)
.set('project', cli.project())
.set('iids', cli.iids())
.set('project', args.project())
.set('iids', args.iids())
.set('from', program.opts().from)
.set('to', program.opts().to)
.set('closed', program.opts().closed)
Expand Down
2 changes: 1 addition & 1 deletion src/reporting/output/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Output {
* render to stdout
*/
toStdOut() {
console.log(this.out);
process.stdout.write(`${this.out}\n`);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/timekeeping/commands/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ timekeeper.archiveInit()
let years = year ? Object.keys(grouped).filter(y => y === year) : Object.keys(grouped);

if (years.length === 0) {
console.log('Nothing to archive.');
Cli.out('Nothing to archive.\n');
return;
}

Expand All @@ -42,7 +42,7 @@ timekeeper.archiveInit()
zip.write(file);
frames.forEach(frame => Fs.remove(frame.file));

console.log(`Archived ${frames.length} record(s) into ${file}`);
Cli.out(`Archived ${frames.length} record(s) into ${file}\n`);
});
})
.catch(error => Cli.error(error));
Expand Down
4 changes: 2 additions & 2 deletions src/timekeeping/commands/cancel.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ timekeeper.cancel()
.then(frames => {
frames.forEach(frame => {
if(!frame.resource.new)
return console.log(`Cancel project ${pc.magenta(frame.project)} ${pc.blue(frame.resource.type)} ${pc.blue(('#' + frame.resource.id))}, started ${pc.green(dayjs(frame.start).fromNow())}`)
return Cli.out(`Cancel project ${pc.magenta(frame.project)} ${pc.blue(frame.resource.type)} ${pc.blue(('#' + frame.resource.id))}, started ${pc.green(dayjs(frame.start).fromNow())}\n`)

console.log(`Cancel project ${pc.magenta(frame.project)} for new ${frame.resource.type} "${pc.blue((frame.resource.id))}", started ${pc.green(dayjs(frame.start).fromNow())}`)
Cli.out(`Cancel project ${pc.magenta(frame.project)} for new ${frame.resource.type} "${pc.blue((frame.resource.id))}", started ${pc.green(dayjs(frame.start).fromNow())}\n`)
})
})
.catch(error => Cli.error(error));
Expand Down
2 changes: 1 addition & 1 deletion src/timekeeping/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if (!title)
Cli.error('Wrong or missing title');

timekeeper.start(project, type, title)
.then(frame => console.log(`Starting project ${pc.magenta(config.get('project'))} and create ${type} "${pc.blue(title)}" at ${pc.green(dayjs().format('HH:mm'))}`))
.then(frame => Cli.out(`Starting project ${pc.magenta(config.get('project'))} and create ${type} "${pc.blue(title)}" at ${pc.green(dayjs().format('HH:mm'))}\n`))
.catch(error => Cli.error(error));
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/timekeeping/commands/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function delCmd() {
} else {
let frame = Frame.fromFile(config, file).stopMe();
Fs.remove(file);
console.log(`Deleting record ${pc.magenta(frame.id)}`);
Cli.out(`Deleting record ${pc.magenta(frame.id)}\n`);
}
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/timekeeping/commands/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ timekeeper.list(project, type, program.opts().closed ? 'closed' : 'opened', prog
style : {compact : true, 'padding-left' : 1}
});
if (tasks.length == 0) {
console.log("No issues or merge requests found.");
cli.out("No issues or merge requests found.\n");
}
tasks.forEach(issue => {
table.push([pc.magenta(issue.iid.toString()), pc.green(issue.title) + "\n" + pc.gray(issue.data.web_url), issue.state])
})
console.log(table.toString());
cli.out(`${table.toString()}\n`);
})
.catch(error => cli.error(error));

Expand Down
10 changes: 5 additions & 5 deletions src/timekeeping/commands/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ function column(str, n){
}

const logCSV = (frames, times) => {
console.log("frameId, project, issueid, date, starttime, endtime, duration (s), title, note");
Cli.out("frameId, project, issueid, date, starttime, endtime, duration (s), title, note\n");
Object.keys(frames).sort().forEach(date => {
if (!frames.hasOwnProperty(date)) return;
frames[date].sort((a, b) => a.start.isBefore(b.start) ? -1 : 1)
.forEach(frame => {
console.log(`${frame.id}, ${frame.project}, ${frame.resource.id}, ${dayjs(date).format('YYYY-MM-DD')}, ${frame.start.clone().format('HH:mm')}, ${frame.stop.clone().format('HH:mm')}, ${frame.duration}, ${frame.title!=null?frame.title:''}, ${frame.note!=null?frame.note:''}`);
Cli.out(`${frame.id}, ${frame.project}, ${frame.resource.id}, ${dayjs(date).format('YYYY-MM-DD')}, ${frame.start.clone().format('HH:mm')}, ${frame.stop.clone().format('HH:mm')}, ${frame.duration}, ${frame.title!=null?frame.title:''}, ${frame.note!=null?frame.note:''}\n`);
});
});
};
Expand All @@ -61,7 +61,7 @@ const logCli = (frames, times) => {
}


console.log(pc.green(`${dayjs(date).format('MMMM Do YYYY')} (${toHumanReadable(times[date])})`) + dayNote);
Cli.out(pc.green(`${dayjs(date).format('MMMM Do YYYY')} (${toHumanReadable(times[date])})`) + dayNote + '\n');
frames[date]
.sort((a, b) => a.start.isBefore(b.start) ? -1 : 1)
.forEach(frame => {
Expand All @@ -70,8 +70,8 @@ const logCli = (frames, times) => {
let issue = frame.resource.new ?
column(`(new ${frame.resource.type + ' "' + frame.resource.id}")`, 70).bgBlue:
`${pc.blue((frame.resource.type + ' #' + frame.resource.id).padEnd(20))}${column(frame.title!=null?frame.title:'', 50)}`;
console.log(` ${frame.id} ${pc.green(frame.start.clone().format('HH:mm'))} to ${pc.green(frame.stop.clone().format('HH:mm'))}\t${durationText}`+
`${pc.magenta(column(frame.project, 50))}${issue}${frame.note!=null?frame.note:''}`);
Cli.out(` ${frame.id} ${pc.green(frame.start.clone().format('HH:mm'))} to ${pc.green(frame.stop.clone().format('HH:mm'))}\t${durationText}`+
`${pc.magenta(column(frame.project, 50))}${issue}${frame.note!=null?frame.note:''}\n`);
});
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/timekeeping/commands/resume.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function column(str, n) {

function resumeFrame(timekeeper, frame) {
timekeeper.resume(frame)
.then(frame => console.log(`Starting project ${pc.magenta(frame.project)} ${pc.blue(frame.resource.type)} ${pc.blue(('#' + frame.resource.id))} ${frame.note?frame.note:''} at ${pc.green(dayjs().format('HH:mm'))}`))
.then(frame => Cli.out(`Starting project ${pc.magenta(frame.project)} ${pc.blue(frame.resource.type)} ${pc.blue(('#' + frame.resource.id))} ${frame.note?frame.note:''} at ${pc.green(dayjs().format('HH:mm'))}\n`))
.catch(error => Cli.error(error));
}

Expand Down
2 changes: 1 addition & 1 deletion src/timekeeping/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if (!id)
Cli.error('Wrong or missing issue/merge_request id');

timekeeper.start(project, type, id, note)
.then(frame => console.log(`Starting project ${pc.magenta(config.get('project'))} ${pc.blue(type)} ${pc.blue(('#' + id))} at ${pc.green(dayjs().format('HH:mm'))}`))
.then(frame => Cli.out(`Starting project ${pc.magenta(config.get('project'))} ${pc.blue(type)} ${pc.blue(('#' + id))} at ${pc.green(dayjs().format('HH:mm'))}\n`))
.catch(error => Cli.error(error));
}
);
Expand Down
8 changes: 4 additions & 4 deletions src/timekeeping/commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ timekeeper.status()
.then(frames => {
if (frames.length === 0) {
if (program.opts().s) {
console.log('gtt idle ');
Cli.out('gtt idle \n');
}else {
console.log('No projects are started right now.');
Cli.out('No projects are started right now.\n');
}
return;
}
if (program.opts().s) {
frames.forEach(frame => console.log(`${pc.magenta(frame.project)} ${pc.blue(frame.resource.type)} ${pc.blue(('#' + frame.resource.id))} ${pc.green(dayjs(frame.start).fromNow())} (id: ${frame.id})`));
frames.forEach(frame => Cli.out(`${pc.magenta(frame.project)} ${pc.blue(frame.resource.type)} ${pc.blue(('#' + frame.resource.id))} ${pc.green(dayjs(frame.start).fromNow())} (id: ${frame.id})\n`));
} else {
frames.forEach(frame => console.log(`Project ${pc.magenta(frame.project)} ${pc.blue(frame.resource.type)} ${pc.blue(('#' + frame.resource.id))} ${frame.note?frame.note:''} is running, started ${pc.green(dayjs(frame.start).fromNow())} (id: ${frame.id})`));
frames.forEach(frame => Cli.out(`Project ${pc.magenta(frame.project)} ${pc.blue(frame.resource.type)} ${pc.blue(('#' + frame.resource.id))} ${frame.note?frame.note:''} is running, started ${pc.green(dayjs(frame.start).fromNow())} (id: ${frame.id})\n`));
}
})
.catch(error => Cli.error('Could not read frames.', error));
Expand Down
4 changes: 2 additions & 2 deletions src/timekeeping/commands/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ timekeeper.stop()
.then(frames => {
frames.forEach(frame => {
if(!frame.resource.new)
return console.log(`Stopping project ${pc.magenta(frame.project)} ${pc.blue(frame.resource.type)} ${pc.blue(('#' + frame.resource.id))}, started ${pc.green(dayjs(frame.start).fromNow())} (id: ${frame.id})`)
return Cli.out(`Stopping project ${pc.magenta(frame.project)} ${pc.blue(frame.resource.type)} ${pc.blue(('#' + frame.resource.id))}, started ${pc.green(dayjs(frame.start).fromNow())} (id: ${frame.id})\n`)

console.log(`Stopping project ${pc.magenta(frame.project)} for new ${frame.resource.type} "${pc.blue((frame.resource.id))}", started ${pc.green(dayjs(frame.start).fromNow())} (id: ${frame.id})`)
Cli.out(`Stopping project ${pc.magenta(frame.project)} for new ${frame.resource.type} "${pc.blue((frame.resource.id))}", started ${pc.green(dayjs(frame.start).fromNow())} (id: ${frame.id})\n`)
});
})
.catch(error => Cli.error(error));
Expand Down
3 changes: 1 addition & 2 deletions src/timekeeping/storage/frameCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class FrameCollection {
try {
return Frame.fromFile(this.config, Fs.join(this.config.frameDir, file.name));
} catch (e) {
console.log(e);
throw new Error(`Error parsing frame file: ${file.name}`)
throw new Error(`Error parsing frame file: ${file.name}`, { cause: e });
}
})
.filter(frame => frame);
Expand Down