Skip to content

Commit ef1af00

Browse files
author
Schmidely Stéphane
committed
Add UI component for export command
1 parent 6c62cfc commit ef1af00

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

  • src/utils/commands/export

src/utils/commands/export/ui.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { prompt, Question } from 'inquirer'
2+
import * as moment from 'moment'
3+
4+
import { DateValidator } from './validators'
5+
6+
export default class ExportUi {
7+
constructor() {}
8+
9+
/**
10+
* Ask user for missing flags
11+
*/
12+
public async askMissingFlags(
13+
missingFlags: Array<string>, availableFlagsValues: any
14+
) {
15+
const questions = missingFlags.map(el => {
16+
let input = null
17+
switch (el) {
18+
case 'format':
19+
input = this.askFormat(availableFlagsValues.formats)
20+
break
21+
case 'project':
22+
input = this.askProject(availableFlagsValues.projects)
23+
break
24+
case 'from':
25+
input = this.askStartDate()
26+
break
27+
case 'to':
28+
input = this.askEndDate()
29+
break
30+
default:
31+
// FIXME handle error case
32+
// this.error(`You should not be here with ${el}`, { exit: 3 })
33+
}
34+
return input
35+
})
36+
37+
const answers = await this.askUser(questions)
38+
return answers
39+
}
40+
41+
/**
42+
* InquirerJS question for output format
43+
* 'json' or 'csv' are currently supported
44+
*/
45+
private askFormat(availableFormats: Array<string>): Question {
46+
const choices = availableFormats.map(format => {
47+
return { name: format.toUpperCase(), value: format }
48+
})
49+
return {
50+
type: 'list',
51+
name: 'format',
52+
message: 'Which output format do you want ?',
53+
default: choices[0].value,
54+
choices
55+
}
56+
}
57+
58+
/**
59+
* InquirerJS question for project
60+
*/
61+
private askProject(availableProjects: Array<string>): Question {
62+
return {
63+
type: 'list',
64+
name: 'project',
65+
message: 'Which project ?',
66+
default: availableProjects[0],
67+
choices: availableProjects,
68+
pageSize: availableProjects.length
69+
}
70+
}
71+
72+
/**
73+
* InquirerJS question for start date export
74+
* valide ! (use of moment.js)
75+
*/
76+
private askStartDate(): Question {
77+
return {
78+
type: 'input',
79+
name: 'from',
80+
message: 'From which date (YYYY-MM-DD) ?',
81+
default: moment().startOf('month').format('YYYY-MM-DD'),
82+
validate: new DateValidator().isValid
83+
}
84+
}
85+
86+
/**
87+
* InquirerJS question for end date export
88+
*/
89+
private askEndDate(): Question {
90+
return {
91+
type: 'input',
92+
name: 'to',
93+
message: 'Until which date (YYYY-MM-DD) ?',
94+
default: moment().endOf('month').format('YYYY-MM-DD'),
95+
validate: new DateValidator().isValid
96+
}
97+
}
98+
99+
/**
100+
* Ask user input with Inquirer.js
101+
*
102+
* @see https://github.com/SBoudrias/Inquirer.js
103+
*/
104+
private async askUser(questions: Array<Question>) {
105+
return prompt(questions)
106+
}
107+
}

0 commit comments

Comments
 (0)