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 dfbb92c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 43 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
80 changes: 46 additions & 34 deletions packages/schematics/src/command-line/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,52 @@ const args = process.argv.slice(3);
yargs
.usage('Usage: $0 <command> [options>')
.command(
'affected <action>',
'affected <apps|build|e2e|dep-graph>',
'Compute affected applications',
yargs =>
yargs
.positional('action', {
choices: ['apps', 'build', 'e2e', 'dep-graph']
.option('files', {
type: 'array',
group: 'Compute apps affected by:',
describe: 'Specified files'
})
.option('uncommitted', {
group: 'Compute apps affected by:',
describe: 'Uncommitted changes'
})
.option('untracked', {
group: 'Compute apps affected by:',
describe: 'Untracked changes'
})
.option('SHAs', {
type: 'array',
group: 'Compute apps affected by:',
describe: 'Files changed between two SHAs'
})
.command('<action> -- <SHA1 SHA2>', 'Targets within a range of SHAs')
.command(
'<action> -- files="<file,list>"',
'Targets a comma delimited list of files'
'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('<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', [
Expand All @@ -55,17 +72,12 @@ yargs
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);
}
.conflicts({
files: ['uncommitted', 'untracked', 'SHAs'],
untracked: ['uncommitted', 'files', 'SHAs'],
SHAs: ['uncommitted', 'untracked', 'files'],
uncommitted: ['files', 'untracked', 'SHAs']
})
)
.command(
'format <write|check>',
Expand All @@ -81,19 +93,19 @@ yargs
.command(
// TODO: delete this after 1.0
'update <check|skip>',
'Migrate to the latest version of NX',
'Update to the latest version of Nx',
yargs =>
yargs
.describe('check', 'Check if there are migrations to run')
.describe('skip', 'Skip running the latest set of migrations'),
.describe('check', 'Check if there are Nx updates to run')
.describe('skip', 'Skip running the latest set of Nx updates'),
_ => {
update(args);
}
)
.alias('update', 'migrates') // TODO: Remove after 1.0
.command(
'lint [files..]',
'Line workspace or set of files',
'Lint workspace or set of files',
yargs => yargs,
_ => {
lint();
Expand All @@ -110,7 +122,7 @@ yargs
)
.command(
'workspace-schematic <name>',
'Generate a schematic `ng g <name>`',
'Generate a custom schematic that can be run via `ng g <name>`',
yargs =>
yargs.positional('name', {
type: 'string',
Expand All @@ -122,5 +134,5 @@ yargs
)
.help('help')
.version(false)
.strict()
.showHelpOnFail(true)
.demandCommand().argv; // .argv bootstraps the CLI creation;

0 comments on commit dfbb92c

Please sign in to comment.