Skip to content

Commit

Permalink
refactor: replace commander with yargs
Browse files Browse the repository at this point in the history
yargs has better validation options and seems easier to use than commander. the parsed args are typed as well, which is a bonus
  • Loading branch information
satya164 committed Jan 6, 2019
1 parent a392a12 commit fd4af7b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 47 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"babel-core": "^7.0.0-bridge.0",
"codecov": "^3.1.0",
"conventional-changelog-cli": "^2.0.5",
"dedent": "^0.7.0",
"del-cli": "^1.1.0",
"dtslint": "^0.3.0",
"eslint": "^5.7.0",
Expand All @@ -84,17 +83,16 @@
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
"@babel/register": "^7.0.0",
"commander": "^2.19.0",
"dedent": "^0.7.0",
"glob": "^7.1.3",
"loader-utils": "^1.1.0",
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"postcss": "^7.0.2",
"react-is": "^16.5.1",
"rollup-pluginutils": "^2.3.3",
"source-map": "^0.7.3",
"stylis": "^3.5.4"
"stylis": "^3.5.4",
"yargs": "^12.0.5"
},
"resolutions": {
"**/babel-core": "7.0.0-bridge.0"
Expand Down
81 changes: 39 additions & 42 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,48 @@ const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const glob = require('glob');
const commander = require('commander');
const yargs = require('yargs');
const transform = require('./transform');

commander
.version(require('../package.json').version)
.usage('[options] <file ...>')
.option('-o, --out-dir <dir>', 'output directory for the extracted CSS files')
.option('-s, --source-maps', 'generate source maps for the CSS files')
.option('-r, --source-root <dir>', 'directory contaning the source JS files')
.option(
'-i, --insert-css-requires <dir>',
'directory containing JS files to insert require statements for the CSS files'
)
.action((file, ...rest) => {
if (typeof file !== 'string') {
console.error('No files were specified to extract CSS from.\n');
commander.help();
process.exit(1);
}

const command = rest[rest.length - 1];
const files = [file, ...rest.slice(0, -1)];

if (!command.outDir) {
console.error('--out-dir must be specified.\n');
commander.help();
process.exit(1);
}

if (command.insertCssRequires && !command.sourceRoot) {
console.error(
'--source-root must be specified when --insert-css-requires is specified.\n'
);
commander.help();
process.exit(1);
}

processFiles(files, {
outDir: command.outDir,
sourceMaps: command.sourceMaps,
sourceRoot: command.sourceRoot,
insertCssRequires: command.insertCssRequires,
});
const { argv } = yargs
.usage('Usage: $0 [options] <files ...>')
.option('out-dir', {
alias: 'o',
type: 'string',
description: 'Output directory for the extracted CSS files',
demandOption: true,
requiresArg: true,
})
.option('source-maps', {
alias: 's',
type: 'boolean',
description: 'Generate source maps for the CSS files',
default: false,
})
.option('source-root', {
alias: 'r',
// type: 'string',
description: 'Directory contaning the source JS files',
requiresArg: true,
})
.option('insert-css-requires', {
alias: 'i',
type: 'string',
description:
'Directory containing JS files to insert require statements for the CSS files',
requiresArg: true,
})
.parse(process.argv);
.implies('insert-css-requires', 'source-root')
.alias('help', 'h')
.alias('version', 'v')
.strict();

processFiles(argv._, {
outDir: argv['out-dir'],
sourceMaps: argv['source-maps'],
sourceRoot: argv['source-root'],
insertCssRequires: argv['insert-css-requires'],
});

type Options = {
outDir: string,
Expand Down
28 changes: 27 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2318,7 +2318,7 @@ commander@^2.11.0, commander@^2.8.1:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.18.0.tgz#2bf063ddee7c7891176981a2cc798e5754bc6970"
integrity sha512-6CYPa+JP2ftfRU2qkDK+UTVeQYosOg/2GbcjIcKPHfinyOLPVGXu/ovN86RP49Re5ndJK1N0kuiidFFuepc4ZQ==

commander@^2.12.1, commander@^2.19.0:
commander@^2.12.1:
version "2.19.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.19.0.tgz#f6198aa84e5b83c46054b94ddedbfed5ee9ff12a"
integrity sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==
Expand Down Expand Up @@ -8356,6 +8356,14 @@ yargs-parser@^10.1.0:
dependencies:
camelcase "^4.1.0"

yargs-parser@^11.1.1:
version "11.1.1"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-11.1.1.tgz#879a0865973bca9f6bab5cbdf3b1c67ec7d3bcf4"
integrity sha512-C6kB/WJDiaxONLJQnF8ccx9SEeoTTLek8RVbaOIsrAUS8VrBEXfmeSnCZxygc+XC2sNMBIwOOnfcxiynjHsVSQ==
dependencies:
camelcase "^5.0.0"
decamelize "^1.2.0"

yargs-parser@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-8.1.0.tgz#f1376a33b6629a5d063782944da732631e966950"
Expand Down Expand Up @@ -8424,6 +8432,24 @@ yargs@^12.0.1:
y18n "^3.2.1 || ^4.0.0"
yargs-parser "^10.1.0"

yargs@^12.0.5:
version "12.0.5"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-12.0.5.tgz#05f5997b609647b64f66b81e3b4b10a368e7ad13"
integrity sha512-Lhz8TLaYnxq/2ObqHDql8dX8CJi97oHxrjUcYtzKbbykPtVW9WB+poxI+NM2UIzsMgNCZTIf0AQwsjK5yMAqZw==
dependencies:
cliui "^4.0.0"
decamelize "^1.2.0"
find-up "^3.0.0"
get-caller-file "^1.0.1"
os-locale "^3.0.0"
require-directory "^2.1.1"
require-main-filename "^1.0.1"
set-blocking "^2.0.0"
string-width "^2.0.0"
which-module "^2.0.0"
y18n "^3.2.1 || ^4.0.0"
yargs-parser "^11.1.1"

yargs@~3.10.0:
version "3.10.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.10.0.tgz#f7ee7bd857dd7c1d2d38c0e74efbd681d1431fd1"
Expand Down

0 comments on commit fd4af7b

Please sign in to comment.