Last active
February 9, 2021 17:38
-
-
Save radekwarisch/7c2e329673a407752a420714ca0fcb7a to your computer and use it in GitHub Desktop.
Schematic pipe utils
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
import { | |
Rule, | |
SchematicContext, | |
Tree, | |
MergeStrategy, | |
empty, | |
mergeWith, | |
apply, | |
template, | |
move, | |
url | |
} from '@angular-devkit/schematics'; | |
import {map, tap} from 'rxjs'; | |
import {compose} from 'ramda'; | |
type ChainableType<TInputType> = (input: TInputType) => Rule; | |
export const schematicPipe = <TInputType>( | |
...chainables: (ChainableType<TInputType>)[] | |
) => (tree: Tree, context: SchematicContext) => { | |
// Input is mutable due to Schematics' unability to pass the output through chain([]) | |
const input: TInputType = {tree, context}; | |
return chain([...chainables.map(chainable => chainable(input))])( | |
tree, | |
context | |
); | |
}; | |
export const chainifyGatherOptions = <TInputType>( | |
gatherOptionsFn: ( | |
tree: Tree, | |
context: SchematicContext | |
) => Observable<TInputType> | Promise<TInputType> | TInputType | |
) => { | |
const reassignInputKeys = (input: TInputType) => (newInput: TInputType) => { | |
Object.entries(newInput).forEach(([key, newValue]: [string, unknown]) => { | |
Object.assign(input, { | |
[key]: newValue, | |
}); | |
}); | |
}; | |
return (input: TInputType) => (tree: Tree, context: SchematicContext) => { | |
return compose(gatherOptionsFn)(tree, context).pipe( | |
tap(reassignInputKeys(input)), | |
map(() => { | |
return tree; | |
}) | |
); | |
}; | |
}; | |
export const copyTemplatesAnd = <TInputType>( | |
...plugins: (ChainableType<TInputType>)[] | |
) => (input: TInputType) => () => { | |
const {templatesPath, movePath, templateVariables, tree} = input; | |
const newTemplates = apply(url(templatesPath), [ | |
template(templateVariables), | |
move(movePath), | |
...plugins.map(compose(input)), | |
]); | |
input.templates = newTemplates; | |
return tree; | |
}; | |
export const mergeFilesAnd = <TInputType>( | |
...plugins: (ChainableType<TInputType>)[] | |
) => (input: TInputType) => () => { | |
const {templates = empty()} = input; | |
return chain([ | |
mergeWith(templates, MergeStrategy.Overwrite), | |
...plugins.map(compose(input)), | |
]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment