Skip to content

Commit 6c62cfc

Browse files
author
Schmidely Stéphane
committed
Update export command with ExportValidator
1 parent 28c7cc2 commit 6c62cfc

1 file changed

Lines changed: 8 additions & 49 deletions

File tree

src/commands/export.ts

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { Command, flags } from '@oclif/command'
2-
import { prompt, Question, Separator } from 'inquirer'
1+
import { Command } from '@oclif/command'
2+
import { prompt, Question } from 'inquirer'
33
import * as moment from 'moment'
44
import { EOL } from 'os'
5-
import { which } from 'shelljs'
65

76
// Our files - FIXME path import
87
import { FLAGS, FORMATS, PROJECTS } from '../utils/commands/export/constants'
9-
import { DateValidator, FormatValidator, ProjectValidator } from '../utils/commands/export/validators'
8+
import ExportValidator, { DateValidator } from '../utils/commands/export/validators'
109

1110
export default class Export extends Command {
1211
static description = 'export data for a specific date / project'
@@ -33,14 +32,15 @@ export default class Export extends Command {
3332
formats: FORMATS,
3433
projects: PROJECTS
3534
}
35+
const validator = new ExportValidator()
3636

3737
// Step 1 - check requirements
38-
const invalidRequirements = this.checkRequirements()
38+
const invalidRequirements = validator.checkRequirements()
3939
if (invalidRequirements.length !== 0) {
4040
const plural = (invalidRequirements.length > 1) ? 's' : ''
4141
let errorMessage = `missing requirement${plural} ${EOL}`
42-
errorMessage += invalidRequirements.map((executable: any) => {
43-
return `• ${executable.name} is required (command '${executable.cmd}')`
42+
errorMessage += invalidRequirements.map((error: any) => {
43+
return `• ${error}`
4444
}).join(EOL)
4545
this.error(errorMessage, { exit: 2 })
4646
}
@@ -61,7 +61,7 @@ export default class Export extends Command {
6161
const { interactive, ...params } = flags
6262

6363
// Step 3 - Verify params
64-
const invalidParams = this.checkParams(params, availableFlagsValues)
64+
const invalidParams = validator.checkParams(params, availableFlagsValues)
6565
if (invalidParams.length !== 0) {
6666
const plural = (invalidParams.length > 1) ? 's' : ''
6767
let errorMessage = `invalid param${plural} ${EOL}`
@@ -86,20 +86,6 @@ export default class Export extends Command {
8686
})
8787
}
8888

89-
/**
90-
* Check that Taskwarrior and Timewarrior are installed
91-
*/
92-
private checkRequirements(): Array<string> {
93-
const executables = [
94-
{ name: 'Taskwarrior', cmd: 'task' },
95-
{ name: 'Timewarrior', cmd: 'timew' }
96-
]
97-
return executables.reduce((acc: Array<any>, executable) => {
98-
const installed = which(executable.cmd)
99-
return installed ? acc : acc.concat([executable])
100-
}, [])
101-
}
102-
10389
/**
10490
* Ask user for missing flags
10591
*/
@@ -197,31 +183,4 @@ export default class Export extends Command {
197183
private async askUser(questions: Array<Question>) {
198184
return prompt(questions)
199185
}
200-
201-
/**
202-
* Check if all params are valid
203-
*/
204-
private checkParams(params: any, availableFlagsValues: any) {
205-
return Object.keys(params).reduce((acc: Array<any>, key) => {
206-
const value = params[key]
207-
let valid: boolean | string = true
208-
switch (key) {
209-
case 'format':
210-
valid = new FormatValidator().isValid(value, availableFlagsValues.formats)
211-
break
212-
case 'project':
213-
valid = new ProjectValidator().isValid(value, availableFlagsValues.projects)
214-
break
215-
case 'from':
216-
valid = new DateValidator().isValid(value, {})
217-
break
218-
case 'to':
219-
valid = new DateValidator().isValid(value, params)
220-
break
221-
default:
222-
this.error(`You should not be here with ${key}`, { exit: 3 })
223-
}
224-
return valid !== true ? acc.concat([valid]) : acc
225-
}, [])
226-
}
227186
}

0 commit comments

Comments
 (0)