Skip to content

Commit 1709a51

Browse files
committed
feat(schematics): add first migration
1 parent 43cf5c0 commit 1709a51

File tree

13 files changed

+146
-53
lines changed

13 files changed

+146
-53
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"@ngrx/schematics": "7.4.0",
7878
"@nrwl/builders": "7.7.2",
7979
"@nrwl/schematics": "7.7.2",
80+
"@types/fs-extra": "^5.0.5",
8081
"@types/jasmine": "~3.3.0",
8182
"@types/jasminewd2": "~2.0.3",
8283
"@types/jest": "24.0.11",

packages/schematics/DEVELOP.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Getting Started With Schematics
2+
3+
This repository is a basic Schematic implementation that serves as a starting point to create and publish Schematics to NPM.
4+
5+
### Testing
6+
7+
To test locally, install `@angular-devkit/schematics-cli` globally and use the `schematics` command line tool. That tool acts the same as the `generate` command of the Angular CLI, but also has a debug mode.
8+
9+
Check the documentation with
10+
```bash
11+
schematics --help
12+
```
13+
14+
### Unit Testing
15+
16+
`npm run test` will run the unit tests, using Jasmine as a runner and test framework.
17+
18+
### Publishing
19+
20+
To publish, simply do:
21+
22+
```bash
23+
npm run build
24+
npm publish
25+
```
26+
27+
That's it!
28+
29+
30+
#### Futher Reading
31+
32+
- https://brianflove.com/2018/12/11/angular-schematics-tutorial/
33+
- `ng update --registry http://myregistry.org`: https://github.com/angular/angular-cli/issues/10624
34+
- ng update command: https://github.com/angular/angular-cli/blob/master/docs/specifications/update.md#library-developers

packages/schematics/README.md

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
1-
# Getting Started With Schematics
1+
@sparkles/schematics
2+
====================
23

3-
This repository is a basic Schematic implementation that serves as a starting point to create and publish Schematics to NPM.
4-
5-
### Testing
6-
7-
To test locally, install `@angular-devkit/schematics-cli` globally and use the `schematics` command line tool. That tool acts the same as the `generate` command of the Angular CLI, but also has a debug mode.
8-
9-
Check the documentation with
104
```bash
11-
schematics --help
12-
```
13-
14-
### Unit Testing
15-
16-
`npm run test` will run the unit tests, using Jasmine as a runner and test framework.
5+
$ ng add @sparkles/schematics
176

18-
### Publishing
19-
20-
To publish, simply do:
21-
22-
```bash
23-
npm run build
24-
npm publish
257
```
268

27-
That's it!
28-
29-
30-
#### Futher Reading
31-
32-
- https://brianflove.com/2018/12/11/angular-schematics-tutorial/
33-
- `ng update --registry http://myregistry.org`: https://github.com/angular/angular-cli/issues/10624
34-
- ng update command: https://github.com/angular/angular-cli/blob/master/docs/specifications/update.md#library-developers
9+
https://github.com/nrwl/nx/blob/master/packages/schematics/migrations/update-7-0-0/update-7-0-0.ts

packages/schematics/package.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,5 @@
2424
}
2525
},
2626
"dependencies": {
27-
"@angular-devkit/core": "^7.3.8",
28-
"@angular-devkit/schematics": "^7.3.8",
29-
"@types/jasmine": "^3.0.0",
30-
"@types/node": "^8.0.31",
31-
"jasmine": "^3.0.0",
32-
"typescript": "~3.2.2"
3327
}
3428
}

packages/schematics/src/collection.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
3+
"extends": "@nrwl/schematics",
34
"schematics": {
45
"ng-add": {
56
"description": "Adds Sparkles to the application",

packages/schematics/src/migration.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"migration-v1": {
55
"version": "1",
66
"description": "Updates Sparkles to v1",
7-
"factory": "./ng-update/index#updateToV1"
7+
"factory": "./ng-update/index#migrateToV1"
88
}
99
}
1010
}
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
1+
import { Rule, SchematicContext, Tree, externalSchematic, chain } from '@angular-devkit/schematics';
2+
3+
const printHelloWorld: Rule = (tree: Tree, context: SchematicContext) => {
4+
console.log("Hello world!");
5+
6+
return tree;
7+
};
28

39
// You don't have to export the function as default. You can also have more than one rule factory
410
// per file.
511
export default function schematics(_options: any): Rule {
6-
return (tree: Tree, _context: SchematicContext) => {
7-
console.log("Hello world!");
8-
9-
return tree;
10-
};
12+
return chain([
13+
printHelloWorld,
14+
externalSchematic('@schematics/update', '@angular/cli', {})
15+
]);
1116
}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
2-
import { updateJsonInTree } from '@nrwl/schematics/src/utils/ast-utils';
3-
4-
export function updateToV1(): Rule {
5-
return (tree: Tree, _context: SchematicContext) => {
6-
console.log("Updating to v1!");
7-
8-
return tree;
9-
};
10-
}
1+
export { migrateToV1 } from './migration-v1/migration-v1';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Rule, SchematicContext, Tree, externalSchematic, chain } from '@angular-devkit/schematics';
2+
import { readJsonInTree } from '../../utils/json';
3+
4+
const updateOrAddNx: Rule = (tree: Tree, context: SchematicContext) => {
5+
const packageJson = readJsonInTree(tree, 'package.json');
6+
7+
const inDevDependencies = packageJson['devDependencies'] ? packageJson['devDependencies']['@nrwl/schematics'] : null;
8+
const inDependencies = packageJson['devDependencies'] ? packageJson['devDependencies']['@nrwl/schematics'] : null;
9+
if (inDevDependencies || inDependencies) {
10+
const fromVersion = inDevDependencies || inDependencies;
11+
const toVersion = '7.7.0';
12+
context.logger.info(`Migrating @nrwl/schematis from already installed version=${fromVersion} to ${toVersion}`);
13+
14+
return externalSchematic('@schematics/update', 'update', {
15+
packages: ['@nrwl/schematics'],
16+
from: fromVersion,
17+
to: toVersion,
18+
force: true
19+
})
20+
} else {
21+
// ng add @nrwl/schematics
22+
return externalSchematic('@nrwl/schematics', 'ng-add', {});
23+
}
24+
};
25+
26+
export function migrateToV1(_options: any): Rule {
27+
return chain([
28+
updateOrAddNx
29+
]);
30+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import { ensureDirSync } from 'fs-extra';
4+
5+
export function writeToFile(filePath: string, str: string) {
6+
ensureDirSync(path.dirname(filePath));
7+
fs.writeFileSync(filePath, str);
8+
}

0 commit comments

Comments
 (0)