UNPKG

@ngx-pwa/local-storage

Version:

Efficient local storage module for Angular: simple API based on native localStorage API, but internally stored via the asynchronous IndexedDB API for performance, and wrapped in RxJS observables to be homogeneous with other Angular modules.

89 lines 5.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateModule = updateModule; exports.updateModuleToV9 = updateModuleToV9; const schematics_1 = require("@angular-devkit/schematics"); const typescript_1 = require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils"); const config_1 = require("./config"); /** * @param mainPath Path of the project `main.ts` file * @returns An object with `AppModule` path and source */ function getAppModule(host, mainPath) { const appModulePath = (0, ng_ast_utils_1.getAppModulePath)(host, mainPath); if (!host.exists(appModulePath)) { throw new schematics_1.SchematicsException(`Can't find AppModule`); } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- Existence checked above const appModuleFile = host.read(appModulePath).toString("utf-8"); return { appModulePath, appModuleFile }; } function updateAppModule(host, appModulePath, appModuleFile, storageModuleName) { /* Third param is to disable transpilation, 4rd note sure I just followed other official schematics */ const appModuleSource = (0, typescript_1.createSourceFile)(appModulePath, appModuleFile, typescript_1.ScriptTarget.Latest, true); // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion const appModuleChanges = (0, ast_utils_1.addImportToModule)(appModuleSource, appModulePath, storageModuleName, config_1.packageName); /* The changes must be applied, otherwise the previous line does nothing */ const recorder = host.beginUpdate(appModulePath); appModuleChanges.forEach((change) => { recorder.insertLeft(change.pos, change.toAdd); }); host.commitUpdate(recorder); } /** * @param mainPath Path of the project `main.ts` file */ function updateModule(mainPath) { return (host) => { const { appModulePath, appModuleFile } = getAppModule(host, mainPath); /* If `IDBNoWrap` is already set, it **must not** be changed, otherwise previously stored data would be lost */ if (!appModuleFile.includes("IDBNoWrap")) { if (appModuleFile.includes(config_1.packageName)) { /* It's important to keep the current options, otherwise previously stored data would be lost */ const updatedAppModuleFile = appModuleFile.replace(/StorageModule.forRoot\({(.*)}\)/s, "StorageModule.forRoot({ IDBNoWrap: false,$1 })"); /* If the file is still the same, it means we didn't catch the module * (for example, it can happen if the user aliased `StorageModule`) */ if (updatedAppModuleFile === appModuleFile) { throw new schematics_1.SchematicsException(`We couldn't update AppModule automatically. Be sure to follow the documentation to update manually, otherwise previsouly stored data could be lost.`); } host.overwrite(appModulePath, updatedAppModuleFile); } else { /* `IDBNoWrap` **must** be `false` in existing applications, otherwise previously stored data would be lost */ const storageModuleName = `StorageModule.forRoot({ IDBNoWrap: false })`; updateAppModule(host, appModulePath, appModuleFile, storageModuleName); } } return host; }; } /** * @param mainPath Path of the project `main.ts` file */ function updateModuleToV9(mainPath) { return (host) => { const { appModulePath, appModuleFile } = getAppModule(host, mainPath); /* If `IDBNoWrap` is already set, it **must not** be changed, otherwise previously stored data would be lost */ if (!appModuleFile.includes("IDBNoWrap")) { if (appModuleFile.includes(config_1.packageName)) { /* It's important to keep the current options, otherwise previously stored data would be lost */ const updatedAppModuleFile = appModuleFile.replace(/StorageModule.forRoot\({(.*)}\)/s, "StorageModule.forRoot({ IDBNoWrap: false,$1 })"); /* If the file is still the same, it means we didn't catch the module * (for example, it can happen if the user aliased `StorageModule`) */ if (updatedAppModuleFile === appModuleFile) { throw new schematics_1.SchematicsException(`We couldn't update AppModule automatically, please check documentation for manual update`); } host.overwrite(appModulePath, updatedAppModuleFile); } else { /* `IDBNoWrap` **must** be `false` in existing applications, otherwise previously stored data would be lost */ const storageModuleName = `StorageModule.forRoot({ IDBNoWrap: false })`; updateAppModule(host, appModulePath, appModuleFile, storageModuleName); } } return host; }; } //# sourceMappingURL=module.js.map