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 2a377fe commit ff878e5
Show file tree
Hide file tree
Showing 14 changed files with 913 additions and 140 deletions.
72 changes: 24 additions & 48 deletions e2e/schematics/command-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,14 @@ describe('Command line', () => {
newApp('app_before');
runCommand('mv apps/app-before apps/app-after');

try {
runCommand('npm run lint');
fail('Boom!');
} catch (e) {
const errorOutput = e.stderr.toString();
expect(errorOutput).toContain(
`Cannot find project 'app-before' in 'apps/app-before'`
);
expect(errorOutput).toContain(
`The 'apps/app-after/e2e/app.e2e-spec.ts' file doesn't belong to any project.`
);
}
const stdout = runCommand('npm run lint');

expect(stdout).toContain(
`Cannot find project 'app-before' in 'apps/app-before'`
);
expect(stdout).toContain(
`The 'apps/app-after/e2e/app.e2e-spec.ts' file doesn't belong to any project.`
);
});

it(
Expand Down Expand Up @@ -204,46 +200,26 @@ describe('Command line', () => {
`
);

try {
// this will group it by lib, so all three files will be "marked"
runCommand(
'npm run -s format:check -- --files="libs/mylib/index.ts" --libs-and-apps'
);
fail('boom');
} catch (e) {
expect(e.stdout.toString()).toContain('libs/mylib/index.ts');
expect(e.stdout.toString()).toContain('libs/mylib/src/mylib.module.ts');
}

try {
// this is a global run
runCommand('npm run -s format:check');
fail('boom');
} catch (e) {
expect(e.stdout.toString()).toContain('apps/myapp/src/main.ts');
expect(e.stdout.toString()).toContain(
'apps/myapp/src/app/app.module.ts'
);
expect(e.stdout.toString()).toContain(
'apps/myapp/src/app/app.component.ts'
);
}
let stdout = runCommand(
'npm run -s format:check -- --files="libs/mylib/index.ts" --libs-and-apps'
);
expect(stdout).toContain('libs/mylib/index.ts');
expect(stdout).toContain('libs/mylib/src/mylib.module.ts');

stdout = runCommand('npm run -s format:check');
expect(stdout).toContain('apps/myapp/src/main.ts');
expect(stdout).toContain('apps/myapp/src/app/app.module.ts');
expect(stdout).toContain('apps/myapp/src/app/app.component.ts');

runCommand(
'npm run format:write -- --files="apps/myapp/src/app/app.module.ts,apps/myapp/src/app/app.component.ts"'
);

try {
runCommand('npm run -s format:check');
fail('boom');
} catch (e) {
expect(e.stdout.toString()).toContain('apps/myapp/src/main.ts');
expect(e.stdout.toString()).not.toContain(
'apps/myapp/src/app/app.module.ts'
);
expect(e.stdout.toString()).not.toContain(
'apps/myapp/src/app/app.component.ts'
);
}
stdout = runCommand('npm run -s format:check');

expect(stdout).toContain('apps/myapp/src/main.ts');
expect(stdout).not.toContain('apps/myapp/src/app/app.module.ts');
expect(stdout).not.toContain('apps/myapp/src/app/app.component.ts');

runCommand('npm run format:write');
expect(runCommand('npm run -s format:check')).toEqual('');
Expand Down
12 changes: 8 additions & 4 deletions e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ export function newModule(name: string): string {
}

export function runCommand(command: string): string {
return execSync(command, {
cwd: `./tmp/${projectName}`,
stdio: ['pipe', 'pipe', 'pipe']
}).toString();
try {
return execSync(command, {
cwd: `./tmp/${projectName}`,
stdio: ['pipe', 'pipe', 'pipe']
}).toString();
} catch (e) {
return e.stdout.toString() + e.stderr.toString();
}
}

export function updateFile(f: string, content: string): void {
Expand Down
12 changes: 6 additions & 6 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 @@ -64,16 +66,14 @@
"tslint": "5.9.1",
"typescript": "2.6.2",
"viz.js": "^1.8.1",
"yargs-parser": "9.0.2",
"yargs-parser": "10.0.0",
"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: 1 addition & 1 deletion packages/bazel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"npm-run-all": "4.1.2",
"semver": "5.4.1",
"tmp": "0.0.33",
"yargs-parser": "9.0.2"
"yargs-parser": "10.0.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"affected:apps": "./node_modules/.bin/nx affected apps",
"affected:build": "./node_modules/.bin/nx affected build",
"affected:e2e": "./node_modules/.bin/nx affected e2e",
"affected:dep-graph": "./node_modules/.bin/nx affected dep-graph",
"format": "./node_modules/.bin/nx format write",
"format:write": "./node_modules/.bin/nx format write",
"format:check": "./node_modules/.bin/nx format check",
"affected:apps": "./node_modules/.bin/nx affected:apps",
"affected:build": "./node_modules/.bin/nx affected:build",
"affected:e2e": "./node_modules/.bin/nx affected:e2e",
"affected:dep-graph": "./node_modules/.bin/nx affected:dep-graph",
"format": "./node_modules/.bin/nx format:write",
"format:write": "./node_modules/.bin/nx format:write",
"format:check": "./node_modules/.bin/nx format:check",
"update": "./node_modules/.bin/nx update",
"update:check": "./node_modules/.bin/nx update check",
"update:skip": "./node_modules/.bin/nx update skip",
"update:check": "./node_modules/.bin/nx update:check",
"update:skip": "./node_modules/.bin/nx update:skip",
"dep-graph": "./node_modules/.bin/nx dep-graph",
"postinstall": "ngc -p ngc.tsconfig.json"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@
"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-parser": "9.0.2"
"yargs-parser": "10.0.0",
"yargs": "^11.0.0"
},
"devDependencies": {
"@angular-devkit/schematics": "0.4.6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
"test": "ng test",
"lint": "./node_modules/.bin/nx lint && ng lint",
"e2e": "ng e2e",
"affected:apps": "./node_modules/.bin/nx affected apps",
"affected:build": "./node_modules/.bin/nx affected build",
"affected:e2e": "./node_modules/.bin/nx affected e2e",
"affected:dep-graph": "./node_modules/.bin/nx affected dep-graph",
"format": "./node_modules/.bin/nx format write",
"format:write": "./node_modules/.bin/nx format write",
"format:check": "./node_modules/.bin/nx format check",
"affected:apps": "./node_modules/.bin/nx affected:apps",
"affected:build": "./node_modules/.bin/nx affected:build",
"affected:e2e": "./node_modules/.bin/nx affected:e2e",
"affected:dep-graph": "./node_modules/.bin/nx affected:dep-graph",
"format": "./node_modules/.bin/nx format:write",
"format:write": "./node_modules/.bin/nx format:write",
"format:check": "./node_modules/.bin/nx format:check",
"update": "./node_modules/.bin/nx update",
"update:check": "./node_modules/.bin/nx update check",
"update:skip": "./node_modules/.bin/nx update skip",
"update:check": "./node_modules/.bin/nx update:check",
"update:skip": "./node_modules/.bin/nx update:skip",
"workspace-schematic": "./node_modules/.bin/nx workspace-schematic",
"dep-graph": "./node_modules/.bin/nx dep-graph",
"postinstall": "./node_modules/.bin/nx postinstall"
Expand Down
18 changes: 9 additions & 9 deletions packages/schematics/src/collection/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ function updatePackageJson() {
}

packageJson.scripts['affected:apps'] =
'./node_modules/.bin/nx affected apps';
'./node_modules/.bin/nx affected:apps';
packageJson.scripts['affected:build'] =
'./node_modules/.bin/nx affected build';
packageJson.scripts['affected:e2e'] = './node_modules/.bin/nx affected e2e';
'./node_modules/.bin/nx affected:build';
packageJson.scripts['affected:e2e'] = './node_modules/.bin/nx affected:e2e';

packageJson.scripts['affected:dep-graph'] =
'./node_modules/.bin/nx affected dep-graph';
'./node_modules/.bin/nx affected:dep-graph';

packageJson.scripts['format'] = './node_modules/.bin/nx format write';
packageJson.scripts['format:write'] = './node_modules/.bin/nx format write';
packageJson.scripts['format:check'] = './node_modules/.bin/nx format check';
packageJson.scripts['format'] = './node_modules/.bin/nx format:write';
packageJson.scripts['format:write'] = './node_modules/.bin/nx format:write';
packageJson.scripts['format:check'] = './node_modules/.bin/nx format:check';

packageJson.scripts['update'] = './node_modules/.bin/nx update';
packageJson.scripts['update:check'] = './node_modules/.bin/nx update check';
packageJson.scripts['update:skip'] = './node_modules/.bin/nx update skip';
packageJson.scripts['update:check'] = './node_modules/.bin/nx update:check';
packageJson.scripts['update:skip'] = './node_modules/.bin/nx update:skip';

packageJson.scripts['lint'] = './node_modules/.bin/nx lint && ng lint';

Expand Down
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
5 changes: 3 additions & 2 deletions packages/schematics/src/command-line/dep-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Dependency,
DependencyType
} from './affected-apps';
import * as yargs from 'yargs';

import { readCliConfig, getProjectNodes } from './shared';
import * as path from 'path';
Expand Down Expand Up @@ -62,11 +63,11 @@ export enum OutputType {
'dot' = 'dot'
}

export type UserOptions = {
export interface UserOptions extends yargs.Arguments {
file?: string;
output?: string;
files?: string;
};
}

type ParsedUserOptions = {
isFilePresent?: boolean;
Expand Down
Loading

0 comments on commit ff878e5

Please sign in to comment.