1 | "use strict";
|
2 | var __values = (this && this.__values) || function(o) {
|
3 | var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
4 | if (m) return m.call(o);
|
5 | if (o && typeof o.length === "number") return {
|
6 | next: function () {
|
7 | if (o && i >= o.length) o = void 0;
|
8 | return { value: o && o[i++], done: !o };
|
9 | }
|
10 | };
|
11 | throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
12 | };
|
13 | var __read = (this && this.__read) || function (o, n) {
|
14 | var m = typeof Symbol === "function" && o[Symbol.iterator];
|
15 | if (!m) return o;
|
16 | var i = m.call(o), r, ar = [], e;
|
17 | try {
|
18 | while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
19 | }
|
20 | catch (error) { e = { error: error }; }
|
21 | finally {
|
22 | try {
|
23 | if (r && !r.done && (m = i["return"])) m.call(i);
|
24 | }
|
25 | finally { if (e) throw e.error; }
|
26 | }
|
27 | return ar;
|
28 | };
|
29 | var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
30 | for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
31 | to[j] = from[i];
|
32 | return to;
|
33 | };
|
34 | exports.__esModule = true;
|
35 | exports.omit = exports.addReducerImportToNgModule = exports.addReducerToActionReducerMap = exports.addReducerToStateInterface = exports.addReducerToState = void 0;
|
36 | var ts = require("typescript");
|
37 | var stringUtils = require("./strings");
|
38 | var change_1 = require("./change");
|
39 | var schematics_1 = require("@angular-devkit/schematics");
|
40 | var core_1 = require("@angular-devkit/core");
|
41 | var find_module_1 = require("./find-module");
|
42 | var ast_utils_1 = require("./ast-utils");
|
43 | function addReducerToState(options) {
|
44 | return function (host) {
|
45 | var e_1, _a;
|
46 | if (!options.reducers) {
|
47 | return host;
|
48 | }
|
49 | var reducersPath = core_1.normalize("/" + options.path + "/" + options.reducers);
|
50 | if (!host.exists(reducersPath)) {
|
51 | throw new Error("Specified reducers path " + reducersPath + " does not exist");
|
52 | }
|
53 | var text = host.read(reducersPath);
|
54 | if (text === null) {
|
55 | throw new schematics_1.SchematicsException("File " + reducersPath + " does not exist.");
|
56 | }
|
57 | var sourceText = text.toString('utf-8');
|
58 | var source = ts.createSourceFile(reducersPath, sourceText, ts.ScriptTarget.Latest, true);
|
59 | var reducerPath = "/" + options.path + "/" +
|
60 | (options.flat ? '' : stringUtils.dasherize(options.name) + '/') +
|
61 | (options.group ? 'reducers/' : '') +
|
62 | stringUtils.dasherize(options.name) +
|
63 | '.reducer';
|
64 | var relativePath = find_module_1.buildRelativePath(reducersPath, reducerPath);
|
65 | var reducerImport = ast_utils_1.insertImport(source, reducersPath, "* as from" + stringUtils.classify(options.name), relativePath, true);
|
66 | var stateInterfaceInsert = addReducerToStateInterface(source, reducersPath, options);
|
67 | var reducerMapInsert = addReducerToActionReducerMap(source, reducersPath, options);
|
68 | var changes = [reducerImport, stateInterfaceInsert, reducerMapInsert];
|
69 | var recorder = host.beginUpdate(reducersPath);
|
70 | try {
|
71 | for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
|
72 | var change = changes_1_1.value;
|
73 | if (change instanceof change_1.InsertChange) {
|
74 | recorder.insertLeft(change.pos, change.toAdd);
|
75 | }
|
76 | }
|
77 | }
|
78 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
79 | finally {
|
80 | try {
|
81 | if (changes_1_1 && !changes_1_1.done && (_a = changes_1["return"])) _a.call(changes_1);
|
82 | }
|
83 | finally { if (e_1) throw e_1.error; }
|
84 | }
|
85 | host.commitUpdate(recorder);
|
86 | return host;
|
87 | };
|
88 | }
|
89 | exports.addReducerToState = addReducerToState;
|
90 |
|
91 |
|
92 |
|
93 | function addReducerToStateInterface(source, reducersPath, options) {
|
94 | var stateInterface = source.statements.find(function (stm) { return stm.kind === ts.SyntaxKind.InterfaceDeclaration; });
|
95 | var node = stateInterface;
|
96 | if (!node) {
|
97 | return new change_1.NoopChange();
|
98 | }
|
99 | var state = options.plural
|
100 | ? stringUtils.pluralize(options.name)
|
101 | : stringUtils.camelize(options.name);
|
102 | var keyInsert = "[from" + stringUtils.classify(options.name) + "." + stringUtils.camelize(state) + "FeatureKey]: from" + stringUtils.classify(options.name) + ".State;";
|
103 | var expr = node;
|
104 | var position;
|
105 | var toInsert;
|
106 | if (expr.members.length === 0) {
|
107 | position = expr.getEnd() - 1;
|
108 | toInsert = " " + keyInsert + "\n";
|
109 | }
|
110 | else {
|
111 | node = expr.members[expr.members.length - 1];
|
112 | position = node.getEnd() + 1;
|
113 |
|
114 | var text = node.getFullText(source);
|
115 | var matches = text.match(/^\r?\n+(\s*)/);
|
116 | if (matches && matches.length > 0) {
|
117 | toInsert = "" + matches[1] + keyInsert + "\n";
|
118 | }
|
119 | else {
|
120 | toInsert = "\n" + keyInsert;
|
121 | }
|
122 | }
|
123 | return new change_1.InsertChange(reducersPath, position, toInsert);
|
124 | }
|
125 | exports.addReducerToStateInterface = addReducerToStateInterface;
|
126 |
|
127 |
|
128 |
|
129 | function addReducerToActionReducerMap(source, reducersPath, options) {
|
130 | var initializer;
|
131 | var actionReducerMap = source.statements
|
132 | .filter(function (stm) { return stm.kind === ts.SyntaxKind.VariableStatement; })
|
133 | .filter(function (stm) { return !!stm.declarationList; })
|
134 | .map(function (stm) {
|
135 | var declarations = stm.declarationList.declarations;
|
136 | var variable = declarations.find(function (decl) { return decl.kind === ts.SyntaxKind.VariableDeclaration; });
|
137 | var type = variable ? variable.type : {};
|
138 | return { initializer: variable.initializer, type: type };
|
139 | })
|
140 | .filter(function (initWithType) { return initWithType.type !== undefined; })
|
141 | .find(function (_a) {
|
142 | var type = _a.type;
|
143 | return type.typeName.text === 'ActionReducerMap';
|
144 | });
|
145 | if (!actionReducerMap || !actionReducerMap.initializer) {
|
146 | return new change_1.NoopChange();
|
147 | }
|
148 | var node = actionReducerMap.initializer;
|
149 | var state = options.plural
|
150 | ? stringUtils.pluralize(options.name)
|
151 | : stringUtils.camelize(options.name);
|
152 | var keyInsert = "[from" + stringUtils.classify(options.name) + "." + stringUtils.camelize(state) + "FeatureKey]: from" + stringUtils.classify(options.name) + ".reducer,";
|
153 | var expr = node;
|
154 | var position;
|
155 | var toInsert;
|
156 | if (expr.properties.length === 0) {
|
157 | position = expr.getEnd() - 1;
|
158 | toInsert = " " + keyInsert + "\n";
|
159 | }
|
160 | else {
|
161 | node = expr.properties[expr.properties.length - 1];
|
162 | position = node.getEnd() + 1;
|
163 |
|
164 | var text = node.getFullText(source);
|
165 | var matches = text.match(/^\r?\n+(\s*)/);
|
166 | if (matches && matches.length > 0) {
|
167 | toInsert = "\n" + matches[1] + keyInsert;
|
168 | }
|
169 | else {
|
170 | toInsert = "\n" + keyInsert;
|
171 | }
|
172 | }
|
173 | return new change_1.InsertChange(reducersPath, position, toInsert);
|
174 | }
|
175 | exports.addReducerToActionReducerMap = addReducerToActionReducerMap;
|
176 |
|
177 |
|
178 |
|
179 | function addReducerImportToNgModule(options) {
|
180 | return function (host) {
|
181 | var e_2, _a;
|
182 | if (!options.module) {
|
183 | return host;
|
184 | }
|
185 | var modulePath = options.module;
|
186 | if (!host.exists(options.module)) {
|
187 | throw new Error("Specified module path " + modulePath + " does not exist");
|
188 | }
|
189 | var text = host.read(modulePath);
|
190 | if (text === null) {
|
191 | throw new schematics_1.SchematicsException("File " + modulePath + " does not exist.");
|
192 | }
|
193 | var sourceText = text.toString('utf-8');
|
194 | var source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
|
195 | var commonImports = [
|
196 | ast_utils_1.insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
|
197 | ];
|
198 | var reducerPath = "/" + options.path + "/" +
|
199 | (options.flat ? '' : stringUtils.dasherize(options.name) + '/') +
|
200 | (options.group ? 'reducers/' : '') +
|
201 | stringUtils.dasherize(options.name) +
|
202 | '.reducer';
|
203 | var relativePath = find_module_1.buildRelativePath(modulePath, reducerPath);
|
204 | var reducerImport = ast_utils_1.insertImport(source, modulePath, "* as from" + stringUtils.classify(options.name), relativePath, true);
|
205 | var state = options.plural
|
206 | ? stringUtils.pluralize(options.name)
|
207 | : stringUtils.camelize(options.name);
|
208 | var _b = __read(ast_utils_1.addImportToModule(source, modulePath, "StoreModule.forFeature(from" + stringUtils.classify(options.name) + "." + state + "FeatureKey, from" + stringUtils.classify(options.name) + ".reducer)", relativePath), 1), storeNgModuleImport = _b[0];
|
209 | var changes = __spreadArray(__spreadArray([], __read(commonImports)), [reducerImport, storeNgModuleImport]);
|
210 | var recorder = host.beginUpdate(modulePath);
|
211 | try {
|
212 | for (var changes_2 = __values(changes), changes_2_1 = changes_2.next(); !changes_2_1.done; changes_2_1 = changes_2.next()) {
|
213 | var change = changes_2_1.value;
|
214 | if (change instanceof change_1.InsertChange) {
|
215 | recorder.insertLeft(change.pos, change.toAdd);
|
216 | }
|
217 | }
|
218 | }
|
219 | catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
220 | finally {
|
221 | try {
|
222 | if (changes_2_1 && !changes_2_1.done && (_a = changes_2["return"])) _a.call(changes_2);
|
223 | }
|
224 | finally { if (e_2) throw e_2.error; }
|
225 | }
|
226 | host.commitUpdate(recorder);
|
227 | return host;
|
228 | };
|
229 | }
|
230 | exports.addReducerImportToNgModule = addReducerImportToNgModule;
|
231 | function omit(object, keyToRemove) {
|
232 | return Object.keys(object)
|
233 | .filter(function (key) { return key !== keyToRemove; })
|
234 | .reduce(function (result, key) {
|
235 | var _a;
|
236 | return Object.assign(result, (_a = {}, _a[key] = object[key], _a));
|
237 | }, {});
|
238 | }
|
239 | exports.omit = omit;
|
240 |
|
\ | No newline at end of file |