UNPKG

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