feat(workspace-schematic): support list-schematics and interactive prompts

This commit is contained in:
James Henry 2019-01-28 17:50:56 -05:00 committed by Jason Jean
parent cc553ae687
commit d7a617e43c
8 changed files with 177 additions and 80 deletions

View File

@ -225,9 +225,9 @@ The files generated are shown below and include placeholders for the _comments_
- [../app.module.ts](#appmodulets) or [../comments-state.module.ts](#commentsstatemodulets)
<br/>
###### comments.actions.ts
```ts
import {Action} from "@ngrx/store";
@ -258,7 +258,8 @@ CommentsLoaded,
CommentsLoadError
}
````
```
###### comments.selectors.ts
```ts
@ -282,7 +283,7 @@ export const commentsQuery = {
getAllComments,
getSelectedComments
}
````
```
###### comments.reducer.ts

View File

@ -32,12 +32,11 @@ yarn global add @nrwl/schematics
> If you want to work with the version still in development you can use `@nrwl/schematics@next` as the package to install.
## Nx Workspace
## Nx Workspace
### Creating an Nx Workspace
To create an Nx workspace, run:
To create an Nx workspace, run:
```bash
ng new myworkspace --collection=@nrw/schematics
@ -51,7 +50,6 @@ create-nx-workspace myworkspacename
This command still runs `ng new` under the hood, but it does it in a sandboxed environment, and, as a result, never fails.
### Adding to an Existing Angular CLI workspace
If you already have a regular Angular CLI project, you can add Nx power-ups by running:
@ -60,8 +58,6 @@ If you already have a regular Angular CLI project, you can add Nx power-ups by r
ng add @nrwl/schematics
```
## Creating First Application
Unlike the CLI, an Nx workspace starts blank. There are no applications to build, serve, and test. To create one run:
@ -104,12 +100,10 @@ nx.json
All the files that the CLI would have in a new project are still here, just in a different folder structure which makes it easier to create more apps and libs in the future.
## Serving Application
Run `ng serve myapp` to serve the newly generated application!
## Use Angular Console
You can also create a new Nx project using Angular Console--UI for the CLI:

View File

@ -2,25 +2,22 @@
Nx **is not** a replacement for Angular CLI. It's a collection of Angular CLI power-ups (schematics and builders) that transform the CLI into a powerful tool for full-stack development.
* **An Nx workspace is an Angular CLI workspace.**
* You run same `ng build`, `ng serve` commands.
* You configure your projects in `angular.json`.
* Anything you can do in a standard Angular CLI project, you can also do in an Nx workspace.
- **An Nx workspace is an Angular CLI workspace.**
- You run same `ng build`, `ng serve` commands.
- You configure your projects in `angular.json`.
- Anything you can do in a standard Angular CLI project, you can also do in an Nx workspace.
## How?
Angular CLI is extensible. Nx extends what the CLI generates (schematics) and how the CLI runs commands (builders).
What does Nx add?
Angular CLI is extensible. Nx extends what the CLI generates (schematics) and how the CLI runs commands (builders).
What does Nx add?
## Full-Stack Development
With Nx, you can build a backend application next to your Angular application in the same repository. The backend and the frontend can share code. You can connect them to enable a fantastic development experience.
*How do you do it?*
_How do you do it?_
First, generate a node application.
@ -40,24 +37,20 @@ Now, add the right proxy configuration:
```json
{
"/graphql": {
"target": "http://localhost:3333",
"secure": false
}
"/graphql": {
"target": "http://localhost:3333",
"secure": false
}
}
```
Finally, you can run `ng serve backend` and `ng serve frontend`. There is a lot more to full-stack development in Nx, which we will cover the the guides.
## Use effective development practices pioneered at Google
Using Nx, you can implement monorepo-style development--an approach popularized by Google and used by many tech companies today (Facebook, Uber, Twitter, etc..).
*Doesn't Angular CLI support having multiple projects in the same workspace?*
_Doesn't Angular CLI support having multiple projects in the same workspace?_
Yes, starting with Angular CLI 6 you can add different types of projects to a single workspace (by default you can add applications and libraries). This is great, but is not sufficient to enable the monorepo-style development. Nx adds an extra layer of tooling to make this possible.
@ -67,7 +60,6 @@ To be able to support the monorepo-style development, the tools must know how di
![Dependency Diagram](./dep-graph.png)
### Imposing Constraints on the Dependency Graph
If you partition your code into well-defined cohesive units, even a small organization will end up with a dozen apps and dozens or hundreds of libs. If all of them can depend on each other freely, chaos will ensue and the workspace will become unmanageable.
@ -82,21 +74,21 @@ For instance, with this configuration, when we import private client code from t
{
"allow": [],
"depConstraints": [
{
"sourceTag": "shared",
{
"sourceTag": "shared",
"onlyDependOnLibsWithTags": ["shared"]
},
{
"sourceTag": "admin",
"onlyDependOnLibsWithTags": ["shared", "admin" ]
{
"sourceTag": "admin",
"onlyDependOnLibsWithTags": ["shared", "admin" ]
},
{
"sourceTag": "client",
"onlyDependOnLibsWithTags": ["shared", "client" ]
{
"sourceTag": "client",
"onlyDependOnLibsWithTags": ["shared", "client" ]
},
{
"sourceTag": "*",
"onlyDependOnLibsWithTags": ["*"]
{
"sourceTag": "*",
"onlyDependOnLibsWithTags": ["*"]
}
]
}
@ -105,7 +97,6 @@ For instance, with this configuration, when we import private client code from t
![Lint Error](./lint-error.png)
### Rebuilding and Retesting What is Affected
To be productive in a monorepo, you need to be able to check that your change is safe, and rebuilding and retesting everything on every change wont scale. Nx uses code analysis to determine what needs to be rebuilt and retested.
@ -120,26 +111,23 @@ yarn affected:e2e --base=master # reruns e2e tests for all the projects affected
Nx will topologically sort the projects, and will run what it can in parallel.
### Angular CLI = Code Collocation, Nx = Monorepo
Imagine you have a regular Angular CLI workspace containing tens projects, where each project has its own suite of e2e tests. The CLI doesnt't know how the projects depend on each other, so the only way for you to make sure your PR works is to rebuild and retest all ten projects. This isn't great.
First, this makes the CI expensive--10 times more expensive in the worse case scenario. Second, e2e tests can be flaky. If you always rerun all the tests in the repo, some tests will be bound to fail for the reasons unrelated to your change.
First, this makes the CI expensive--10 times more expensive in the worse case scenario. Second, e2e tests can be flaky. If you always rerun all the tests in the repo, some tests will be bound to fail for the reasons unrelated to your change.
> Only the projects affected by your PR should be rebuilt and retested.
This is a hard requirement for monorepo-style development. Nx implements it.
### Automation
In addition to using the monorepo, Google is also know for its use of automation. Nx adds powerful capabilities helping your team promote best practices and ensure consistency.
In addition to using the monorepo, Google is also know for its use of automation. Nx adds powerful capabilities helping your team promote best practices and ensure consistency.
## Use Innovative Tools
Tools like Apollo, Cypress, Jest, Prettier, and NestJS have gained a lot of popularity.
Tools like Apollo, Cypress, Jest, Prettier, and NestJS have gained a lot of popularity.
It's not the case that Apollo is always better than REST or Cypress is always better than Protractor. There are tradeoffs. But in many situations, for many projects, these innovative tools offer a lot of advantages.
@ -164,13 +152,11 @@ Nx is not a replacement for the CLI. It's a set of Angular CLI power-ups.
With Nx, you can:
* Do everything you can do using the CLI
* Build full-stack applications using Angular and NestJS
* Use scalable development practices such as monorepos
* Use innovative tools like Cypress and Jest
- Do everything you can do using the CLI
- Build full-stack applications using Angular and NestJS
- Use scalable development practices such as monorepos
- Use innovative tools like Cypress and Jest
### A la carte
Most importantly, you can use these power-ups a la carte. Just want to build a single Angular application using Cypress? Nx is still an excellent choice for that.

View File

@ -4,9 +4,9 @@ Nx is a set of Angular CLI power-ups that transform the CLI into a powerful tool
With Nx, you can:
* Build full-stack applications using Angular and NestJS
* Use effective development practices pioneered at Google
* Use innovative tools like Cypress and Jest
- Build full-stack applications using Angular and NestJS
- Use effective development practices pioneered at Google
- Use innovative tools like Cypress and Jest
## Getting Started
@ -20,8 +20,8 @@ With Nx, you can:
- [API docs](apidocs.md)
## Community
- [Books, talks, and blog posts about Nx](resources.md)
- [Books, talks, and blog posts about Nx](resources.md)
## Contributing

View File

@ -142,7 +142,7 @@ describe('Command line', () => {
it('should support workspace-specific schematics', () => {
newProject();
runCLI('g workspace-schematic custom');
runCLI('g workspace-schematic custom -- --noInteractive');
checkFilesExist(
'tools/schematics/custom/index.ts',
'tools/schematics/custom/schema.json'
@ -165,7 +165,7 @@ describe('Command line', () => {
);
const dryRunOutput = runCommand(
'npm run workspace-schematic custom mylib -- --directory=dir -d'
'npm run workspace-schematic custom mylib -- --noInteractive --directory=dir -d'
);
expect(exists('libs/dir/mylib/src/index.ts')).toEqual(false);
expect(dryRunOutput).toContain(
@ -175,7 +175,7 @@ describe('Command line', () => {
expect(dryRunOutput).toContain('update nx.json');
const output = runCommand(
'npm run workspace-schematic custom mylib -- --directory=dir'
'npm run workspace-schematic custom mylib -- --no-interactive --directory=dir'
);
checkFilesExist('libs/dir/mylib/src/index.ts');
expect(output).toContain(
@ -183,7 +183,26 @@ describe('Command line', () => {
);
expect(output).toContain('update angular.json');
expect(output).toContain('update nx.json');
runCLI('g workspace-schematic another -- --noInteractive');
const listSchematicsOutput = runCommand(
'npm run workspace-schematic -- --list-schematics'
);
expect(listSchematicsOutput).toContain(
'nx workspace-schematic "--list-schematics"'
);
expect(listSchematicsOutput).toContain('custom');
expect(listSchematicsOutput).toContain('another');
const promptOutput = runCommand(
'npm run workspace-schematic custom mylib2 --'
);
expect(promptOutput).toContain(
'In which directory should the library be generated?'
);
}, 1000000);
describe('dep-graph', () => {
beforeEach(() => {
newProject();

View File

@ -123,13 +123,25 @@ yargs
.command('update', 'Update workspace', noop, _ => update([]))
.alias('update', 'migrates') // TODO: Remove after 1.0
.command(
'workspace-schematic <name>',
'workspace-schematic [name]',
'Runs a workspace schematic from the tools/schematics directory',
yargs =>
yargs.positional('name', {
type: 'string',
describe: 'The name of your schematic`'
}),
yargs => {
yargs.option('list-schematics', {
describe: 'List the available workspace-schematics',
type: 'boolean'
});
/**
* Don't require `name` if only listing available
* schematics
*/
if (yargs.argv.listSchematics !== true) {
yargs.demandOption(['name']).positional('name', {
type: 'string',
describe: 'The name of your schematic`'
});
}
return yargs;
},
() => workspaceSchematic(process.argv.slice(3))
)
.help('help')

View File

@ -1,24 +1,47 @@
import {
JsonObject,
logging,
normalize,
schema,
virtualFs
} from '@angular-devkit/core';
import { createConsoleLogger, NodeJsSyncHost } from '@angular-devkit/core/node';
import {
SchematicEngine,
UnsuccessfulWorkflowExecution
} from '@angular-devkit/schematics';
import {
NodeModulesEngineHost,
NodeWorkflow
} from '@angular-devkit/schematics/tools';
import * as appRoot from 'app-root-path';
import { execSync } from 'child_process';
import * as fs from 'fs';
import { readFileSync, writeFileSync, statSync } from 'fs';
import * as path from 'path';
import { readFileSync, statSync, writeFileSync } from 'fs';
import { copySync, removeSync } from 'fs-extra';
import { NodeWorkflow } from '@angular-devkit/schematics/tools';
import { UnsuccessfulWorkflowExecution } from '@angular-devkit/schematics';
import * as inquirer from 'inquirer';
import * as path from 'path';
import * as yargsParser from 'yargs-parser';
import * as appRoot from 'app-root-path';
import { virtualFs, normalize, JsonObject } from '@angular-devkit/core';
import { NodeJsSyncHost, createConsoleLogger } from '@angular-devkit/core/node';
const rootDirectory = appRoot.path;
export function workspaceSchematic(args: string[]) {
const parsedArgs = parseOptions(args);
const logger = createConsoleLogger(
parsedArgs['verbose'],
process.stdout,
process.stderr
);
const outDir = compileTools();
if (parsedArgs['list-schematics']) {
return listSchematics(
path.join(outDir, 'workspace-schematics.json'),
logger
);
}
const schematicName = args[0];
const workflow = createWorkflow(parsedArgs.dryRun);
executeSchematic(schematicName, parsedArgs, workflow, outDir);
executeSchematic(schematicName, parsedArgs, workflow, outDir, logger);
}
// compile tools
@ -98,12 +121,70 @@ function createWorkflow(dryRun: boolean) {
});
}
function listSchematics(collectionName: string, logger: logging.Logger) {
try {
const engineHost = new NodeModulesEngineHost();
const engine = new SchematicEngine(engineHost);
const collection = engine.createCollection(collectionName);
logger.info(engine.listSchematicNames(collection).join('\n'));
} catch (error) {
logger.fatal(error.message);
return 1;
}
return 0;
}
function createPromptProvider(): schema.PromptProvider {
return (definitions: Array<schema.PromptDefinition>) => {
const questions: inquirer.Questions = definitions.map(definition => {
const question: inquirer.Question = {
name: definition.id,
message: definition.message,
default: definition.default
};
const validator = definition.validator;
if (validator) {
question.validate = input => validator(input);
}
switch (definition.type) {
case 'confirmation':
return { ...question, type: 'confirm' };
case 'list':
return {
...question,
type: 'list',
choices:
definition.items &&
definition.items.map(item => {
if (typeof item == 'string') {
return item;
} else {
return {
name: item.label,
value: item.value
};
}
})
};
default:
return { ...question, type: definition.type };
}
});
return inquirer.prompt(questions);
};
}
// execute schematic
async function executeSchematic(
schematicName: string,
options: { [p: string]: any },
workflow: NodeWorkflow,
outDir: string
outDir: string,
logger: logging.Logger
) {
let nothingDone = true;
workflow.reporter.subscribe((event: any) => {
@ -146,7 +227,11 @@ async function executeSchematic(
}
});
delete options._;
const logger = createConsoleLogger(true, process.stdout, process.stderr);
// Add support for interactive prompts
if (!options.noInteractive && options.interactive !== false) {
workflow.registry.usePromptProvider(createPromptProvider());
}
try {
await workflow

0
scripts/format.sh Normal file → Executable file
View File