-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
index.ts
74 lines (67 loc) · 1.75 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import {
Rule,
SchematicContext,
SchematicsException,
Tree,
apply,
applyTemplates,
branchAndMerge,
chain,
filter,
mergeWith,
move,
noop,
template,
url,
} from '@angular-devkit/schematics';
import * as ts from 'typescript';
import {
getProjectPath,
findModuleFromOptions,
stringUtils,
addReducerToState,
addReducerImportToNgModule,
parseName,
} from '@ngrx/schematics/schematics-core';
import { Schema as ReducerOptions } from './schema';
export default function(options: ReducerOptions): Rule {
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);
if (options.module) {
options.module = findModuleFromOptions(host, options);
}
const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
const templateOptions = {
...stringUtils,
'if-flat': (s: string) =>
stringUtils.group(
options.flat ? '' : s,
options.group ? 'reducers' : ''
),
...(options as object),
};
const commonTemplate = apply(url('./common-files'), [
options.spec
? noop()
: filter(path => !path.endsWith('.spec.ts.template')),
applyTemplates(templateOptions),
move(parsedPath.path),
]);
const templateSource = apply(
url(options.creators ? './creator-files' : './files'),
[applyTemplates(templateOptions), move(parsedPath.path)]
);
return chain([
branchAndMerge(chain([addReducerToState(options)])),
branchAndMerge(
chain([
addReducerImportToNgModule(options),
mergeWith(commonTemplate),
mergeWith(templateSource),
])
),
])(host, context);
};
}