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 __values = (this && this.__values) || function(o) {
|
14 | var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
15 | if (m) return m.call(o);
|
16 | if (o && typeof o.length === "number") return {
|
17 | next: function () {
|
18 | if (o && i >= o.length) o = void 0;
|
19 | return { value: o && o[i++], done: !o };
|
20 | }
|
21 | };
|
22 | throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
23 | };
|
24 | Object.defineProperty(exports, "__esModule", { value: true });
|
25 | var schematics_1 = require("@angular-devkit/schematics");
|
26 | var ts = require("typescript");
|
27 | var schematics_core_1 = require("../../schematics-core");
|
28 | function addStateToComponent(options) {
|
29 | return function (host) {
|
30 | var e_1, _a;
|
31 | if (!options.state && !options.stateInterface) {
|
32 | return host;
|
33 | }
|
34 | var statePath = "/".concat(options.path, "/").concat(options.state);
|
35 | if (options.state) {
|
36 | if (!host.exists(statePath)) {
|
37 | throw new Error("The Specified state path ".concat(statePath, " does not exist"));
|
38 | }
|
39 | }
|
40 | var componentPath = "/".concat(options.path, "/") +
|
41 | (options.flat ? '' : schematics_core_1.stringUtils.dasherize(options.name) + '/') +
|
42 | schematics_core_1.stringUtils.dasherize(options.name) +
|
43 | '.component.ts';
|
44 | var text = host.read(componentPath);
|
45 | if (text === null) {
|
46 | throw new schematics_1.SchematicsException("File ".concat(componentPath, " does not exist."));
|
47 | }
|
48 | var sourceText = text.toString('utf-8');
|
49 | var source = ts.createSourceFile(componentPath, sourceText, ts.ScriptTarget.Latest, true);
|
50 | var stateImportPath = (0, schematics_core_1.buildRelativePath)(componentPath, statePath);
|
51 | var storeImport = (0, schematics_core_1.insertImport)(source, componentPath, 'Store', '@ngrx/store');
|
52 | var stateImport = options.state
|
53 | ? (0, schematics_core_1.insertImport)(source, componentPath, "* as fromStore", stateImportPath, true)
|
54 | : new schematics_core_1.NoopChange();
|
55 | var componentClass = source.statements.find(function (stm) { return stm.kind === ts.SyntaxKind.ClassDeclaration; });
|
56 | var constructorUpdate = new schematics_core_1.ReplaceChange(componentPath, componentClass.members.pos, '\n', "\n constructor(private store: Store) {}");
|
57 | var changes = [storeImport, stateImport, constructorUpdate];
|
58 | var recorder = host.beginUpdate(componentPath);
|
59 | try {
|
60 | for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
|
61 | var change = changes_1_1.value;
|
62 | if (change instanceof schematics_core_1.InsertChange) {
|
63 | recorder.insertLeft(change.pos, change.toAdd);
|
64 | }
|
65 | else if (change instanceof schematics_core_1.ReplaceChange) {
|
66 | recorder.remove(change.pos, change.oldText.length);
|
67 | recorder.insertLeft(change.order, change.newText);
|
68 | }
|
69 | }
|
70 | }
|
71 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
72 | finally {
|
73 | try {
|
74 | if (changes_1_1 && !changes_1_1.done && (_a = changes_1.return)) _a.call(changes_1);
|
75 | }
|
76 | finally { if (e_1) throw e_1.error; }
|
77 | }
|
78 | host.commitUpdate(recorder);
|
79 | return host;
|
80 | };
|
81 | }
|
82 | function default_1(options) {
|
83 | return function (host, context) {
|
84 | options.path = (0, schematics_core_1.getProjectPath)(host, options);
|
85 | var parsedPath = (0, schematics_core_1.parseName)(options.path, options.name);
|
86 | options.name = parsedPath.name;
|
87 | options.path = parsedPath.path;
|
88 | var opts = ['state', 'stateInterface', 'testDepth'].reduce(function (current, key) {
|
89 | return (0, schematics_core_1.omit)(current, key);
|
90 | }, options);
|
91 | var templateSource = (0, schematics_1.apply)((0, schematics_1.url)(options.testDepth === 'unit' ? './files' : './integration-files'), [
|
92 | options.skipTests
|
93 | ? (0, schematics_1.filter)(function (path) { return !path.endsWith('.spec.ts.template'); })
|
94 | : (0, schematics_1.noop)(),
|
95 | (0, schematics_1.applyTemplates)(__assign(__assign({ 'if-flat': function (s) { return (options.flat ? '' : s); } }, schematics_core_1.stringUtils), options)),
|
96 | (0, schematics_1.move)(parsedPath.path),
|
97 | ]);
|
98 |
|
99 | Object.keys(opts).forEach(function (key) {
|
100 | return opts[key] === undefined ? delete opts[key] : {};
|
101 | });
|
102 | return (0, schematics_1.chain)([
|
103 | (0, schematics_1.externalSchematic)('@schematics/angular', 'component', __assign(__assign({}, opts), { skipTests: true })),
|
104 | addStateToComponent(options),
|
105 | (0, schematics_1.mergeWith)(templateSource),
|
106 | ])(host, context);
|
107 | };
|
108 | }
|
109 | exports.default = default_1;
|
110 |
|
\ | No newline at end of file |