-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathindex.ts
191 lines (167 loc) · 4.99 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import {
Rule,
SchematicContext,
SchematicsException,
Tree,
apply,
applyTemplates,
branchAndMerge,
chain,
mergeWith,
url,
move,
filter,
noop,
} from '@angular-devkit/schematics';
import * as ts from 'typescript';
import {
stringUtils,
buildRelativePath,
insertImport,
Change,
InsertChange,
getProjectPath,
isLib,
findModuleFromOptions,
addImportToModule,
parseName,
visitNgModuleImports,
} from '../../schematics-core';
import { Schema as StoreOptions } from './schema';
function addImportToNgModule(options: StoreOptions): 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 statePath = `${options.path}/${options.statePath}`;
const relativePath = buildRelativePath(modulePath, statePath);
const rootStoreReducers = options.minimal ? `{}` : `reducers`;
const rootStoreConfig = options.minimal ? `` : `, { metaReducers }`;
const storeNgModuleImport = addImportToModule(
source,
modulePath,
options.root
? `StoreModule.forRoot(${rootStoreReducers}${rootStoreConfig})`
: `StoreModule.forFeature(from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(
options.name
)}FeatureKey, from${stringUtils.classify(
options.name
)}.reducers, { metaReducers: from${stringUtils.classify(
options.name
)}.metaReducers })`,
relativePath
).shift();
let commonImports = [
insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
storeNgModuleImport,
];
if (options.root && !options.minimal) {
commonImports = commonImports.concat([
insertImport(
source,
modulePath,
'reducers, metaReducers',
relativePath
),
]);
} else if (!options.root) {
commonImports = commonImports.concat([
insertImport(
source,
modulePath,
`* as from${stringUtils.classify(options.name)}`,
relativePath,
true
),
]);
}
let rootImports: (Change | undefined)[] = [];
if (options.root) {
let hasImports = false;
visitNgModuleImports(source, (_, importNodes) => {
hasImports = importNodes.length > 0;
});
// `addImportToModule` adds a comma to imports when there are already imports present
// because at this time the store import hasn't been committed yet, `addImportToModule` wont add a comma
// so we have to add it here for empty import arrays
const adjectiveComma = hasImports ? '' : ', ';
const storeDevtoolsNgModuleImport = addImportToModule(
source,
modulePath,
`${adjectiveComma}isDevMode() ? StoreDevtoolsModule.instrument() : []`,
relativePath
).shift();
rootImports = rootImports.concat([
insertImport(
source,
modulePath,
'StoreDevtoolsModule',
'@ngrx/store-devtools'
),
insertImport(source, modulePath, 'isDevMode', '@angular/core'),
storeDevtoolsNgModuleImport,
]);
}
const changes = [...commonImports, ...rootImports];
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;
};
}
export default function (options: StoreOptions): Rule {
return (host: Tree, context: SchematicContext) => {
if (!options.name && !options.root) {
throw new Error(`Please provide a name for the feature state`);
}
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);
}
if (
options.root &&
options.stateInterface &&
options.stateInterface !== 'State'
) {
options.stateInterface = stringUtils.classify(options.stateInterface);
}
const templateSource = apply(url('./files'), [
options.root && options.minimal ? filter((_) => false) : noop(),
applyTemplates({
...stringUtils,
...(options as object),
isLib: isLib(host, options),
}),
move(parsedPath.path),
]);
return chain([
branchAndMerge(
chain([addImportToNgModule(options), mergeWith(templateSource)])
),
])(host, context);
};
}