UNPKG

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