-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
index.ts
72 lines (65 loc) · 1.92 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
import {
Rule,
SchematicsException,
apply,
applyTemplates,
branchAndMerge,
chain,
filter,
mergeWith,
move,
noop,
template,
url,
Tree,
SchematicContext,
} from '@angular-devkit/schematics';
import {
stringUtils,
addReducerToState,
addReducerImportToNgModule,
getProjectPath,
findModuleFromOptions,
parseName,
} from '@ngrx/schematics/schematics-core';
import { Schema as EntityOptions } from './schema';
export default function(options: EntityOptions): Rule {
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);
const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
if (options.module) {
options.module = findModuleFromOptions(host, options);
}
const templateOptions = {
...stringUtils,
'if-flat': (s: string) => (options.flat ? '' : s),
'group-actions': (name: string) =>
stringUtils.group(name, options.group ? 'actions' : ''),
'group-models': (name: string) =>
stringUtils.group(name, options.group ? 'models' : ''),
'group-reducers': (s: string) =>
stringUtils.group(s, options.group ? 'reducers' : ''),
...(options as object),
};
const commonTemplates = 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([
addReducerToState({ ...options, plural: true }),
addReducerImportToNgModule({ ...options, plural: true }),
branchAndMerge(
chain([mergeWith(commonTemplates), mergeWith(templateSource)])
),
])(host, context);
};
}