UNPKG

8.15 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};
40var __spreadArray = (this && this.__spreadArray) || function (to, from) {
41 for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
42 to[j] = from[i];
43 return to;
44};
45exports.__esModule = true;
46var ts = require("typescript");
47var core_1 = require("@angular-devkit/core");
48var schematics_1 = require("@angular-devkit/schematics");
49var tasks_1 = require("@angular-devkit/schematics/tasks");
50var schematics_core_1 = require("../../schematics-core");
51function addImportToNgModule(options) {
52 return function (host) {
53 var e_1, _a;
54 var modulePath = options.module;
55 if (!modulePath) {
56 return host;
57 }
58 if (!host.exists(modulePath)) {
59 throw new Error('Specified module does not exist');
60 }
61 var text = host.read(modulePath);
62 if (text === null) {
63 throw new schematics_1.SchematicsException("File " + modulePath + " does not exist.");
64 }
65 var sourceText = text.toString('utf-8');
66 var source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
67 var storeModuleReducers = options.minimal ? "{}" : "reducers";
68 var storeModuleConfig = options.minimal
69 ? "{}"
70 : "{\n metaReducers\n }";
71 var storeModuleSetup = "StoreModule.forRoot(" + storeModuleReducers + ", " + storeModuleConfig + ")";
72 var statePath = "/" + options.path + "/" + options.statePath;
73 var relativePath = schematics_core_1.buildRelativePath(modulePath, statePath);
74 var _b = __read(schematics_core_1.addImportToModule(source, modulePath, storeModuleSetup, relativePath), 1), storeNgModuleImport = _b[0];
75 var changes = [
76 schematics_core_1.insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
77 storeNgModuleImport,
78 ];
79 if (!options.minimal) {
80 changes = changes.concat([
81 schematics_core_1.insertImport(source, modulePath, 'reducers, metaReducers', relativePath),
82 ]);
83 }
84 var recorder = host.beginUpdate(modulePath);
85 try {
86 for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
87 var change = changes_1_1.value;
88 if (change instanceof schematics_core_1.InsertChange) {
89 recorder.insertLeft(change.pos, change.toAdd);
90 }
91 }
92 }
93 catch (e_1_1) { e_1 = { error: e_1_1 }; }
94 finally {
95 try {
96 if (changes_1_1 && !changes_1_1.done && (_a = changes_1["return"])) _a.call(changes_1);
97 }
98 finally { if (e_1) throw e_1.error; }
99 }
100 host.commitUpdate(recorder);
101 return host;
102 };
103}
104function addNgRxStoreToPackageJson() {
105 return function (host, context) {
106 schematics_core_1.addPackageToPackageJson(host, 'dependencies', '@ngrx/store', schematics_core_1.platformVersion);
107 context.addTask(new tasks_1.NodePackageInstallTask());
108 return host;
109 };
110}
111function addNgRxESLintPlugin() {
112 return function (host, context) {
113 var _a;
114 var eslintConfigPath = '.eslintrc.json';
115 var docs = 'https://github.com/timdeschryver/eslint-plugin-ngrx/#eslint-plugin-ngrx';
116 var eslint = (_a = host.read(eslintConfigPath)) === null || _a === void 0 ? void 0 : _a.toString('utf-8');
117 if (!eslint) {
118 return host;
119 }
120 schematics_core_1.addPackageToPackageJson(host, 'devDependencies', 'eslint-plugin-ngrx', '^1.0.0');
121 context.addTask(new tasks_1.NodePackageInstallTask());
122 try {
123 var json = JSON.parse(eslint);
124 if (json.overrides) {
125 json.overrides
126 .filter(function (override) { var _a; return (_a = override.files) === null || _a === void 0 ? void 0 : _a.some(function (file) { return file.endsWith('*.ts'); }); })
127 .forEach(configureESLintPlugin);
128 }
129 else {
130 configureESLintPlugin(json);
131 }
132 host.overwrite(eslintConfigPath, JSON.stringify(json, null, 2));
133 context.logger.info("\nThe NgRx ESLint Plugin is installed and configured with the recommended config.\n\nIf you want to change the configuration, please see " + docs + ".\n");
134 return host;
135 }
136 catch (err) {
137 context.logger.warn("\nSomething went wrong while adding the NgRx ESLint Plugin.\nThe NgRx ESLint Plugin is installed but not configured.\n\nPlease see " + docs + " to configure the NgRx ESLint Plugin.\n\nDetails:\n" + err.message + "\n");
138 }
139 return host;
140 };
141}
142function configureESLintPlugin(json) {
143 json.plugins = __spreadArray(__spreadArray([], __read((json.plugins || []))), ['ngrx']);
144 json["extends"] = __spreadArray(__spreadArray([], __read((json["extends"] || []))), ['plugin:ngrx/recommended']);
145}
146function default_1(options) {
147 return function (host, context) {
148 options.path = schematics_core_1.getProjectPath(host, options);
149 var parsedPath = schematics_core_1.parseName(options.path, '');
150 options.path = parsedPath.path;
151 var statePath = "/" + options.path + "/" + options.statePath + "/index.ts";
152 var srcPath = core_1.dirname(options.path);
153 var environmentsPath = schematics_core_1.buildRelativePath(statePath, "/" + srcPath + "/environments/environment");
154 if (options.module) {
155 options.module = schematics_core_1.findModuleFromOptions(host, {
156 name: '',
157 module: options.module,
158 path: options.path
159 });
160 }
161 if (options.stateInterface && options.stateInterface !== 'State') {
162 options.stateInterface = schematics_core_1.stringUtils.classify(options.stateInterface);
163 }
164 var templateSource = schematics_1.apply(schematics_1.url('./files'), [
165 schematics_1.filter(function () { return (options.minimal ? false : true); }),
166 schematics_1.applyTemplates(__assign(__assign(__assign({}, schematics_core_1.stringUtils), options), { environmentsPath: environmentsPath })),
167 schematics_1.move(parsedPath.path),
168 ]);
169 return schematics_1.chain([
170 schematics_1.branchAndMerge(schematics_1.chain([addImportToNgModule(options), schematics_1.mergeWith(templateSource)])),
171 options && options.skipPackageJson ? schematics_1.noop() : addNgRxStoreToPackageJson(),
172 options && options.skipESLintPlugin ? schematics_1.noop() : addNgRxESLintPlugin(),
173 ])(host, context);
174 };
175}
176exports["default"] = default_1;
177//# sourceMappingURL=index.js.map
\No newline at end of file