Skip to content

Commit

Permalink
feat(Store): Add support ng update (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored and brandonroberts committed May 11, 2018
1 parent 4c4e6a9 commit 5b4f067
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 4 deletions.
4 changes: 2 additions & 2 deletions modules/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
38 changes: 38 additions & 0 deletions modules/store/migrations/6_0_0/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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`);
});
});
});
44 changes: 44 additions & 0 deletions modules/store/migrations/6_0_0/index.ts
Original file line number Diff line number Diff line change
@@ -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 : '';
}
11 changes: 11 additions & 0 deletions modules/store/migrations/migration.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
3 changes: 2 additions & 1 deletion modules/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion tools/defaults.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}


Expand Down

0 comments on commit 5b4f067

Please sign in to comment.