diff --git a/modules/schematics/package.json b/modules/schematics/package.json index 8598bfd634..edbeaaf60c 100644 --- a/modules/schematics/package.json +++ b/modules/schematics/package.json @@ -25,7 +25,7 @@ "packageGroup": "NG_UPDATE_PACKAGE_GROUP" }, "peerDependencies": { - "@angular-devkit/core": "^0.5.0", - "@angular-devkit/schematics": "^0.5.0" + "@angular-devkit/core": "NG_DEVKIT_VERSION", + "@angular-devkit/schematics": "NG_DEVKIT_VERSION" } } diff --git a/modules/store/migrations/6_0_0/index.spec.ts b/modules/store/migrations/6_0_0/index.spec.ts new file mode 100644 index 0000000000..cd45c05835 --- /dev/null +++ b/modules/store/migrations/6_0_0/index.spec.ts @@ -0,0 +1,38 @@ +import { Tree } from '@angular-devkit/schematics'; +import { + SchematicTestRunner, + UnitTestTree, +} from '@angular-devkit/schematics/testing'; +import * as path from 'path'; + +const packagePath = '/package.json'; +const collectionPath = path.join(__dirname, '../migration.json'); + +describe('Migration 6_0_0', () => { + function setup(prefix: string) { + const tree = Tree.empty() as UnitTestTree; + tree.create( + packagePath, + `{ + "dependencies": { + "@ngrx/store": "${prefix}5.2.0" + } + }` + ); + + return { + tree, + runner: new SchematicTestRunner('schematics', collectionPath), + }; + } + + const prefixes = ['~', '^', '']; + prefixes.forEach(prefix => { + it(`should install version ${prefix}6.0.0`, () => { + const { runner, tree } = setup(prefix); + const newTree = runner.runSchematic('ngrx-store-migration-01', {}, tree); + const pkg = JSON.parse(newTree.readContent(packagePath)); + expect(pkg.dependencies['@ngrx/store']).toBe(`${prefix}6.0.0-beta.2`); + }); + }); +}); diff --git a/modules/store/migrations/6_0_0/index.ts b/modules/store/migrations/6_0_0/index.ts new file mode 100644 index 0000000000..0172780b20 --- /dev/null +++ b/modules/store/migrations/6_0_0/index.ts @@ -0,0 +1,44 @@ +import { + Rule, + SchematicContext, + Tree, + SchematicsException, + chain, +} from '@angular-devkit/schematics'; +import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; + +export default function(): Rule { + return (tree: Tree, context: SchematicContext) => { + const pkgPath = '/package.json'; + const buffer = tree.read(pkgPath); + if (buffer == null) { + throw new SchematicsException('Could not read package.json'); + } + const content = buffer.toString(); + const pkg = JSON.parse(content); + + if (pkg === null || typeof pkg !== 'object' || Array.isArray(pkg)) { + throw new SchematicsException('Error reading package.json'); + } + + if (!pkg.dependencies) { + pkg.dependencies = {}; + } + + if (pkg.dependencies['@ngrx/store']) { + const firstChar = pkg.dependencies['@ngrx/store'][0]; + const suffix = match(firstChar, '^') || match(firstChar, '~'); + + // TODO: remove beta + pkg.dependencies['@ngrx/store'] = `${suffix}6.0.0-beta.2`; + tree.overwrite(pkgPath, JSON.stringify(pkg, null, 2)); + context.addTask(new NodePackageInstallTask()); + } + + return tree; + }; +} + +function match(value: string, test: string) { + return value === test ? test : ''; +} diff --git a/modules/store/migrations/migration.json b/modules/store/migrations/migration.json new file mode 100644 index 0000000000..6a0dc2d52f --- /dev/null +++ b/modules/store/migrations/migration.json @@ -0,0 +1,11 @@ +{ + "$schema": + "../../../node_modules/@angular-devkit/schematics/collection-schema.json", + "schematics": { + "ngrx-store-migration-01": { + "description": "The road to v6", + "version": "5.2", + "factory": "./6_0_0/index" + } + } +} diff --git a/modules/store/package.json b/modules/store/package.json index 8a235b683d..7bf494f012 100644 --- a/modules/store/package.json +++ b/modules/store/package.json @@ -22,7 +22,8 @@ "rxjs": "RXJS_VERSION" }, "ng-update": { - "packageGroup": "NG_UPDATE_PACKAGE_GROUP" + "packageGroup": "NG_UPDATE_PACKAGE_GROUP", + "migrations": "NG_UPDATE_MIGRATIONS" }, "sideEffects": false } diff --git a/tools/defaults.bzl b/tools/defaults.bzl index e5fd57d382..1272a54cb5 100644 --- a/tools/defaults.bzl +++ b/tools/defaults.bzl @@ -7,6 +7,8 @@ load("@build_bazel_rules_nodejs//:defs.bzl", DEFAULT_TSCONFIG = "//:tsconfig.json" NG_VERSION = "^6.0.0" RXJS_VERSION = "^5.6.0-forward-compat.0 || ^6.0.0" +NG_DEVKIT_VERSION = "^0.6.0" +NG_UPDATE_MIGRATIONS = "./migrations/migration.json" NGRX_SCOPED_PACKAGES = ["@ngrx/%s" % p for p in [ "effects", @@ -26,7 +28,9 @@ PKG_GROUP_REPLACEMENTS = { "RXJS_VERSION": RXJS_VERSION, "\"NG_UPDATE_PACKAGE_GROUP\"": """[ %s - ]""" % ",\n ".join(["\"%s\"" % s for s in NGRX_SCOPED_PACKAGES]) + ]""" % ",\n ".join(["\"%s\"" % s for s in NGRX_SCOPED_PACKAGES]), + "NG_DEVKIT_VERSION": NG_DEVKIT_VERSION, + "NG_UPDATE_MIGRATIONS": NG_UPDATE_MIGRATIONS }