Skip to content

Commit

Permalink
Add CLI support
Browse files Browse the repository at this point in the history
Add cli.js
  • Loading branch information
apoorv-mishra committed Jul 17, 2020
1 parent 50be26f commit a6e899d
Show file tree
Hide file tree
Showing 5 changed files with 407 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ quick-test/*
*~
*.swo
*.swp
bin
79 changes: 79 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import chalk from 'chalk';
import arg from 'arg';

import guessFormat from './index.js';
import pkg from './package';

const warning = (message) => chalk`{yellow WARNING:} ${message}`;
const info = (message) => chalk`{magenta INFO:} ${message}`;
const error = (message) => chalk`{red ERROR:} ${message}`;

const showHelp = () => console.log(
chalk`
{bold.cyan moment-guess} - {blue Utility for guessing date's format}
{bold USAGE}
{bold $} {cyan moment-guess} --date {yellow 2020-10-10}
{bold $} {cyan moment-guess} --version
{bold $} {cyan moment-guess} --help
{bold OPTIONS}
-h, --help Shows this help message
-v, --version Displays the current version of moment-guess
-d, --date Displays the provided date's format
`
);
const showUsage = () => console.log(
chalk`
{bold USAGE}
{bold $} {cyan moment-guess} --date {yellow 2020-10-10}
{bold $} {cyan moment-guess} --version
{bold $} {cyan moment-guess} --help
`
);

const showVersion = () => console.log(chalk.bold.white(pkg.version));

(function() {
let args = null;
let config = {};
let date;

try {
args = arg({
'--help': Boolean,
'--version': Boolean,
'--date': String,
'--preference': String,
'-h': '--help',
'-v': '--version',
'-d': '--date',
'-p': '--preference',
});
} catch (err) {
console.error(error(err.message));
process.exit(1);
}

if (args['--help']) {
return showHelp()
}
if (args['--version']) {
return showVersion();
}
if (args['--date']) {
date = args['--date'];
}

if (!date) {
console.log(chalk`{bold.red Error:} {red Missing date!}`);
return showUsage();
}
console.log(chalk.bold.white(guessFormat(date, config)));
})();

Loading

0 comments on commit a6e899d

Please sign in to comment.