Skip to content

Commit 13149b6

Browse files
author
Schmidely Stéphane
committed
Use of ExportUi
1 parent ef1af00 commit 13149b6

1 file changed

Lines changed: 3 additions & 102 deletions

File tree

src/commands/export.ts

Lines changed: 3 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { Command } from '@oclif/command'
2-
import { prompt, Question } from 'inquirer'
3-
import * as moment from 'moment'
42
import { EOL } from 'os'
53

64
// Our files - FIXME path import
75
import { FLAGS, FORMATS, PROJECTS } from '../utils/commands/export/constants'
8-
import ExportValidator, { DateValidator } from '../utils/commands/export/validators'
6+
import ExportUi from '../utils/commands/export/ui'
7+
import ExportValidator from '../utils/commands/export/validators'
98

109
export default class Export extends Command {
1110
static description = 'export data for a specific date / project'
@@ -48,7 +47,7 @@ export default class Export extends Command {
4847
// Step 2 - check flags values (or exit)
4948
if (missingFlags.length !== 0) {
5049
if (flags.interactive) {
51-
const missingFlagsData = await this.askMissingFlags(
50+
const missingFlagsData = await new ExportUi().askMissingFlags(
5251
missingFlags, availableFlagsValues
5352
)
5453
flags = { ...flags, ...missingFlagsData }
@@ -85,102 +84,4 @@ export default class Export extends Command {
8584
return el !== 'help'
8685
})
8786
}
88-
89-
/**
90-
* Ask user for missing flags
91-
*/
92-
private async askMissingFlags(
93-
missingFlags: Array<string>, availableFlagsValues: any
94-
) {
95-
const questions = missingFlags.map(el => {
96-
let input = null
97-
switch (el) {
98-
case 'format':
99-
input = this.askFormat(availableFlagsValues.formats)
100-
break
101-
case 'project':
102-
input = this.askProject(availableFlagsValues.projects)
103-
break
104-
case 'from':
105-
input = this.askStartDate()
106-
break
107-
case 'to':
108-
input = this.askEndDate()
109-
break
110-
default:
111-
this.error(`You should not be here with ${el}`, { exit: 3 })
112-
}
113-
return input
114-
})
115-
116-
const answers = await this.askUser(questions)
117-
return answers
118-
}
119-
120-
/**
121-
* InquirerJS question for output format
122-
* 'json' or 'csv' are currently supported
123-
*/
124-
private askFormat(availableFormats: Array<string>): Question {
125-
const choices = availableFormats.map(format => {
126-
return { name: format.toUpperCase(), value: format }
127-
})
128-
return {
129-
type: 'list',
130-
name: 'format',
131-
message: 'Which output format do you want ?',
132-
default: choices[0].value,
133-
choices
134-
}
135-
}
136-
137-
/**
138-
* InquirerJS question for project
139-
*/
140-
private askProject(availableProjects: Array<string>): Question {
141-
return {
142-
type: 'list',
143-
name: 'project',
144-
message: 'Which project ?',
145-
default: availableProjects[0],
146-
choices: availableProjects,
147-
pageSize: availableProjects.length
148-
}
149-
}
150-
151-
/**
152-
* InquirerJS question for start date export
153-
* valide ! (use of moment.js)
154-
*/
155-
private askStartDate(): Question {
156-
return {
157-
type: 'input',
158-
name: 'from',
159-
message: 'From which date (YYYY-MM-DD) ?',
160-
default: moment().startOf('month').format('YYYY-MM-DD'),
161-
validate: new DateValidator().isValid
162-
}
163-
}
164-
165-
/**
166-
* InquirerJS question for end date export
167-
*/
168-
private askEndDate(): Question {
169-
return {
170-
type: 'input',
171-
name: 'to',
172-
message: 'Until which date (YYYY-MM-DD) ?',
173-
default: moment().endOf('month').format('YYYY-MM-DD'),
174-
validate: new DateValidator().isValid
175-
}
176-
}
177-
178-
/**
179-
* Ask user input with Inquirer.js
180-
*
181-
* @see https://github.com/SBoudrias/Inquirer.js
182-
*/
183-
private async askUser(questions: Array<Question>) {
184-
return prompt(questions)
185-
}
18687
}

0 commit comments

Comments
 (0)