Skip to content

Commit 3cff2df

Browse files
author
Schmidely Stéphane
committed
Handle error display and fix system requirements validator
1 parent daf0212 commit 3cff2df

3 files changed

Lines changed: 24 additions & 36 deletions

File tree

src/commands/export.ts

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { EOL } from 'os'
44
// Our files - FIXME path import
55
import { FLAGS, FORMATS, PROJECTS } from '../utils/commands/export/constants'
66
import ExportUi from '../utils/commands/export/ui'
7+
import ExportUtils from '../utils/commands/export/utils'
78
import ExportValidator from '../utils/commands/export/validators'
89

910
export default class Export extends Command {
@@ -25,36 +26,33 @@ export default class Export extends Command {
2526
* Entrypoint of the `twe export` command
2627
*/
2728
async run() {
28-
let { flags } = this.parse(Export)
29-
const availableFlags = this.availableFlags(FLAGS)
30-
const missingFlags = availableFlags.filter(el => !(el in flags))
31-
const availableFlagsValues = {
32-
formats: FORMATS,
33-
projects: PROJECTS
34-
}
29+
const utils = new ExportUtils()
3530
const validator = new ExportValidator()
3631

3732
// Step 1 - check requirements
3833
const invalidRequirements = validator.checkRequirements()
3934
if (invalidRequirements.length !== 0) {
40-
const plural = (invalidRequirements.length > 1) ? 's' : ''
41-
let errorMessage = `missing requirement${plural} ${EOL}`
42-
errorMessage += invalidRequirements.map((error: any) => {
43-
return `• ${error}`
44-
}).join(EOL)
45-
this.error(errorMessage, { exit: 2 })
35+
this.error(
36+
utils.errorMessage('missing requirement', invalidRequirements),
37+
{ exit: 2 }
38+
)
4639
}
4740

41+
// Prepare information
42+
let { flags } = this.parse(Export)
43+
const availableFlags = utils.availableFlags(FLAGS)
44+
const missingFlags = availableFlags.filter(el => !(el in flags))
45+
const availableFlagsValues = { formats: FORMATS, projects: PROJECTS }
46+
4847
// Step 2 - check flags values (or exit)
4948
if (missingFlags.length !== 0) {
50-
if (flags.interactive) {
51-
const missingFlagsData = await new ExportUi().askMissingFlags(
52-
missingFlags, availableFlagsValues
53-
)
54-
flags = { ...flags, ...missingFlagsData }
55-
} else {
49+
if (!flags.interactive) {
5650
this.error('Missing parameters', { exit: 3 })
5751
}
52+
const missingFlagsData = await new ExportUi().askMissingFlags(
53+
missingFlags, availableFlagsValues
54+
)
55+
flags = { ...flags, ...missingFlagsData }
5856
}
5957

6058
// Step 2.1 - remove 'interactive' flag
@@ -63,26 +61,15 @@ export default class Export extends Command {
6361
// Step 3 - Verify params
6462
const invalidParams = validator.checkParams(params, availableFlagsValues)
6563
if (invalidParams.length !== 0) {
66-
const plural = (invalidParams.length > 1) ? 's' : ''
67-
let errorMessage = `invalid param${plural} ${EOL}`
68-
errorMessage += invalidParams.map((invalidParam: string) => {
69-
return `• ${invalidParam}`
70-
}).join(EOL)
71-
this.error(errorMessage, { exit: 2 })
64+
this.error(
65+
utils.errorMessage('invalid param', invalidParams),
66+
{ exit: 2 }
67+
)
7268
}
7369

7470
// (FIXME) Step 4 - Extract data
7571
// (FIXME) Step 5 - Filter data
7672
// (FIXME) Step 6 - Aggregate data
7773
// (FIXME) Step 7 - Save data
7874
}
79-
80-
/**
81-
* CLI flags of 'export' commands
82-
*/
83-
private availableFlags(flags: any) {
84-
return Object.keys(flags).filter((el, _index, _array) => {
85-
return el !== 'help'
86-
})
87-
}
8875
}

src/utils/commands/export/validators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class ExportValidator {
1515
public checkRequirements(): Array<string> {
1616
return this.executables.reduce((acc: Array<any>, executable) => {
1717
const installed = new SystemRequirementValidator().isValid(executable)
18-
return installed ? acc : acc.concat([executable])
18+
return installed === true ? acc : acc.concat([installed])
1919
}, [])
2020
}
2121

src/utils/commands/export/validators/system-requirement-validator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { Validator } from './validator'
77
*/
88
export default class SystemRequirementValidator implements Validator {
99
public isValid(executable: any) {
10-
let valid: boolean | string = which(executable.cmd)
10+
const path = which(executable.cmd)
11+
let valid: boolean | string = (path !== null)
1112
if (!valid) {
1213
valid = `${executable.name} is not installed `
1314
valid += `(command '${executable.cmd}')`

0 commit comments

Comments
 (0)