UNPKG

7.97 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 __values = (this && this.__values) || function(o) {
30 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
31 if (m) return m.call(o);
32 if (o && typeof o.length === "number") return {
33 next: function () {
34 if (o && i >= o.length) o = void 0;
35 return { value: o && o[i++], done: !o };
36 }
37 };
38 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
39};
40Object.defineProperty(exports, "__esModule", { value: true });
41exports.addComponentStoreProviderToComponent = exports.addComponentStoreProviderToNgModule = void 0;
42var schematics_1 = require("@angular-devkit/schematics");
43var ts = require("typescript");
44var schematics_core_1 = require("../../schematics-core");
45function createProvidingContext(options, providingPath) {
46 var componentStoreName = "".concat(schematics_core_1.stringUtils.classify("".concat(options.name, "Store")));
47 var componentStorePath = "/".concat(options.path, "/") +
48 (options.flat ? '' : schematics_core_1.stringUtils.dasherize(options.name) + '/') +
49 schematics_core_1.stringUtils.dasherize(options.name) +
50 '.store';
51 var componentStoreRelativePath = (0, schematics_core_1.buildRelativePath)(providingPath, componentStorePath);
52 return {
53 componentStoreRelativePath: componentStoreRelativePath,
54 componentStoreName: componentStoreName,
55 };
56}
57/**
58 * Add component store to NgModule
59 */
60function addComponentStoreProviderToNgModule(options) {
61 return function (host) {
62 var e_1, _a;
63 if (!options.module) {
64 return host;
65 }
66 var modulePath = options.module;
67 if (!host.exists(options.module)) {
68 throw new Error("Specified module path ".concat(modulePath, " does not exist"));
69 }
70 var text = host.read(modulePath);
71 if (text === null) {
72 throw new schematics_1.SchematicsException("File ".concat(modulePath, " does not exist."));
73 }
74 var sourceText = text.toString('utf-8');
75 var source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
76 var context = createProvidingContext(options, options.module);
77 var componentStore = (0, schematics_core_1.insertImport)(source, modulePath, context.componentStoreName, context.componentStoreRelativePath);
78 var _b = __read((0, schematics_core_1.addProviderToModule)(source, modulePath, context.componentStoreName, context.componentStoreRelativePath), 1), storeNgModuleProvider = _b[0];
79 var changes = [componentStore, storeNgModuleProvider];
80 var recorder = host.beginUpdate(modulePath);
81 try {
82 for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
83 var change = changes_1_1.value;
84 if (change instanceof schematics_core_1.InsertChange) {
85 recorder.insertLeft(change.pos, change.toAdd);
86 }
87 }
88 }
89 catch (e_1_1) { e_1 = { error: e_1_1 }; }
90 finally {
91 try {
92 if (changes_1_1 && !changes_1_1.done && (_a = changes_1.return)) _a.call(changes_1);
93 }
94 finally { if (e_1) throw e_1.error; }
95 }
96 host.commitUpdate(recorder);
97 return host;
98 };
99}
100exports.addComponentStoreProviderToNgModule = addComponentStoreProviderToNgModule;
101/**
102 * Add component store to Component
103 */
104function addComponentStoreProviderToComponent(options) {
105 return function (host) {
106 var e_2, _a;
107 if (!options.component) {
108 return host;
109 }
110 var componentPath = options.component;
111 if (!host.exists(options.component)) {
112 throw new Error("Specified component path ".concat(componentPath, " does not exist"));
113 }
114 var text = host.read(componentPath);
115 if (text === null) {
116 throw new schematics_1.SchematicsException("File ".concat(componentPath, " does not exist."));
117 }
118 var sourceText = text.toString('utf-8');
119 var source = ts.createSourceFile(componentPath, sourceText, ts.ScriptTarget.Latest, true);
120 var context = createProvidingContext(options, options.component);
121 var componentStore = (0, schematics_core_1.insertImport)(source, componentPath, context.componentStoreName, context.componentStoreRelativePath);
122 var _b = __read((0, schematics_core_1.addProviderToComponent)(source, componentPath, context.componentStoreName, context.componentStoreRelativePath), 1), storeNgModuleProvider = _b[0];
123 var changes = [componentStore, storeNgModuleProvider];
124 var recorder = host.beginUpdate(componentPath);
125 try {
126 for (var changes_2 = __values(changes), changes_2_1 = changes_2.next(); !changes_2_1.done; changes_2_1 = changes_2.next()) {
127 var change = changes_2_1.value;
128 if (change instanceof schematics_core_1.InsertChange) {
129 recorder.insertLeft(change.pos, change.toAdd);
130 }
131 }
132 }
133 catch (e_2_1) { e_2 = { error: e_2_1 }; }
134 finally {
135 try {
136 if (changes_2_1 && !changes_2_1.done && (_a = changes_2.return)) _a.call(changes_2);
137 }
138 finally { if (e_2) throw e_2.error; }
139 }
140 host.commitUpdate(recorder);
141 return host;
142 };
143}
144exports.addComponentStoreProviderToComponent = addComponentStoreProviderToComponent;
145function default_1(options) {
146 return function (host, context) {
147 options.path = (0, schematics_core_1.getProjectPath)(host, options);
148 if (options.module) {
149 options.module = (0, schematics_core_1.findModuleFromOptions)(host, options);
150 }
151 if (options.component) {
152 options.component = (0, schematics_core_1.findComponentFromOptions)(host, options);
153 }
154 var parsedPath = (0, schematics_core_1.parseName)(options.path, options.name);
155 options.name = parsedPath.name;
156 options.path = parsedPath.path;
157 var templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
158 options.skipTests
159 ? (0, schematics_1.filter)(function (path) { return !path.endsWith('.spec.ts.template'); })
160 : (0, schematics_1.noop)(),
161 (0, schematics_1.applyTemplates)(__assign(__assign(__assign({}, schematics_core_1.stringUtils), { 'if-flat': function (s) { return (options.flat ? '' : s); } }), options)),
162 (0, schematics_1.move)(parsedPath.path),
163 ]);
164 return (0, schematics_1.chain)([
165 (0, schematics_1.branchAndMerge)((0, schematics_1.chain)([
166 addComponentStoreProviderToNgModule(options),
167 addComponentStoreProviderToComponent(options),
168 (0, schematics_1.mergeWith)(templateSource),
169 ])),
170 ])(host, context);
171 };
172}
173exports.default = default_1;
174//# sourceMappingURL=index.js.map
\No newline at end of file