-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathindex.ts
155 lines (134 loc) · 3.93 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import * as ts from 'typescript';
import {
Rule,
SchematicContext,
SchematicsException,
Tree,
apply,
applyTemplates,
branchAndMerge,
chain,
filter,
mergeWith,
move,
noop,
url,
} from '@angular-devkit/schematics';
import {
InsertChange,
addImportToModule,
buildRelativePath,
findModuleFromOptions,
getProjectPath,
insertImport,
parseName,
stringUtils,
getPrefix,
} from '../../schematics-core';
import { Schema as EffectOptions } from './schema';
function addImportToNgModule(options: EffectOptions): Rule {
return (host: Tree) => {
const modulePath = options.module;
if (!modulePath) {
return host;
}
if (!host.exists(modulePath)) {
throw new Error(`Specified module path ${modulePath} does not exist`);
}
const text = host.read(modulePath);
if (text === null) {
throw new SchematicsException(`File ${modulePath} does not exist.`);
}
const sourceText = text.toString('utf-8');
const source = ts.createSourceFile(
modulePath,
sourceText,
ts.ScriptTarget.Latest,
true
);
const effectsName = `${stringUtils.classify(`${options.name}Effects`)}`;
const effectsModuleImport = insertImport(
source,
modulePath,
'EffectsModule',
'@ngrx/effects'
);
const effectsPath =
`/${options.path}/` +
(options.flat ? '' : stringUtils.dasherize(options.name) + '/') +
(options.group ? 'effects/' : '') +
stringUtils.dasherize(options.name) +
'.effects';
const relativePath = buildRelativePath(modulePath, effectsPath);
const effectsImport = insertImport(
source,
modulePath,
effectsName,
relativePath
);
const effectsSetup =
options.root && options.minimal ? `[]` : `[${effectsName}]`;
const [effectsNgModuleImport] = addImportToModule(
source,
modulePath,
`EffectsModule.for${options.root ? 'Root' : 'Feature'}(${effectsSetup})`,
relativePath
);
let changes = [effectsModuleImport, effectsNgModuleImport];
if (!options.root || (options.root && !options.minimal)) {
changes = changes.concat([effectsImport]);
}
const recorder = host.beginUpdate(modulePath);
for (const change of changes) {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(recorder);
return host;
};
}
function getEffectStart(name: string, effectPrefix: string): string {
const effectName = stringUtils.classify(name);
const effectMethodPrefix = stringUtils.camelize(effectPrefix);
return (
`${effectMethodPrefix}${effectName}s$ = createEffect(() => {` +
'\n return this.actions$.pipe(\n'
);
}
export default function (options: EffectOptions): Rule {
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);
options.prefix = getPrefix(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 templateSource = apply(url('./files'), [
options.skipTests
? filter((path) => !path.endsWith('.spec.ts.template'))
: noop(),
options.root && options.minimal ? filter((_) => false) : noop(),
applyTemplates({
...stringUtils,
'if-flat': (s: string) =>
stringUtils.group(
options.flat ? '' : s,
options.group ? 'effects' : ''
),
effectMethod: 'createEffect',
effectStart: getEffectStart(options.name, options.prefix),
effectEnd: ' );\n' + ' });',
...(options as object),
} as any),
move(parsedPath.path),
]);
return chain([
branchAndMerge(
chain([addImportToNgModule(options), mergeWith(templateSource)])
),
])(host, context);
};
}