diff --git a/package.json b/package.json index f5f16ee..590b6b0 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "dependencies": { "ansi-escape-sequences": "^5.1.2", + "getopts": "^2.2.5", "js-yaml": "^3.13.1", "lodash": "^4.17.15", "markdown-table": "^2.0.0", diff --git a/src/bin/main.js b/src/bin/main.js index f7a3a9b..ee9ffb8 100644 --- a/src/bin/main.js +++ b/src/bin/main.js @@ -1,8 +1,17 @@ +import * as getopts from 'getopts'; import runWorkflow, { loadJobs } from '@core/workflow'; import createConsoleReporter from '@reporters/console'; import readConfig from '@bin/readConfig'; import library from '@bin/library'; +/** + * The `jxax` CLI usage. + */ +const USAGE = ` +usage: jxax WORKFLOW_FILE [-s|--simple] [--no-color] + -s, --simple Print in simple format + --no-color Print in plain format +`.trim(); /** * The `jxax` CLI application entry. @@ -10,11 +19,30 @@ import library from '@bin/library'; * @param {string[]} args The CLI arguments. */ export default function main(args) { - if (args.length !== 1) throw new Error('expected [file] argument'); + // Parse args. + const opts = getopts(args, { + alias: { + help: 'h', + simple: 's', + }, + default: { + color: true, + simple: false, + }, + }); + + if (opts.help) { // Print help. + console.log(`jxax CLI\n\n${USAGE}`); + return; + } + + const file = opts._[0]; + if (!file) throw new Error(`expected WORKFLOW_FILE argument\n\n${USAGE}`); + // Run workflow. const config = readConfig(args[0]); const jobs = loadJobs(library, config); - const reporter = createConsoleReporter(); + const reporter = createConsoleReporter({ simple: opts.simple, color: opts.color }); if (!runWorkflow(jobs, { reporter })) { throw new Error('workflow failed'); } diff --git a/src/reporters/console.js b/src/reporters/console.js index 1c2ea6f..c8dd076 100644 --- a/src/reporters/console.js +++ b/src/reporters/console.js @@ -123,10 +123,11 @@ function getConsoleColumns() { * * @param {object} opts Options. * @param {boolean} opts.color Report in colorful or plain text format? + * @param {boolean} opts.simple Report in simple format (only report errors)? * @returns {Reporter} The `Reporter`. */ export default function createConsoleReporter(opts = {}) { - const { color } = { ...createConsoleReporter.defaultOpts, ...opts }; + const { color, simple } = { ...createConsoleReporter.defaultOpts, ...opts }; let cols = 80; // Default console columns. // The reporter's variables. @@ -179,6 +180,7 @@ export default function createConsoleReporter(opts = {}) { let finishChar; let msgF; if (succeeded) { + if (simple) return undefined; // Don't print succeeded steps when in simple mode. finishChar = format( FINISH_CHARS[JobStatuses.SUCCEEDED], FINISH_FORMATS[JobStatuses.SUCCEEDED], @@ -288,4 +290,5 @@ export default function createConsoleReporter(opts = {}) { */ createConsoleReporter.defaultOpts = { color: true, + simple: false, }; diff --git a/yarn.lock b/yarn.lock index 79a944c..67ad2a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3401,6 +3401,11 @@ get-value@^2.0.3, get-value@^2.0.6: resolved "https://registry.yarnpkg.com/get-value/-/get-value-2.0.6.tgz#dc15ca1c672387ca76bd37ac0a395ba2042a2c28" integrity sha1-3BXKHGcjh8p2vTesCjlbogQqLCg= +getopts@^2.2.5: + version "2.2.5" + resolved "https://registry.yarnpkg.com/getopts/-/getopts-2.2.5.tgz#67a0fe471cacb9c687d817cab6450b96dde8313b" + integrity sha512-9jb7AW5p3in+IiJWhQiZmmwkpLaR/ccTWdWQCtZM66HJcHHLegowh4q4tSD7gouUyeNvFWRavfK9GXosQHDpFA== + getpass@^0.1.1: version "0.1.7" resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa"