UNPKG

@spartacus/schematics

Version:
60 lines 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addPWA = void 0; const schematics_1 = require("@angular-devkit/schematics"); const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils"); const file_utils_1 = require("../shared/utils/file-utils"); const workspace_utils_1 = require("../shared/utils/workspace-utils"); function removeServiceWorkerSetup(host, modulePath) { const buffer = host.read(modulePath); if (!buffer) { return; } let fileContent = buffer.toString(); const serviceWorkerImport = `import { ServiceWorkerModule } from '@angular/service-worker';`; const serviceWorkerModuleImport = `ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })`; if (!fileContent.includes(serviceWorkerModuleImport) || !fileContent.includes(serviceWorkerImport)) { return; } const recorder = host.beginUpdate(modulePath); recorder.remove(...file_utils_1.getLineFromTSFile(host, modulePath, fileContent.indexOf(serviceWorkerImport))); recorder.remove(...file_utils_1.getLineFromTSFile(host, modulePath, fileContent.indexOf(serviceWorkerModuleImport))); host.commitUpdate(recorder); // clean up environment import fileContent = (host.read(modulePath) || {}).toString(); const environmentImport = `import { environment } from '../environments/environment';`; if (fileContent.includes(environmentImport)) { const importPos = fileContent.indexOf(environmentImport) + environmentImport.length; // check if it's not needed if (!fileContent.includes('environment', importPos + environmentImport.length)) { const envRecorder = host.beginUpdate(modulePath); envRecorder.remove(...file_utils_1.getLineFromTSFile(host, modulePath, importPos)); host.commitUpdate(envRecorder); } } } function updateAppModule(options) { return (host) => { const projectTargets = workspace_utils_1.getProjectTargets(host, options.project); if (!projectTargets.build) { throw new schematics_1.SchematicsException(`Project target "build" not found.`); } const mainPath = projectTargets.build.options.main; const modulePath = ng_ast_utils_1.getAppModulePath(host, mainPath); removeServiceWorkerSetup(host, modulePath); return host; }; } function addPWA(options) { return (tree, context) => { return schematics_1.chain([ schematics_1.externalSchematic('@angular/pwa', 'pwa', { project: options.project, }), updateAppModule(options), ])(tree, context); }; } exports.addPWA = addPWA; //# sourceMappingURL=index.js.map