|
| 1 | +import { Command } from '@oclif/command' |
| 2 | +import { EOL } from 'os' |
| 3 | + |
| 4 | +// Our files - FIXME path import |
| 5 | +import { FLAGS, FORMATS, PROJECTS } from '../utils/commands/export/constants' |
| 6 | +import ExportUi from '../utils/commands/export/ui' |
| 7 | +import ExportUtils from '../utils/commands/export/utils' |
| 8 | +import ExportValidator from '../utils/commands/export/validators' |
| 9 | + |
| 10 | +export default class Export extends Command { |
| 11 | + static description = 'export data for a specific date / project' |
| 12 | + |
| 13 | + static examples = [ |
| 14 | + '$ twe export --interactive', |
| 15 | + '$ twe export --format=json --interactive', |
| 16 | + '$ twe export --format=csv --project=name --from=2019-12-01 --to=2019-12-15' |
| 17 | + ] |
| 18 | + |
| 19 | + // https://oclif.io/docs/flags |
| 20 | + static flags = FLAGS |
| 21 | + |
| 22 | + // https://oclif.io/docs/args |
| 23 | + static args = [] |
| 24 | + |
| 25 | + /** |
| 26 | + * Entrypoint of the `twe export` command |
| 27 | + */ |
| 28 | + async run() { |
| 29 | + const tools = { |
| 30 | + utils: new ExportUtils(), |
| 31 | + validator: new ExportValidator() |
| 32 | + } |
| 33 | + |
| 34 | + // Step 1 - check requirements |
| 35 | + this.checkRequirements(tools) |
| 36 | + |
| 37 | + // Step 2 - Prepare parsing |
| 38 | + let { |
| 39 | + flags, availableFlags, missingFlags, availableFlagsValues |
| 40 | + } = this.prepareCommandParsing(tools) |
| 41 | + |
| 42 | + // Step 3 - check flags values (or exit) |
| 43 | + if (missingFlags.length !== 0) { |
| 44 | + const missingFlagsData = await this.askMissingFlags( |
| 45 | + flags.interactive, missingFlags, availableFlagsValues |
| 46 | + ) |
| 47 | + flags = { ...flags, ...missingFlagsData } |
| 48 | + } |
| 49 | + |
| 50 | + // Step 3 - Verify params |
| 51 | + const { interactive, ...params } = flags |
| 52 | + this.checkParams(tools, params, availableFlagsValues) |
| 53 | + |
| 54 | + // (FIXME) Step 4 - Extract data |
| 55 | + // (FIXME) Step 5 - Filter data |
| 56 | + // (FIXME) Step 6 - Aggregate data |
| 57 | + // (FIXME) Step 7 - Save data |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Check system requirements |
| 62 | + */ |
| 63 | + private checkRequirements(tools: any) { |
| 64 | + // Step 1 - check requirements |
| 65 | + const invalidRequirements = tools.validator.checkRequirements() |
| 66 | + if (invalidRequirements.length !== 0) { |
| 67 | + this.error( |
| 68 | + tools.utils.errorMessage('missing requirement', invalidRequirements), |
| 69 | + { exit: 2 } |
| 70 | + ) |
| 71 | + } |
| 72 | + return true |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Prepare some stuff |
| 77 | + */ |
| 78 | + private prepareCommandParsing(tools: any) { |
| 79 | + // Prepare information |
| 80 | + const { flags } = this.parse(Export) |
| 81 | + const availableFlags = tools.utils.availableFlags(FLAGS) |
| 82 | + const missingFlags = availableFlags.filter(el => !(el in flags)) |
| 83 | + const availableFlagsValues = { formats: FORMATS, projects: PROJECTS } |
| 84 | + return { flags, availableFlags, missingFlags, availableFlagsValues } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Ask for missing flags when interactive mode |
| 89 | + */ |
| 90 | + private async askMissingFlags( |
| 91 | + interactive: boolean, missingFlags: Array<string>, availableFlagsValues: any |
| 92 | + ) { |
| 93 | + if (!interactive) { |
| 94 | + this.error('Missing parameters', { exit: 3 }) |
| 95 | + } |
| 96 | + const missingFlagsData = await new ExportUi().askMissingFlags( |
| 97 | + missingFlags, availableFlagsValues |
| 98 | + ) |
| 99 | + return missingFlagsData |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Check validity of every params |
| 104 | + */ |
| 105 | + private checkParams(tools: any, params: any, availableFlagsValues: any) { |
| 106 | + const invalidParams = tools.validator.checkParams(params, availableFlagsValues) |
| 107 | + if (invalidParams.length !== 0) { |
| 108 | + this.error( |
| 109 | + tools.utils.errorMessage('invalid param', invalidParams), |
| 110 | + { exit: 2 } |
| 111 | + ) |
| 112 | + } |
| 113 | + return true |
| 114 | + } |
| 115 | +} |
0 commit comments