11import * as ts from 'typescript' ;
2- import { Rule , chain , Tree } from '@angular-devkit/schematics' ;
2+ import { tags , logging } from '@angular-devkit/core' ;
3+ import {
4+ Rule ,
5+ chain ,
6+ Tree ,
7+ SchematicContext ,
8+ } from '@angular-devkit/schematics' ;
39import {
410 ReplaceChange ,
511 createReplaceChange ,
@@ -10,7 +16,7 @@ import {
1016const META_REDUCERS = 'META_REDUCERS' ;
1117
1218function updateMetaReducersToken ( ) : Rule {
13- return ( tree : Tree ) => {
19+ return ( tree : Tree , context : SchematicContext ) => {
1420 visitTSSourceFiles ( tree , sourceFile => {
1521 const createChange = ( node : ts . Node ) =>
1622 createReplaceChange (
@@ -22,7 +28,11 @@ function updateMetaReducersToken(): Rule {
2228
2329 const changes : ReplaceChange [ ] = [ ] ;
2430 changes . push (
25- ...findMetaReducersImportStatements ( sourceFile , createChange )
31+ ...findMetaReducersImportStatements (
32+ sourceFile ,
33+ createChange ,
34+ context . logger
35+ )
2636 ) ;
2737 changes . push ( ...findMetaReducersAssignment ( sourceFile , createChange ) ) ;
2838
@@ -37,11 +47,22 @@ export default function(): Rule {
3747
3848function findMetaReducersImportStatements (
3949 sourceFile : ts . SourceFile ,
40- createChange : ( node : ts . Node ) => ReplaceChange
50+ createChange : ( node : ts . Node ) => ReplaceChange ,
51+ logger : logging . LoggerApi
4152) {
53+ let canRunSchematics = false ;
54+
4255 const metaReducerImports = sourceFile . statements
4356 . filter ( ts . isImportDeclaration )
4457 . filter ( isNgRxStoreImport )
58+ . filter ( p => {
59+ canRunSchematics = Boolean (
60+ p . importClause &&
61+ p . importClause . namedBindings &&
62+ ( p . importClause ! . namedBindings ! as ts . NamedImports ) . elements
63+ ) ;
64+ return canRunSchematics ;
65+ } )
4566 . map ( p =>
4667 ( p . importClause ! . namedBindings ! as ts . NamedImports ) . elements . filter (
4768 isMetaReducersImportSpecifier
@@ -50,6 +71,15 @@ function findMetaReducersImportStatements(
5071 . reduce ( ( imports , curr ) => imports . concat ( curr ) , [ ] ) ;
5172
5273 const changes = metaReducerImports . map ( createChange ) ;
74+ if ( ! canRunSchematics && changes . length === 0 ) {
75+ logger . info ( tags . stripIndent `
76+ NgRx 8 Migration: Unable to run the schematics to rename \`META_REDUCERS\` to \`USER_PROVIDED_META_REDUCERS\`
77+ in file '${ sourceFile . fileName } '.
78+
79+ For more info see https://ngrx.io/guide/migration/v8#meta_reducers-token.
80+ ` ) ;
81+ }
82+
5383 return changes ;
5484
5585 function isNgRxStoreImport ( importDeclaration : ts . ImportDeclaration ) {
0 commit comments