1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.exportPushModule = exports.importPushModule = exports.migrateToNgrxPush = void 0;
|
4 | var ts = require("typescript");
|
5 | var schematics_1 = require("@angular-devkit/schematics");
|
6 | var schematics_core_1 = require("../../schematics-core");
|
7 | var ASYNC_REGEXP = /\| {0,}async/g;
|
8 | var PUSH_MODULE = 'PushModule';
|
9 | var COMPONENT_MODULE = '@ngrx/component';
|
10 | var pushModuleToFind = function (node) {
|
11 | return ts.isIdentifier(node) && node.text === PUSH_MODULE;
|
12 | };
|
13 | var ngModulesToFind = function (node) {
|
14 | return ts.isIdentifier(node) &&
|
15 | (node.text === 'CommonModule' || node.text === 'BrowserModule');
|
16 | };
|
17 | function migrateToNgrxPush() {
|
18 | return function (host) {
|
19 | return (0, schematics_core_1.visitTemplates)(host, function (template) {
|
20 | var match;
|
21 | var changes = [];
|
22 | while ((match = ASYNC_REGEXP.exec(template.content)) !== null) {
|
23 | var m = match.toString();
|
24 | changes.push(new schematics_core_1.ReplaceChange(template.fileName,
|
25 |
|
26 | template.start + match.index, m, m.replace('async', 'ngrxPush')));
|
27 | }
|
28 | return (0, schematics_core_1.commitChanges)(host, template.fileName, changes);
|
29 | });
|
30 | };
|
31 | }
|
32 | exports.migrateToNgrxPush = migrateToNgrxPush;
|
33 | function importPushModule() {
|
34 | return function (host) {
|
35 | (0, schematics_core_1.visitTSSourceFiles)(host, function (sourceFile) {
|
36 | var hasCommonModuleOrBrowserModule = false;
|
37 | var hasPushModule = false;
|
38 | (0, schematics_core_1.visitNgModuleImports)(sourceFile, function (_, importNodes) {
|
39 | hasCommonModuleOrBrowserModule = importNodes.some(ngModulesToFind);
|
40 | hasPushModule = importNodes.some(pushModuleToFind);
|
41 | });
|
42 | if (hasCommonModuleOrBrowserModule && !hasPushModule) {
|
43 | var changes = (0, schematics_core_1.addImportToModule)(sourceFile, sourceFile.fileName, PUSH_MODULE, COMPONENT_MODULE);
|
44 | (0, schematics_core_1.commitChanges)(host, sourceFile.fileName, changes);
|
45 | }
|
46 | });
|
47 | };
|
48 | }
|
49 | exports.importPushModule = importPushModule;
|
50 | function exportPushModule() {
|
51 | return function (host) {
|
52 | (0, schematics_core_1.visitTSSourceFiles)(host, function (sourceFile) {
|
53 | var hasCommonModuleOrBrowserModule = false;
|
54 | var hasPushModule = false;
|
55 | (0, schematics_core_1.visitNgModuleExports)(sourceFile, function (_, exportNodes) {
|
56 | hasCommonModuleOrBrowserModule = exportNodes.some(ngModulesToFind);
|
57 | hasPushModule = exportNodes.some(pushModuleToFind);
|
58 | });
|
59 | if (hasCommonModuleOrBrowserModule && !hasPushModule) {
|
60 | var changes = (0, schematics_core_1.addExportToModule)(sourceFile, sourceFile.fileName, PUSH_MODULE, COMPONENT_MODULE);
|
61 | (0, schematics_core_1.commitChanges)(host, sourceFile.fileName, changes);
|
62 | }
|
63 | });
|
64 | };
|
65 | }
|
66 | exports.exportPushModule = exportPushModule;
|
67 | function default_1() {
|
68 | return (0, schematics_1.chain)([migrateToNgrxPush(), importPushModule(), exportPushModule()]);
|
69 | }
|
70 | exports.default = default_1;
|
71 |
|
\ | No newline at end of file |