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 11, 2018
1 parent 2a377fe commit 0655f5d
Show file tree
Hide file tree
Showing 6 changed files with 841 additions and 44 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"copy": "./scripts/copy.sh",
"test:schematics": "yarn linknpm fast && ./scripts/test_schematics.sh",
"test:nx": "yarn linknpm fast && ./scripts/test_nx.sh",
"test": "yarn linknpm fast && ./scripts/test_nx.sh && ./scripts/test_schematics.sh",
"test":
"yarn linknpm fast && ./scripts/test_nx.sh && ./scripts/test_schematics.sh",
"checkformat": "prettier \"{packages,e2e}/**/*.ts\" --list-different",
"publish_npm": "./scripts/publish.sh"
},
Expand All @@ -40,6 +41,7 @@
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"@types/prettier": "^1.10.0",
"@types/yargs": "^11.0.0",
"angular": "1.6.6",
"app-root-path": "^2.0.1",
"cosmiconfig": "^4.0.0",
Expand All @@ -65,15 +67,13 @@
"typescript": "2.6.2",
"viz.js": "^1.8.1",
"yargs-parser": "9.0.2",
"yargs": "^11.0.0",
"zone.js": "^0.8.19",
"fs-extra": "5.0.0"
},
"author": "Victor Savkin",
"license": "MIT",
"jest": {
"modulePathIgnorePatterns": [
"tmp",
"files"
]
"modulePathIgnorePatterns": ["tmp", "files"]
}
}
2 changes: 2 additions & 0 deletions packages/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
"dependencies": {
"@ngrx/schematics": "5.2.0",
"@schematics/angular": "0.4.6",
"@types/yargs": "^11.0.0",
"app-root-path": "^2.0.1",
"graphviz": "0.0.8",
"npm-run-all": "4.1.2",
"opn": "^5.3.0",
"semver": "5.4.1",
"tmp": "0.0.33",
"viz.js": "^1.8.1",
"yargs": "^11.0.0",
"yargs-parser": "9.0.2"
},
"devDependencies": {
Expand Down
152 changes: 114 additions & 38 deletions packages/schematics/src/command-line/nx.ts
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;
2 changes: 2 additions & 0 deletions packages/schematics/src/command-line/workspace-schematic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ class EngineHostHandlingWorkspaceSchematics implements EngineHost<any, any> {
readonly toolsHost: FileSystemEngineHost;
readonly defaultHost: NodeModulesEngineHost;

transformContext;

constructor(outDir: string) {
const transforms = validateOptionsWithSchema(
new CoreSchemaRegistry(standardFormats)
Expand Down
Loading

0 comments on commit 0655f5d

Please sign in to comment.