-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schematics): Generate --help for every CLI function.
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
Showing
6 changed files
with
841 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,125 @@ | ||
#!/usr/bin/env node | ||
import * as yargsParser from 'yargs-parser'; | ||
import * as yargs from 'yargs'; | ||
|
||
import * as yargsParser from 'yargs-parser'; | ||
import { affected } from './affected'; | ||
import { format } from './format'; | ||
import { update } from './update'; | ||
import { patchNg } from './patch-ng'; | ||
import { lint } from './lint'; | ||
import { workspaceSchematic } from './workspace-schematic'; | ||
import { generateGraph } from './dep-graph'; | ||
import { generateGraph, OutputType } from './dep-graph'; | ||
|
||
const processedArgs = yargsParser(process.argv, { | ||
alias: { | ||
app: ['a'] | ||
}, | ||
string: ['app'] | ||
}); | ||
const command = processedArgs._[2]; | ||
const args = process.argv.slice(3); | ||
|
||
switch (command) { | ||
case 'affected': | ||
affected(args); | ||
break; | ||
case 'dep-graph': | ||
generateGraph(yargsParser(args)); | ||
break; | ||
case 'format': | ||
format(args); | ||
break; | ||
case 'migrate': // TODO: delete this after 1.0 | ||
update(args); | ||
break; | ||
case 'update': | ||
update(args); | ||
break; | ||
case 'lint': | ||
lint(); | ||
break; | ||
case 'postinstall': | ||
patchNg(); | ||
update(['check']); | ||
break; | ||
case 'workspace-schematic': | ||
workspaceSchematic(args); | ||
break; | ||
default: | ||
throw new Error(`Unrecognized command '${command}'`); | ||
} | ||
yargs | ||
.usage('Usage: $0 <command> [options>') | ||
.command( | ||
'affected', | ||
'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( | ||
'format <write|check>', | ||
'Format workspace', | ||
yargs => | ||
yargs | ||
.describe('write', 'Overwrite non-formatted files') | ||
.describe('check', 'Check if files need formatting'), | ||
_ => { | ||
format(args); | ||
} | ||
) | ||
.command( | ||
// TODO: delete this after 1.0 | ||
'update <check|skip>', | ||
'Migrate 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'), | ||
_ => { | ||
update(args); | ||
} | ||
) | ||
.alias('update', 'migrates') // TODO: Remove after 1.0 | ||
.command( | ||
'lint [files..]', | ||
'Line workspace or set of files', | ||
yargs => yargs, | ||
_ => { | ||
lint(); | ||
} | ||
) | ||
.command( | ||
'postinstall', | ||
false, | ||
yargs => yargs.help(false), | ||
_ => { | ||
patchNg(); | ||
update(['check']); | ||
} | ||
) | ||
.command( | ||
'workspace-schematic <name>', | ||
'Generate a schematic `ng g <name>`', | ||
yargs => | ||
yargs.positional('name', { | ||
type: 'string', | ||
describe: 'The name of your schematic`' | ||
}), | ||
_ => { | ||
workspaceSchematic(args); | ||
} | ||
) | ||
.help('help') | ||
.version(false) | ||
.demandCommand().argv; // .argv bootstraps the CLI creation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.