-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(router-store): add v8 migration schematic (#1699)
- Loading branch information
1 parent
214316f
commit 0b794ce
Showing
19 changed files
with
415 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { | ||
SchematicTestRunner, | ||
UnitTestTree, | ||
} from '@angular-devkit/schematics/testing'; | ||
import * as path from 'path'; | ||
import { createPackageJson } from '../../../schematics-core/testing/create-package'; | ||
|
||
describe('Router Store Migration 8_0_0', () => { | ||
let appTree: UnitTestTree; | ||
const collectionPath = path.join(__dirname, '../migration.json'); | ||
const pkgName = 'router-store'; | ||
|
||
beforeEach(() => { | ||
appTree = new UnitTestTree(Tree.empty()); | ||
appTree.create( | ||
'/tsconfig.json', | ||
` | ||
{ | ||
"include": [**./*.ts"] | ||
} | ||
` | ||
); | ||
createPackageJson('', pkgName, appTree); | ||
}); | ||
|
||
it(`should import StoreRouterConnectingModule as StoreRouterConnectingModule.forRoot()`, () => { | ||
const contents = ` | ||
import { StoreRouterConnectingModule } from '@ngrx/router-store'; | ||
@NgModule({ | ||
imports: [ | ||
AuthModule, | ||
AppRoutingModule, | ||
/** | ||
* @ngrx/router-store keeps router state up-to-date in the store. | ||
*/ | ||
StoreRouterConnectingModule, | ||
CoreModule, | ||
], | ||
bootstrap: [AppComponent], | ||
}) | ||
export class AppModule {} | ||
`; | ||
const expected = ` | ||
import { StoreRouterConnectingModule } from '@ngrx/router-store'; | ||
@NgModule({ | ||
imports: [ | ||
AuthModule, | ||
AppRoutingModule, | ||
/** | ||
* @ngrx/router-store keeps router state up-to-date in the store. | ||
*/ | ||
StoreRouterConnectingModule.forRoot(), | ||
CoreModule, | ||
], | ||
bootstrap: [AppComponent], | ||
}) | ||
export class AppModule {} | ||
`; | ||
|
||
appTree.create('./app.module.ts', contents); | ||
const runner = new SchematicTestRunner('schematics', collectionPath); | ||
|
||
const newTree = runner.runSchematic( | ||
`ngrx-${pkgName}-migration-02`, | ||
{}, | ||
appTree | ||
); | ||
const file = newTree.readContent('app.module.ts'); | ||
|
||
expect(file).toBe(expected); | ||
}); | ||
|
||
it(`should not replace StoreRouterConnectingModule.forRoot()`, () => { | ||
const contents = ` | ||
import { StoreRouterConnectingModule } from '@ngrx/router-store'; | ||
@NgModule({ | ||
imports: [ | ||
AuthModule, | ||
AppRoutingModule, | ||
/** | ||
* @ngrx/router-store keeps router state up-to-date in the store. | ||
*/ | ||
StoreRouterConnectingModule.forRoot(), | ||
CoreModule, | ||
], | ||
bootstrap: [AppComponent], | ||
}) | ||
export class AppModule {} | ||
`; | ||
const expected = ` | ||
import { StoreRouterConnectingModule } from '@ngrx/router-store'; | ||
@NgModule({ | ||
imports: [ | ||
AuthModule, | ||
AppRoutingModule, | ||
/** | ||
* @ngrx/router-store keeps router state up-to-date in the store. | ||
*/ | ||
StoreRouterConnectingModule.forRoot(), | ||
CoreModule, | ||
], | ||
bootstrap: [AppComponent], | ||
}) | ||
export class AppModule {} | ||
`; | ||
|
||
appTree.create('./app.module.ts', contents); | ||
const runner = new SchematicTestRunner('schematics', collectionPath); | ||
|
||
const newTree = runner.runSchematic( | ||
`ngrx-${pkgName}-migration-02`, | ||
{}, | ||
appTree | ||
); | ||
const file = newTree.readContent('app.module.ts'); | ||
|
||
expect(file).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as ts from 'typescript'; | ||
import { Rule, chain, Tree } from '@angular-devkit/schematics'; | ||
import { | ||
ReplaceChange, | ||
createChangeRecorder, | ||
createReplaceChange, | ||
} from '@ngrx/router-store/schematics-core'; | ||
|
||
function updateRouterStoreImport(): Rule { | ||
return (tree: Tree) => { | ||
tree.visit(path => { | ||
if (!path.endsWith('.ts')) { | ||
return; | ||
} | ||
|
||
const sourceFile = ts.createSourceFile( | ||
path, | ||
tree.read(path)!.toString(), | ||
ts.ScriptTarget.Latest | ||
); | ||
|
||
if (sourceFile.isDeclarationFile) { | ||
return; | ||
} | ||
let changes: ReplaceChange[] = []; | ||
ts.forEachChild(sourceFile, function findDecorator(node) { | ||
if (!ts.isDecorator(node)) { | ||
ts.forEachChild(node, findDecorator); | ||
return; | ||
} | ||
|
||
ts.forEachChild(node, function findImports(node) { | ||
if ( | ||
ts.isPropertyAssignment(node) && | ||
ts.isArrayLiteralExpression(node.initializer) && | ||
ts.isIdentifier(node.name) && | ||
node.name.text === 'imports' | ||
) { | ||
node.initializer.elements | ||
.filter(ts.isIdentifier) | ||
.filter(element => element.text === 'StoreRouterConnectingModule') | ||
.forEach(element => { | ||
changes.push( | ||
createReplaceChange( | ||
sourceFile, | ||
path, | ||
element, | ||
'StoreRouterConnectingModule', | ||
'StoreRouterConnectingModule.forRoot()' | ||
) | ||
); | ||
}); | ||
} | ||
|
||
ts.forEachChild(node, findImports); | ||
}); | ||
}); | ||
|
||
if (changes.length < 1) { | ||
return; | ||
} | ||
|
||
const recorder = createChangeRecorder(tree, path, changes); | ||
tree.commitUpdate(recorder); | ||
}); | ||
}; | ||
} | ||
|
||
export default function(): Rule { | ||
return chain([updateRouterStoreImport()]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.