Skip to content

Commit 5b4f067

Browse files
timdeschryverbrandonroberts
authored andcommitted
feat(Store): Add support ng update (#1032)
1 parent 4c4e6a9 commit 5b4f067

File tree

6 files changed

+102
-4
lines changed

6 files changed

+102
-4
lines changed

modules/schematics/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
2626
},
2727
"peerDependencies": {
28-
"@angular-devkit/core": "^0.5.0",
29-
"@angular-devkit/schematics": "^0.5.0"
28+
"@angular-devkit/core": "NG_DEVKIT_VERSION",
29+
"@angular-devkit/schematics": "NG_DEVKIT_VERSION"
3030
}
3131
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Tree } from '@angular-devkit/schematics';
2+
import {
3+
SchematicTestRunner,
4+
UnitTestTree,
5+
} from '@angular-devkit/schematics/testing';
6+
import * as path from 'path';
7+
8+
const packagePath = '/package.json';
9+
const collectionPath = path.join(__dirname, '../migration.json');
10+
11+
describe('Migration 6_0_0', () => {
12+
function setup(prefix: string) {
13+
const tree = Tree.empty() as UnitTestTree;
14+
tree.create(
15+
packagePath,
16+
`{
17+
"dependencies": {
18+
"@ngrx/store": "${prefix}5.2.0"
19+
}
20+
}`
21+
);
22+
23+
return {
24+
tree,
25+
runner: new SchematicTestRunner('schematics', collectionPath),
26+
};
27+
}
28+
29+
const prefixes = ['~', '^', ''];
30+
prefixes.forEach(prefix => {
31+
it(`should install version ${prefix}6.0.0`, () => {
32+
const { runner, tree } = setup(prefix);
33+
const newTree = runner.runSchematic('ngrx-store-migration-01', {}, tree);
34+
const pkg = JSON.parse(newTree.readContent(packagePath));
35+
expect(pkg.dependencies['@ngrx/store']).toBe(`${prefix}6.0.0-beta.2`);
36+
});
37+
});
38+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
Rule,
3+
SchematicContext,
4+
Tree,
5+
SchematicsException,
6+
chain,
7+
} from '@angular-devkit/schematics';
8+
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
9+
10+
export default function(): Rule {
11+
return (tree: Tree, context: SchematicContext) => {
12+
const pkgPath = '/package.json';
13+
const buffer = tree.read(pkgPath);
14+
if (buffer == null) {
15+
throw new SchematicsException('Could not read package.json');
16+
}
17+
const content = buffer.toString();
18+
const pkg = JSON.parse(content);
19+
20+
if (pkg === null || typeof pkg !== 'object' || Array.isArray(pkg)) {
21+
throw new SchematicsException('Error reading package.json');
22+
}
23+
24+
if (!pkg.dependencies) {
25+
pkg.dependencies = {};
26+
}
27+
28+
if (pkg.dependencies['@ngrx/store']) {
29+
const firstChar = pkg.dependencies['@ngrx/store'][0];
30+
const suffix = match(firstChar, '^') || match(firstChar, '~');
31+
32+
// TODO: remove beta
33+
pkg.dependencies['@ngrx/store'] = `${suffix}6.0.0-beta.2`;
34+
tree.overwrite(pkgPath, JSON.stringify(pkg, null, 2));
35+
context.addTask(new NodePackageInstallTask());
36+
}
37+
38+
return tree;
39+
};
40+
}
41+
42+
function match(value: string, test: string) {
43+
return value === test ? test : '';
44+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema":
3+
"../../../node_modules/@angular-devkit/schematics/collection-schema.json",
4+
"schematics": {
5+
"ngrx-store-migration-01": {
6+
"description": "The road to v6",
7+
"version": "5.2",
8+
"factory": "./6_0_0/index"
9+
}
10+
}
11+
}

modules/store/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"rxjs": "RXJS_VERSION"
2323
},
2424
"ng-update": {
25-
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
25+
"packageGroup": "NG_UPDATE_PACKAGE_GROUP",
26+
"migrations": "NG_UPDATE_MIGRATIONS"
2627
},
2728
"sideEffects": false
2829
}

tools/defaults.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ load("@build_bazel_rules_nodejs//:defs.bzl",
77
DEFAULT_TSCONFIG = "//:tsconfig.json"
88
NG_VERSION = "^6.0.0"
99
RXJS_VERSION = "^5.6.0-forward-compat.0 || ^6.0.0"
10+
NG_DEVKIT_VERSION = "^0.6.0"
11+
NG_UPDATE_MIGRATIONS = "./migrations/migration.json"
1012

1113
NGRX_SCOPED_PACKAGES = ["@ngrx/%s" % p for p in [
1214
"effects",
@@ -26,7 +28,9 @@ PKG_GROUP_REPLACEMENTS = {
2628
"RXJS_VERSION": RXJS_VERSION,
2729
"\"NG_UPDATE_PACKAGE_GROUP\"": """[
2830
%s
29-
]""" % ",\n ".join(["\"%s\"" % s for s in NGRX_SCOPED_PACKAGES])
31+
]""" % ",\n ".join(["\"%s\"" % s for s in NGRX_SCOPED_PACKAGES]),
32+
"NG_DEVKIT_VERSION": NG_DEVKIT_VERSION,
33+
"NG_UPDATE_MIGRATIONS": NG_UPDATE_MIGRATIONS
3034
}
3135

3236

0 commit comments

Comments
 (0)