|
1 | 1 | import { Command } from '@oclif/command' |
2 | | -import { prompt, Question } from 'inquirer' |
3 | | -import * as moment from 'moment' |
4 | 2 | import { EOL } from 'os' |
5 | 3 |
|
6 | 4 | // Our files - FIXME path import |
7 | 5 | 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' |
9 | 8 |
|
10 | 9 | export default class Export extends Command { |
11 | 10 | static description = 'export data for a specific date / project' |
@@ -48,7 +47,7 @@ export default class Export extends Command { |
48 | 47 | // Step 2 - check flags values (or exit) |
49 | 48 | if (missingFlags.length !== 0) { |
50 | 49 | if (flags.interactive) { |
51 | | - const missingFlagsData = await this.askMissingFlags( |
| 50 | + const missingFlagsData = await new ExportUi().askMissingFlags( |
52 | 51 | missingFlags, availableFlagsValues |
53 | 52 | ) |
54 | 53 | flags = { ...flags, ...missingFlagsData } |
@@ -85,102 +84,4 @@ export default class Export extends Command { |
85 | 84 | return el !== 'help' |
86 | 85 | }) |
87 | 86 | } |
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 | | - } |
186 | 87 | } |
0 commit comments