feat(dev-infra): standard CLI commands using yargs (#36326)

Creates a standard model for CLI commands provided by ng-dev.
Allows for us to have any of the tools/scripts extend to be
included in the ng-dev command, or be standalone using the same
yargs parser.

PR Close #36326
This commit is contained in:
Joey Perrott
2020-03-26 10:45:09 -07:00
committed by Kara Erickson
parent 326240eb91
commit 43006bcc45
9 changed files with 114 additions and 40 deletions

View File

@ -19,22 +19,30 @@ import {compareGoldens, convertReferenceChainToGolden, Golden} from './golden';
import {convertPathToForwardSlash} from './file_system';
import {loadTestConfig, CircularDependenciesTestConfig} from './config';
if (require.main === module) {
const {_: command, config: configArg, warnings} =
yargs.help()
.strict()
.command('check', 'Checks if the circular dependencies have changed.')
.command('approve', 'Approves the current circular dependencies.')
.demandCommand()
.option(
'config',
{type: 'string', demandOption: true, description: 'Path to the configuration file.'})
.option('warnings', {type: 'boolean', description: 'Prints all warnings.'})
.argv;
const configPath = isAbsolute(configArg) ? configArg : resolve(configArg);
const config = loadTestConfig(configPath);
const isApprove = command.includes('approve');
process.exit(main(isApprove, config, warnings));
export function tsCircularDependenciesBuilder(localYargs: yargs.Argv) {
return localYargs.help()
.strict()
.demandCommand()
.option(
'config',
{type: 'string', demandOption: true, description: 'Path to the configuration file.'})
.option('warnings', {type: 'boolean', description: 'Prints all warnings.'})
.command(
'check', 'Checks if the circular dependencies have changed.', {},
(argv: yargs.Arguments) => {
const {config: configArg, warnings} = argv;
const configPath = isAbsolute(configArg) ? configArg : resolve(configArg);
const config = loadTestConfig(configPath);
process.exit(main(false, config, warnings));
})
.command(
'approve', 'Approves the current circular dependencies.', {}, (argv: yargs.Arguments) => {
const {config: configArg, warnings} = argv;
const configPath = isAbsolute(configArg) ? configArg : resolve(configArg);
const config = loadTestConfig(configPath);
process.exit(main(true, config, warnings));
});
}
/**
@ -126,3 +134,7 @@ function getRelativePath(baseDir: string, path: string) {
function convertReferenceChainToString(chain: ReferenceChain<string>) {
return chain.join(' → ');
}
if (require.main === module) {
tsCircularDependenciesBuilder(yargs).parse();
}