Skip to content

Commit

Permalink
[core] Replace commander with yargs (#872)
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot-preview[bot] authored Jan 18, 2021
1 parent 682c544 commit fb86cad
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/x-grid-data-generator/bin/data-gen-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
require = require('esm')(module);

// eslint-disable-next-line import/no-unresolved
require('../dist/index-cjs').datagenCli(process.argv);
require('../dist/index-cjs').datagenCli();
6 changes: 4 additions & 2 deletions packages/x-grid-data-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
"@types/chance": "^1.1.0",
"chance": "^1.1.6",
"clsx": "^1.0.4",
"commander": "^6.1.0",
"esm": "^3.2.25",
"lru-cache": "^6.0.0"
},
"devDependencies": {
"esm": "^3.2.25",
"yargs": "^15.0.0"
},
"peerDependencies": {
"@material-ui/core": "^4.9.12",
"@material-ui/icons": "^4.9.1",
Expand Down
74 changes: 50 additions & 24 deletions packages/x-grid-data-generator/src/datagen-cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import * as fs from 'fs';
import * as program from 'commander';
import * as yargs from 'yargs';
import { getCommodityColumns } from './commodities.columns';
import { getEmployeeColumns } from './employees.columns';
import { getRealData } from './services';
Expand All @@ -14,28 +14,54 @@ const loadData = async (size: number, dataset: string): Promise<any> => {
return result.rows;
};

export function datagenCli(args) {
program
.option('-d, --dataset <dataset>', 'A dataset can be "commodity" | "employee"', 'employee')
.option('-s, --size <size>', 'The number of rows to generate', '100')
.option(
'-o, --output <output>',
'The output file, if not passed, it will generate "./<dataset>-<size>.json"',
)
.option('-p, --pretty', 'print a prettier output', false)
.action(() => {
console.log(
`Generating new ${program.dataset} dataset with ${Number(
program.size,
).toLocaleString()} rows`,
);
loadData(Number(program.size), program.dataset).then((data) => {
const output = !program.output
? `./${program.dataset}-${program.size}.json`
: program.output;
console.log(`Saving generated dataset in ${output}`);
fs.writeFileSync(output, JSON.stringify(data, null, program.pretty ? 2 : undefined));
});
interface HandlerArgv {
dataset: string;
output: string;
pretty: boolean;
size: string;
}

export function datagenCli() {
yargs
.command({
command: '$0',
describe: 'Generates Component.propTypes from TypeScript declarations',
builder: (command) => {
return command
.option('dataset', {
default: '',
describe: 'A dataset can be "commodity" | "employee"',
type: 'string',
})
.option('output', {
default: '',
describe: 'The output file, if not passed, it will generate "./<dataset>-<size>.json"',
type: 'string',
})
.option('pretty', {
default: false,
describe: 'Print a prettier output',
type: 'boolean',
})
.option('size', {
default: '1000',
describe: 'The number of rows to generate',
type: 'string',
});
},
handler: (argv: HandlerArgv) => {
console.log(
`Generating new ${argv.dataset} dataset with ${Number(argv.size).toLocaleString()} rows`,
);
loadData(Number(argv.size), argv.dataset).then((data) => {
const output = !argv.output ? `./${argv.dataset}-${argv.size}.json` : argv.output;
console.log(`Saving generated dataset in ${output}`);
fs.writeFileSync(output, JSON.stringify(data, null, argv.pretty ? 2 : undefined));
});
},
})
.parse(args);
.help()
.strict(true)
.version(false)
.parse();
}
2 changes: 1 addition & 1 deletion packages/x-license/bin/license-gen-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/* eslint-disable */
require = require('esm')(module);

require('../dist/cjs/license-cli').licenseGenCli(process.argv);
require('../dist/cjs/license-cli').licenseGenCli();
4 changes: 2 additions & 2 deletions packages/x-license/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"licensegen": "./bin/license-gen-script.js"
},
"dependencies": {
"commander": "^6.1.0",
"yargs": "^15.0.0",
"esm": "^3.2.25"
},
"peerDependencies": {
"react": "^16.13.1 || ^17.0.0"
"react": "^16.8.0 || ^17.0.0"
},
"setupFiles": [
"<rootDir>/src/setupTests.js"
Expand Down
69 changes: 46 additions & 23 deletions packages/x-license/src/license-cli.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
/* eslint-disable no-console */
import * as program from 'commander';
import * as yargs from 'yargs';
import { generateLicence } from './generateLicense';

const oneDayInMs = 1000 * 60 * 60 * 24;

export function licenseGenCli(args: any) {
program
.option('-o, --order <order>', 'Order number id')
.option('-e, --expiry <expiry>', 'Number of days from now until expiry date', '366')
.action(() => {
if (!program.order) {
throw new Error(
'Material-UI: You forgot to pass an order number. $ > licensegen -o order_123.',
);
}
interface HandlerArgv {
order: string;
expiry: string;
}

const licenseDetails = {
expiryDate: new Date(new Date().getTime() + parseInt(program.expiry, 10) * oneDayInMs),
orderNumber: program.order,
};
export function licenseGenCli() {
yargs
.command({
command: '$0',
describe: 'Generates Component.propTypes from TypeScript declarations',
builder: (command) => {
return command
.option('order', {
default: '',
describe: 'Order number id.',
type: 'string',
})
.option('expiry', {
default: '366',
describe: 'Number of days from now until expiry date.',
type: 'string',
});
},
handler: (argv: HandlerArgv) => {
if (!argv.order) {
throw new Error(
'Material-UI: You forgot to pass an order number. $ > licensegen -o order_123.',
);
}

console.log(
`Generating new license number for order ${
licenseDetails.orderNumber
} with expiry date ${licenseDetails.expiryDate.toLocaleDateString()}`,
);
const license = generateLicence(licenseDetails);
console.log(`New license: \n${license}`);
const licenseDetails = {
expiryDate: new Date(new Date().getTime() + parseInt(argv.expiry, 10) * oneDayInMs),
orderNumber: argv.order,
};

console.log(
`Generating new license number for order ${
licenseDetails.orderNumber
} with expiry date ${licenseDetails.expiryDate.toLocaleDateString()}`,
);
const license = generateLicence(licenseDetails);
console.log(`New license: \n${license}`);
},
})
.parse(args);
.help()
.strict(true)
.version(false)
.parse();
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20606,7 +20606,7 @@ yargs@^14.2.2:
y18n "^4.0.0"
yargs-parser "^15.0.1"

yargs@^15.0.2, yargs@^15.3.1, yargs@^15.4.1:
yargs@^15.0.0, yargs@^15.0.2, yargs@^15.3.1, yargs@^15.4.1:
version "15.4.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.4.1.tgz#0d87a16de01aee9d8bec2bfbf74f67851730f4f8"
integrity sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A==
Expand Down

0 comments on commit fb86cad

Please sign in to comment.