Skip to content

Instantly share code, notes, and snippets.

@mitchellsimoens
Created October 1, 2019 20:58
Show Gist options
  • Save mitchellsimoens/53c1d3bdd7465171e95eb01bdd5f25bd to your computer and use it in GitHub Desktop.
Save mitchellsimoens/53c1d3bdd7465171e95eb01bdd5f25bd to your computer and use it in GitHub Desktop.
Testing yargs for Gimbal
import * as yargs from 'yargs';
import './register';
const parser = yargs
.showHelpOnFail(false, 'Specify --help for available options')
.options({
'check-thresholds': {
boolean: true,
default: true,
describe: 'Set to disable checking thresholds.',
},
'comment': {
boolean: true,
default: true,
describe: 'Set to disable commenting results on the VCS.',
},
'config': {
describe: 'The file to load as the configuration file.',
requiresArg: true,
string: true,
},
'cwd': {
default: process.cwd(),
describe: 'The directory to work in.',
requiresArg: true,
string: true,
},
'output-html': {
describe: 'The path to write the results as HTML to.',
requiresArg: true,
string: true,
},
'output-json': {
describe: 'The path to write the results as JSON to.',
requiresArg: true,
string: true,
},
'output-markdown': {
describe: 'The path to write the results as Markdown to.',
requiresArg: true,
string: true,
},
'verbose': {
alias: 'v',
boolean: true,
default: false,
describe: 'Turn on extra logging during command executions.',
},
});
(async () => {
if (!parser.argv._.length) {
await parser.parse([
'audit',
...process.argv.slice(2)
]);
}
console.log('end!');
})();
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "gimbal",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/src/index.ts",
"--no-size",
"--route",
"/foo",
"--route",
"/bar"
]
},
{
"type": "node",
"request": "launch",
"name": "gimbal audit",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/src/index.ts",
"--no-size",
"audit"
]
},
{
"type": "node",
"request": "launch",
"name": "gimbal --help",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/src/index.ts",
"--help"
]
},
{
"type": "node",
"request": "launch",
"name": "gimbal audit --help",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/src/index.ts",
"audit",
"--help"
]
}
]
}
{
"name": "gimbal-yargs-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node -r ts-node/register src/index.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "12.7.8",
"@types/yargs": "13.0.3",
"ts-node": "8.4.1",
"typescript": "3.6.3",
"yargs": "14.0.0"
}
}
import * as yargs from 'yargs';
yargs
.command('audit [opts]', 'Run an audit', yargs =>
yargs
.options({
'build-dir': {
describe: 'Directory storing the build artifacts relative to the --cwd',
default: 'build',
requiresArg: true,
string: true,
},
'calculate-unused-source': {
boolean: true,
describe: 'Disable calculating unused CSS and JavaScript',
default: false,
},
'heap-snapshot': {
boolean: true,
describe: 'Disable getting a heap snapshot',
default: false,
},
'lighthouse': {
boolean: true,
describe: 'Disable the lighthouse auditing',
default: false,
},
'lighthouse-output-html': {
describe: 'Location to output the lighthouse HTML report to.',
requiresArg: true,
string: true,
},
'route': {
describe: 'Route to run tests on.',
default: '/',
requiresArg: true,
string: true,
},
'size': {
boolean: true,
describe: 'Disable checking resource sizes.',
default: false,
},
}),
yargs => {
return new Promise(resolve => {
console.log('run audit command!');
console.log(
JSON.stringify(yargs, null, 2)
);
setTimeout(resolve, 500);
});
}
);
@mitchellsimoens
Copy link
Author

This gist is playing around with how gimbal could use yargs instead of commander.

index.ts in this gist would be code that could go into https://github.com/ModusCreateOrg/gimbal/blob/master/packages/gimbal/src/index.ts and would likely simplify some of what is being done.

register.ts in this gist is an example of splitting commands out like we do in the Commandclass that is kicked off here: https://github.com/ModusCreateOrg/gimbal/blob/master/packages/gimbal/src/index.ts#L41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment