Skip to content
This repository has been archived by the owner on Oct 30, 2020. It is now read-only.

Commit

Permalink
feat: CLI options simple and no-color (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
phuctm97 authored Apr 26, 2020
1 parent 42a9830 commit a6333f1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 30 additions & 2 deletions src/bin/main.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
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.
*
* @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');
}
Expand Down
5 changes: 4 additions & 1 deletion src/reporters/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -288,4 +290,5 @@ export default function createConsoleReporter(opts = {}) {
*/
createConsoleReporter.defaultOpts = {
color: true,
simple: false,
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a6333f1

Please sign in to comment.