UNPKG

@ngxs/store

Version:
108 lines 4.74 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addDeclarationToStandaloneApp = addDeclarationToStandaloneApp; exports.addDeclarationToNonStandaloneApp = addDeclarationToNonStandaloneApp; const schematics_1 = require("@angular-devkit/schematics"); const rules_1 = require("@schematics/angular/utility/standalone/rules"); const util_1 = require("@schematics/angular/utility/standalone/util"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const app_config_1 = require("@schematics/angular/utility/standalone/app_config"); const lib_config_1 = require("../utils/common/lib.config"); function addDeclarationToStandaloneApp(options) { return (host) => __awaiter(this, void 0, void 0, function* () { var _a; const mainFilePath = yield (0, util_1.getMainFilePath)(host, options.project); const bootstrapCall = (0, util_1.findBootstrapApplicationCall)(host, mainFilePath); const appConfigFilePath = ((_a = (0, app_config_1.findAppConfig)(bootstrapCall, host, mainFilePath)) === null || _a === void 0 ? void 0 : _a.filePath) || mainFilePath; const plugins = options.plugins .filter(p => pluginData.has(p)) .map((p) => [p, pluginData.get(p).standalone]); const importPluginRules = plugins.map(([plugin, standaloneDeclaration]) => { return importTree => { const change = (0, ast_utils_1.insertImport)((0, util_1.getSourceFile)(host, appConfigFilePath), appConfigFilePath, standaloneDeclaration, plugin); (0, util_1.applyChangesToFile)(importTree, appConfigFilePath, [change]); }; }); const pluginDeclarations = plugins .map(([, standaloneDeclaration]) => `${standaloneDeclaration}()`) .join(',\n'); return (0, schematics_1.chain)([ ...importPluginRules, (0, rules_1.addRootProvider)(options.project, ({ code, external }) => code `${external('provideStore', '@ngxs/store')}(\n[],\n${pluginDeclarations})`) ]); }); } function addDeclarationToNonStandaloneApp(options) { const pluginRules = options.plugins .map(p => { var _a; return [p, (_a = pluginData.get(p)) === null || _a === void 0 ? void 0 : _a.module]; }) .filter((v) => !!v[1]) .map(([plugin, moduleName]) => { return (0, rules_1.addRootImport)(options.project, ({ code, external }) => code `${external(moduleName, plugin)}.forRoot()`); }); const importPath = '@ngxs/store'; const moduleImportExtras = '.forRoot([], { developmentMode: /** !environment.production */ false })'; return (0, schematics_1.chain)([ (0, rules_1.addRootImport)(options.project, ({ code, external }) => code `${external('NgxsModule', importPath)}${moduleImportExtras}`), ...pluginRules ]); } const pluginData = new Map([ [ lib_config_1.LIBRARIES.DEVTOOLS, { module: 'NgxsReduxDevtoolsPluginModule', standalone: 'withNgxsReduxDevtoolsPlugin' } ], [ lib_config_1.LIBRARIES.FORM, { module: 'NgxsFormPluginModule', standalone: 'withNgxsFormPlugin' } ], [ lib_config_1.LIBRARIES.LOGGER, { module: 'NgxsLoggerPluginModule', standalone: 'withNgxsLoggerPlugin' } ], [ lib_config_1.LIBRARIES.ROUTER, { module: 'NgxsRouterPluginModule', standalone: 'withNgxsRouterPlugin' } ], [ lib_config_1.LIBRARIES.STORAGE, { module: 'NgxsStoragePluginModule', standalone: 'withNgxsStoragePlugin' } ], [ lib_config_1.LIBRARIES.STORE, { standalone: 'provideStore' } ], [ lib_config_1.LIBRARIES.WEBSOCKET, { module: 'NgxsWebSocketPluginModule', standalone: 'withNgxsWebSocketPlugin' } ] ]); //# sourceMappingURL=add-declaration.js.map