Skip to content

Commit

Permalink
feat: add ng update migration schematic
Browse files Browse the repository at this point in the history
  • Loading branch information
damyanpetev committed Jun 14, 2018
1 parent 7c13e96 commit 5bb5dc7
Show file tree
Hide file tree
Showing 8 changed files with 2,739 additions and 1,251 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
!/templates/angular/**/files/**/*
/templates/react/**/*.js
!/templates/react/**/files/**/*
/migrations/**/*.js.map
/migrations/**/*.js
/coverage
/.nyc_output
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"spec/**/*.js.map": true,
"spec/**/*.js": {"when": "$(basename).ts"},
"templates/**/*.js.map": true,
"templates/**/*.js": {"when": "$(basename).ts"}
"templates/**/*.js": {"when": "$(basename).ts"},
"migrations/**/*.js.map": true,
"migrations/**/*.js": {"when": "$(basename).ts"}
},
"tslint.exclude": [
"**/files/**/*.ts",
Expand Down
10 changes: 10 additions & 0 deletions migrations/migration-collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"migration-01": {
"version": "2.0.0-beta.0",
"description": "Updates Ignite UI for Angular project from 1.3",
"factory": "./update-2"
}
}
}
85 changes: 85 additions & 0 deletions migrations/update-2/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as path from "path";

// tslint:disable:no-implicit-dependencies
import { virtualFs } from "@angular-devkit/core";
import { EmptyTree } from "@angular-devkit/schematics";
// tslint:disable-next-line:no-submodule-imports
import { SchematicTestRunner, UnitTestTree } from "@angular-devkit/schematics/testing";

describe("Update 2.0.0", () => {
let appTree: UnitTestTree;
const schematicRunner = new SchematicTestRunner("ig-migrate", path.join(__dirname, "../migration-collection.json"));

beforeEach(() => {
appTree = new UnitTestTree(new EmptyTree());
});

it("should update router event rxjs subscription", done => {
appTree.create(
"/src/app/app.component.ts",
`import { Component, OnInit, ViewChild } from '@angular/core';
import { NavigationStart, Router } from '@angular/router';
import 'rxjs/add/operator/filter';
import { IgxNavigationDrawerComponent } from 'igniteui-angular';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild(IgxNavigationDrawerComponent) public navdrawer: IgxNavigationDrawerComponent;
constructor(private router: Router) {}
public ngOnInit(): void {
this.router.events
.filter((x) => x instanceof NavigationStart)
.subscribe((event: NavigationStart) => {
if (event.url !== '/' && !this.navdrawer.pin) {
this.navdrawer.close();
}
});
}
}
`
);

const tree = schematicRunner.runSchematic("migration-01", {}, appTree);
expect(tree.readContent("/src/app/app.component.ts"))
.toEqual(
`import { Component, OnInit, ViewChild } from '@angular/core';
import { NavigationStart, Router } from '@angular/router';
import { filter } from 'rxjs/operators';
import { IgxNavigationDrawerComponent } from 'igniteui-angular';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild(IgxNavigationDrawerComponent) public navdrawer: IgxNavigationDrawerComponent;
constructor(private router: Router) {}
public ngOnInit(): void {
this.router.events
.pipe(filter((x) => x instanceof NavigationStart))
.subscribe((event: NavigationStart) => {
if (event.url !== '/' && !this.navdrawer.pin) {
this.navdrawer.close();
}
});
}
}
`
);
done();
});

});
28 changes: 28 additions & 0 deletions migrations/update-2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as path from "path";
import * as ts from "typescript";

// tslint:disable:no-implicit-dependencies
import { Rule, SchematicContext, Tree } from "@angular-devkit/schematics";

export default function(): Rule {
return (host: Tree, context: SchematicContext) => {
context.logger.info("Updating project to Ignite UI CLI 2.0.0");

const appComponentPath = "src/app/app.component.ts";

if (host.exists(appComponentPath)) {
let content = host.read(appComponentPath).toString();
if (content.indexOf("rxjs/add/operator") !== -1) {
content = content.replace(
`import 'rxjs/add/operator/filter';`,
`import { filter } from 'rxjs/operators';`
);
content = content.replace(
`.filter((x) => x instanceof NavigationStart)`,
`.pipe(filter((x) => x instanceof NavigationStart))`
);
host.overwrite(appComponentPath, content);
}
}
};
}
Loading

0 comments on commit 5bb5dc7

Please sign in to comment.