Skip to content

Commit

Permalink
feat(workspace-schematic): support list-schematics and interactive pr…
Browse files Browse the repository at this point in the history
…ompts
  • Loading branch information
JamesHenry authored and FrozenPandaz committed Jan 30, 2019
1 parent cc553ae commit d7a617e
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 80 deletions.
9 changes: 5 additions & 4 deletions docs/documentations/ngrx.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -258,7 +258,8 @@ CommentsLoaded,
CommentsLoadError
}

````
```

###### comments.selectors.ts

```ts
Expand All @@ -282,7 +283,7 @@ export const commentsQuery = {
getAllComments,
getSelectedComments
}
````
```

###### comments.reducer.ts

Expand Down
10 changes: 2 additions & 8 deletions docs/getting-started/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
74 changes: 30 additions & 44 deletions docs/getting-started/nx-and-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

Expand All @@ -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.
Expand All @@ -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": ["*"]
}
]
}
Expand All @@ -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 won’t scale. Nx uses code analysis to determine what needs to be rebuilt and retested.
Expand All @@ -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.

Expand All @@ -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.

8 changes: 4 additions & 4 deletions docs/getting-started/welcome.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
25 changes: 22 additions & 3 deletions e2e/schematics/command-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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(
Expand All @@ -175,15 +175,34 @@ 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(
'create libs/dir/mylib/src/lib/dir-mylib.module.ts'
);
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();
Expand Down
24 changes: 18 additions & 6 deletions packages/schematics/src/command-line/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading

0 comments on commit d7a617e

Please sign in to comment.