1 | "use strict";
|
2 | var __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 | };
|
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 __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 | };
|
40 | Object.defineProperty(exports, "__esModule", { value: true });
|
41 | exports.addComponentStoreProviderToComponent = exports.addComponentStoreProviderToNgModule = void 0;
|
42 | var schematics_1 = require("@angular-devkit/schematics");
|
43 | var ts = require("typescript");
|
44 | var schematics_core_1 = require("../../schematics-core");
|
45 | function 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 |
|
59 |
|
60 | function 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 | }
|
100 | exports.addComponentStoreProviderToNgModule = addComponentStoreProviderToNgModule;
|
101 |
|
102 |
|
103 |
|
104 | function 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 | }
|
144 | exports.addComponentStoreProviderToComponent = addComponentStoreProviderToComponent;
|
145 | function 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 | }
|
173 | exports.default = default_1;
|
174 |
|
\ | No newline at end of file |