UNPKG

6.24 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};
40exports.__esModule = true;
41var ts = require("typescript");
42var schematics_1 = require("@angular-devkit/schematics");
43var schematics_core_1 = require("../../schematics-core");
44function addImportToNgModule(options) {
45 return function (host) {
46 var e_1, _a;
47 var modulePath = options.module;
48 if (!modulePath) {
49 return host;
50 }
51 if (!host.exists(modulePath)) {
52 throw new Error("Specified module path ".concat(modulePath, " does not exist"));
53 }
54 var text = host.read(modulePath);
55 if (text === null) {
56 throw new schematics_1.SchematicsException("File ".concat(modulePath, " does not exist."));
57 }
58 var sourceText = text.toString('utf-8');
59 var source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
60 var effectsName = "".concat(schematics_core_1.stringUtils.classify("".concat(options.name, "Effects")));
61 var effectsModuleImport = (0, schematics_core_1.insertImport)(source, modulePath, 'EffectsModule', '@ngrx/effects');
62 var effectsPath = "/".concat(options.path, "/") +
63 (options.flat ? '' : schematics_core_1.stringUtils.dasherize(options.name) + '/') +
64 (options.group ? 'effects/' : '') +
65 schematics_core_1.stringUtils.dasherize(options.name) +
66 '.effects';
67 var relativePath = (0, schematics_core_1.buildRelativePath)(modulePath, effectsPath);
68 var effectsImport = (0, schematics_core_1.insertImport)(source, modulePath, effectsName, relativePath);
69 var effectsSetup = options.root && options.minimal ? "[]" : "[".concat(effectsName, "]");
70 var _b = __read((0, schematics_core_1.addImportToModule)(source, modulePath, "EffectsModule.for".concat(options.root ? 'Root' : 'Feature', "(").concat(effectsSetup, ")"), relativePath), 1), effectsNgModuleImport = _b[0];
71 var changes = [effectsModuleImport, effectsNgModuleImport];
72 if (!options.root || (options.root && !options.minimal)) {
73 changes = changes.concat([effectsImport]);
74 }
75 var recorder = host.beginUpdate(modulePath);
76 try {
77 for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
78 var change = changes_1_1.value;
79 if (change instanceof schematics_core_1.InsertChange) {
80 recorder.insertLeft(change.pos, change.toAdd);
81 }
82 }
83 }
84 catch (e_1_1) { e_1 = { error: e_1_1 }; }
85 finally {
86 try {
87 if (changes_1_1 && !changes_1_1.done && (_a = changes_1["return"])) _a.call(changes_1);
88 }
89 finally { if (e_1) throw e_1.error; }
90 }
91 host.commitUpdate(recorder);
92 return host;
93 };
94}
95function getEffectStart(name, effectPrefix) {
96 var effectName = schematics_core_1.stringUtils.classify(name);
97 var effectMethodPrefix = schematics_core_1.stringUtils.camelize(effectPrefix);
98 return ("".concat(effectMethodPrefix).concat(effectName, "s$ = createEffect(() => {") +
99 '\n return this.actions$.pipe( \n');
100}
101function default_1(options) {
102 return function (host, context) {
103 options.path = (0, schematics_core_1.getProjectPath)(host, options);
104 options.prefix = (0, schematics_core_1.getPrefix)(options);
105 if (options.module) {
106 options.module = (0, schematics_core_1.findModuleFromOptions)(host, options);
107 }
108 var parsedPath = (0, schematics_core_1.parseName)(options.path, options.name || '');
109 options.name = parsedPath.name;
110 options.path = parsedPath.path;
111 var templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
112 options.skipTests
113 ? (0, schematics_1.filter)(function (path) { return !path.endsWith('.spec.ts.template'); })
114 : (0, schematics_1.noop)(),
115 options.root && options.minimal ? (0, schematics_1.filter)(function (_) { return false; }) : (0, schematics_1.noop)(),
116 (0, schematics_1.applyTemplates)(__assign(__assign(__assign({}, schematics_core_1.stringUtils), { 'if-flat': function (s) {
117 return schematics_core_1.stringUtils.group(options.flat ? '' : s, options.group ? 'effects' : '');
118 }, effectMethod: 'createEffect', effectStart: getEffectStart(options.name, options.prefix), effectEnd: ' );\n' + ' });' }), options)),
119 (0, schematics_1.move)(parsedPath.path),
120 ]);
121 return (0, schematics_1.chain)([
122 (0, schematics_1.branchAndMerge)((0, schematics_1.chain)([addImportToNgModule(options), (0, schematics_1.mergeWith)(templateSource)])),
123 ])(host, context);
124 };
125}
126exports["default"] = default_1;
127//# sourceMappingURL=index.js.map
\No newline at end of file