Last active
July 11, 2019 19:59
-
-
Save brandonroberts/d00cbb34dd95884819a4250464e8e413 to your computer and use it in GitHub Desktop.
Schematic vs find/replace
This file contains hidden or 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
import { Rule, Tree } from '@angular-devkit/schematics'; | |
import * as ts from 'typescript'; | |
import { toPropertyName, toClassName } from '@nrwl/workspace'; | |
import { RequestContext } from './request-context'; | |
import { createOrUpdate, replaceNodeValue, findNodes, } from '@nrwl/workspace/src/utils/ast-utils'; | |
import { getDecoratorPropertyValueNode } from '../../../utils/ast-utils'; | |
export function updateReducerKey(context: RequestContext): Rule { | |
return (host: Tree) => { | |
const modulePath = context.options.module; | |
const sourceText = host.read(modulePath)!.toString('utf-8'); | |
const source = ts.createSourceFile( | |
modulePath, | |
sourceText, | |
ts.ScriptTarget.Latest, | |
true | |
); | |
const featureName = `${toPropertyName(context.featureName)}`; | |
const featureClassName = `${toClassName(context.featureName)}`; | |
const featureKeyName = `${featureClassName.toUpperCase()}_FEATURE_KEY`; | |
/*const content = source.text.replace( | |
`StoreModule.forFeature('${featureName}', `, | |
`StoreModule.forFeature(from${featureClassName}.${featureKeyName},` | |
);*/ | |
//source.text. | |
//createOrUpdate(host, modulePath, content); | |
const ngModuleImports = getDecoratorPropertyValueNode( | |
host, | |
modulePath, | |
'NgModule', | |
'imports', | |
'@angular/core' | |
); | |
const storeFeatureNode = (ngModuleImports as ts.ArrayLiteralExpression).elements.find(el => | |
el.getFullText().includes(`StoreModule.forFeature('${featureName}', from${featureClassName}.reducer)`) | |
) | |
if (storeFeatureNode) { | |
replaceNodeValue( | |
host, | |
modulePath, | |
storeFeatureNode, | |
`StoreModule.forFeature(from${featureClassName}.${featureKeyName}, from${featureClassName}.reducer),` | |
); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment