Skip to content

Commit

Permalink
feat(schematics): Generate --help for every CLI function.
Browse files Browse the repository at this point in the history
    This PR introduces the yargs package as our CLI command handler.
    We build up commands using yargs to take advantage of its built
    in --help flag formatter.
  • Loading branch information
mrmeku committed Apr 12, 2018
1 parent ba66d82 commit 487835e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 62 deletions.
10 changes: 1 addition & 9 deletions packages/schematics/src/command-line/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function affected(args: string[]): void {
let apps: string[];
let projects: string[];
let rest: string[];

try {
const p = parseFiles(args.slice(1));
rest = p.rest;
Expand Down Expand Up @@ -38,15 +39,6 @@ export function affected(args: string[]): void {
}

function printError(command: string, e: any) {
console.error(
`Pass the SHA range, as follows: npm run affected:${command} -- SHA1 SHA2.`
);
console.error(
`Or pass the list of files, as follows: npm run affected:${command} -- --files="libs/mylib/index.ts,libs/mylib2/index.ts".`
);
console.error(
`Or to get the list of files from local changes: npm run affected:${command} -- --uncommitted | --untracked.`
);
console.error(e.message);
}

Expand Down
112 changes: 59 additions & 53 deletions packages/schematics/src/command-line/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,64 @@ const args = process.argv.slice(3);

yargs
.usage('Usage: $0 <command> [options>')
.command(
'affected <action>',
'Compute affected applications',
yargs =>
yargs
.positional('action', {
choices: ['apps', 'build', 'e2e', 'dep-graph']
})
.command('<action> -- <SHA1 SHA2>', 'Targets within a range of SHAs')
.command(
'<action> -- files="<file,list>"',
'Targets a comma delimited list of files'
)
.command('<action> -- --uncommitted', 'Targets uncommited changes')
.command('<action> -- --untracked', 'Targets untracked changes')
.demandCommand()
.command(
'dep-graph',
'Generate a graph showing links between targets',
yargs =>
yargs
.describe('file', 'output file (e.g. --file=.vis/output.json)')
.command('-- <SHA1 SHA2>', 'Targets within a range of SHAs')
.command(
'-- files="<file,list>"',
'Targets a comma delimited list of files'
)
.command('-- --uncommitted', 'Targets uncommited changes')
.command('-- --untracked', 'Targets untracked changes')
.demandCommand()
.demand('file')
.demand('output')
.choices('output', [
OutputType.json,
OutputType.dot,
OutputType.html
]),
_ => {
generateGraph(yargsParser(args));
}
)
.demandCommand()
.describe('args', 'Prints the names of the affected apps')
.describe('build', 'Builds the affected apps')
.describe('e2e', 'Tests the affected apps')
.describe(
'dep-graph',
'Generate a graph showing links between apps and libs'
),
_ => {
affected(args);
}
.command('affected', 'Compute affected applications', yargs =>
yargs
.option('files', {
type: 'array',
group: 'target',
describe: 'Files in for which affected targets will be computed'
})
.option('uncommitted', {
type: 'boolean',
group: 'target',
describe: 'Compute targets for uncommitted changes'
})
.option('untracked', {
type: 'boolean',
group: 'target',
describe: 'Compute targets for untracked changes'
})
.option('SHAs', {
type: 'string',
group: 'target',
describe: 'Compute targets changed between two SHAs'
})
.command(
'apps',
'Prints the names of the affected apps',
yargs => yargs,
() => affected(args)
)
.command(
'build',
'Builds the affected apps',
yargs => yargs,
() => affected(args)
)
.command(
'e2e',
'Tests the affected apps',
yargs => yargs,
() => affected(args)
)
.command(
'dep-graph',
'Generate a graph showing links between targets',
yargs =>
yargs
.describe('file', 'output file (e.g. --file=.vis/output.json)')
.demand('file')
.demand('output')
.choices('output', [
OutputType.json,
OutputType.dot,
OutputType.html
]),
_ => {
generateGraph(yargsParser(args));
}
)
.demandCommand()
)
.command(
'format <write|check>',
Expand Down Expand Up @@ -122,5 +128,5 @@ yargs
)
.help('help')
.version(false)
.strict()
.showHelpOnFail(true)
.demandCommand().argv; // .argv bootstraps the CLI creation;

0 comments on commit 487835e

Please sign in to comment.