UNPKG

7.78 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
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};
38var __values = (this && this.__values) || function(o) {
39 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
40 if (m) return m.call(o);
41 if (o && typeof o.length === "number") return {
42 next: function () {
43 if (o && i >= o.length) o = void 0;
44 return { value: o && o[i++], done: !o };
45 }
46 };
47 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
48};
49Object.defineProperty(exports, "__esModule", { value: true });
50var schematics_1 = require("@angular-devkit/schematics");
51var ts = require("typescript");
52var schematics_core_1 = require("../../schematics-core");
53function addImportToNgModule(options) {
54 return function (host) {
55 var e_1, _a;
56 var modulePath = options.module;
57 if (!modulePath) {
58 return host;
59 }
60 if (!host.exists(modulePath)) {
61 throw new Error("Specified module path ".concat(modulePath, " does not exist"));
62 }
63 var text = host.read(modulePath);
64 if (text === null) {
65 throw new schematics_1.SchematicsException("File ".concat(modulePath, " does not exist."));
66 }
67 var sourceText = text.toString('utf-8');
68 var source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
69 var statePath = "".concat(options.path, "/").concat(options.statePath);
70 var relativePath = (0, schematics_core_1.buildRelativePath)(modulePath, statePath);
71 var rootStoreReducers = options.minimal ? "{}" : "reducers";
72 var rootStoreConfig = options.minimal ? "" : ", { metaReducers }";
73 var storeNgModuleImport = (0, schematics_core_1.addImportToModule)(source, modulePath, options.root
74 ? "StoreModule.forRoot(".concat(rootStoreReducers).concat(rootStoreConfig, ")")
75 : "StoreModule.forFeature(from".concat(schematics_core_1.stringUtils.classify(options.name), ".").concat(schematics_core_1.stringUtils.camelize(options.name), "FeatureKey, from").concat(schematics_core_1.stringUtils.classify(options.name), ".reducers, { metaReducers: from").concat(schematics_core_1.stringUtils.classify(options.name), ".metaReducers })"), relativePath).shift();
76 var commonImports = [
77 (0, schematics_core_1.insertImport)(source, modulePath, 'StoreModule', '@ngrx/store'),
78 storeNgModuleImport,
79 ];
80 if (options.root && !options.minimal) {
81 commonImports = commonImports.concat([
82 (0, schematics_core_1.insertImport)(source, modulePath, 'reducers, metaReducers', relativePath),
83 ]);
84 }
85 else if (!options.root) {
86 commonImports = commonImports.concat([
87 (0, schematics_core_1.insertImport)(source, modulePath, "* as from".concat(schematics_core_1.stringUtils.classify(options.name)), relativePath, true),
88 ]);
89 }
90 var rootImports = [];
91 if (options.root) {
92 var hasImports_1 = false;
93 (0, schematics_core_1.visitNgModuleImports)(source, function (_, importNodes) {
94 hasImports_1 = importNodes.length > 0;
95 });
96 // `addImportToModule` adds a comma to imports when there are already imports present
97 // because at this time the store import hasn't been committed yet, `addImportToModule` wont add a comma
98 // so we have to add it here for empty import arrays
99 var adjectiveComma = hasImports_1 ? '' : ', ';
100 var storeDevtoolsNgModuleImport = (0, schematics_core_1.addImportToModule)(source, modulePath, "".concat(adjectiveComma, "isDevMode() ? StoreDevtoolsModule.instrument() : []"), relativePath).shift();
101 rootImports = rootImports.concat([
102 (0, schematics_core_1.insertImport)(source, modulePath, 'StoreDevtoolsModule', '@ngrx/store-devtools'),
103 (0, schematics_core_1.insertImport)(source, modulePath, 'isDevMode', '@angular/core'),
104 storeDevtoolsNgModuleImport,
105 ]);
106 }
107 var changes = __spreadArray(__spreadArray([], __read(commonImports), false), __read(rootImports), false);
108 var recorder = host.beginUpdate(modulePath);
109 try {
110 for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
111 var change = changes_1_1.value;
112 if (change instanceof schematics_core_1.InsertChange) {
113 recorder.insertLeft(change.pos, change.toAdd);
114 }
115 }
116 }
117 catch (e_1_1) { e_1 = { error: e_1_1 }; }
118 finally {
119 try {
120 if (changes_1_1 && !changes_1_1.done && (_a = changes_1.return)) _a.call(changes_1);
121 }
122 finally { if (e_1) throw e_1.error; }
123 }
124 host.commitUpdate(recorder);
125 return host;
126 };
127}
128function default_1(options) {
129 return function (host, context) {
130 if (!options.name && !options.root) {
131 throw new Error("Please provide a name for the feature state");
132 }
133 options.path = (0, schematics_core_1.getProjectPath)(host, options);
134 var parsedPath = (0, schematics_core_1.parseName)(options.path, options.name || '');
135 options.name = parsedPath.name;
136 options.path = parsedPath.path;
137 if (options.module) {
138 options.module = (0, schematics_core_1.findModuleFromOptions)(host, options);
139 }
140 if (options.root &&
141 options.stateInterface &&
142 options.stateInterface !== 'State') {
143 options.stateInterface = schematics_core_1.stringUtils.classify(options.stateInterface);
144 }
145 var templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
146 options.root && options.minimal ? (0, schematics_1.filter)(function (_) { return false; }) : (0, schematics_1.noop)(),
147 (0, schematics_1.applyTemplates)(__assign(__assign(__assign({}, schematics_core_1.stringUtils), options), { isLib: (0, schematics_core_1.isLib)(host, options) })),
148 (0, schematics_1.move)(parsedPath.path),
149 ]);
150 return (0, schematics_1.chain)([
151 (0, schematics_1.branchAndMerge)((0, schematics_1.chain)([addImportToNgModule(options), (0, schematics_1.mergeWith)(templateSource)])),
152 ])(host, context);
153 };
154}
155exports.default = default_1;
156//# sourceMappingURL=index.js.map
\No newline at end of file