|
| 1 | +const _ = require('lodash'); |
| 2 | +const flatten = require('flat'); |
| 3 | +const columnify = require('columnify'); |
| 4 | +const yaml = require('js-yaml'); |
| 5 | +const Style = require('../../../output/style'); |
| 6 | +const { NoPropertyError, MultiplePropertiesError, NotFullPropertyError, SchemaValidationError } = require('../../../logic/cli-config/errors'); |
| 7 | + |
| 8 | +const _jsonFormatter = data => JSON.stringify(data, null, 4); |
| 9 | +const COLUMNIFY_OPTS = { columnSplitter: ' ', headingTransform: Style.bold.uppercase }; |
| 10 | + |
| 11 | +const NO_PROPERTIES_MESSAGE = 'no properties'; |
| 12 | + |
| 13 | +function _valueFormatter(value) { |
| 14 | + if (value === null) { |
| 15 | + return Style.gray('<null>'); |
| 16 | + } |
| 17 | + if (typeof value === 'string') { |
| 18 | + return Style.green(`'${value}'`); |
| 19 | + } |
| 20 | + if (typeof value === 'boolean') { |
| 21 | + return Style.yellow(value); |
| 22 | + } |
| 23 | + return value; |
| 24 | +} |
| 25 | + |
| 26 | +function _defaultOutput(data) { |
| 27 | + const flat = flatten(data); |
| 28 | + const columns = _.keys(flat) |
| 29 | + .sort() |
| 30 | + .map((key) => { |
| 31 | + const value = _valueFormatter(_.get(data, key)); |
| 32 | + return { key, value }; |
| 33 | + }); |
| 34 | + return columnify(columns, COLUMNIFY_OPTS); |
| 35 | +} |
| 36 | + |
| 37 | +function _formatter(format) { |
| 38 | + switch (format) { |
| 39 | + case 'yaml': |
| 40 | + return yaml.safeDump; |
| 41 | + case 'json': |
| 42 | + return _jsonFormatter; |
| 43 | + default: |
| 44 | + return _defaultOutput; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function outputCliConfig(format, data) { |
| 49 | + const formatter = _formatter(format); |
| 50 | + console.log(formatter(data)); |
| 51 | +} |
| 52 | + |
| 53 | +function outputCliMeta(props) { |
| 54 | + const columns = props.map(meta => { |
| 55 | + meta.default = _valueFormatter(meta.default); |
| 56 | + return meta; |
| 57 | + }); |
| 58 | + if (columns.length) { |
| 59 | + console.log(columnify(columns, COLUMNIFY_OPTS)); |
| 60 | + } else { |
| 61 | + console.log(NO_PROPERTIES_MESSAGE); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function printProperties(properties) { |
| 66 | + properties.sort().forEach(prop => console.log(prop)); |
| 67 | +} |
| 68 | + |
| 69 | +function propertyErrorHandler(e) { |
| 70 | + if (e instanceof NoPropertyError) { |
| 71 | + console.log(`Property "${e.property}" is not supported`); |
| 72 | + return; |
| 73 | + } |
| 74 | + if (e instanceof MultiplePropertiesError) { |
| 75 | + console.log('Choose one of the following properties:\n'); |
| 76 | + printProperties(e.properties); |
| 77 | + return; |
| 78 | + } |
| 79 | + if (e instanceof NotFullPropertyError) { |
| 80 | + console.log(`Did you mean property: ${e.property}?`); |
| 81 | + return; |
| 82 | + } |
| 83 | + if (e instanceof SchemaValidationError) { |
| 84 | + e.printErrors(); |
| 85 | + return; |
| 86 | + } |
| 87 | + throw e; |
| 88 | +} |
| 89 | + |
| 90 | +module.exports = { |
| 91 | + outputCliConfig, |
| 92 | + printProperties, |
| 93 | + propertyErrorHandler, |
| 94 | + outputCliMeta, |
| 95 | +}; |
0 commit comments