source: trip-planner-front/node_modules/@schematics/angular/service-worker/index.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 7.0 KB
Line 
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12}) : (function(o, m, k, k2) {
13 if (k2 === undefined) k2 = k;
14 o[k2] = m[k];
15}));
16var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17 Object.defineProperty(o, "default", { enumerable: true, value: v });
18}) : function(o, v) {
19 o["default"] = v;
20});
21var __importStar = (this && this.__importStar) || function (mod) {
22 if (mod && mod.__esModule) return mod;
23 var result = {};
24 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25 __setModuleDefault(result, mod);
26 return result;
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29const core_1 = require("@angular-devkit/core");
30const schematics_1 = require("@angular-devkit/schematics");
31const tasks_1 = require("@angular-devkit/schematics/tasks");
32const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
33const ast_utils_1 = require("../utility/ast-utils");
34const change_1 = require("../utility/change");
35const dependencies_1 = require("../utility/dependencies");
36const ng_ast_utils_1 = require("../utility/ng-ast-utils");
37const paths_1 = require("../utility/paths");
38const project_targets_1 = require("../utility/project-targets");
39const workspace_1 = require("../utility/workspace");
40function addDependencies() {
41 return (host, context) => {
42 const packageName = '@angular/service-worker';
43 context.logger.debug(`adding dependency (${packageName})`);
44 const coreDep = dependencies_1.getPackageJsonDependency(host, '@angular/core');
45 if (coreDep === null) {
46 throw new schematics_1.SchematicsException('Could not find version.');
47 }
48 const serviceWorkerDep = {
49 ...coreDep,
50 name: packageName,
51 };
52 dependencies_1.addPackageJsonDependency(host, serviceWorkerDep);
53 return host;
54 };
55}
56function updateAppModule(mainPath) {
57 return (host, context) => {
58 context.logger.debug('Updating appmodule');
59 const modulePath = ng_ast_utils_1.getAppModulePath(host, mainPath);
60 context.logger.debug(`module path: ${modulePath}`);
61 // add import
62 let moduleSource = getTsSourceFile(host, modulePath);
63 let importModule = 'ServiceWorkerModule';
64 let importPath = '@angular/service-worker';
65 if (!ast_utils_1.isImported(moduleSource, importModule, importPath)) {
66 const change = ast_utils_1.insertImport(moduleSource, modulePath, importModule, importPath);
67 if (change) {
68 const recorder = host.beginUpdate(modulePath);
69 change_1.applyToUpdateRecorder(recorder, [change]);
70 host.commitUpdate(recorder);
71 }
72 }
73 // add import for environments
74 // import { environment } from '../environments/environment';
75 moduleSource = getTsSourceFile(host, modulePath);
76 const environmentExportName = ast_utils_1.getEnvironmentExportName(moduleSource);
77 // if environemnt import already exists then use the found one
78 // otherwise use the default name
79 importModule = environmentExportName || 'environment';
80 // TODO: dynamically find environments relative path
81 importPath = '../environments/environment';
82 if (!environmentExportName) {
83 // if environment import was not found then insert the new one
84 // with default path and default export name
85 const change = ast_utils_1.insertImport(moduleSource, modulePath, importModule, importPath);
86 if (change) {
87 const recorder = host.beginUpdate(modulePath);
88 change_1.applyToUpdateRecorder(recorder, [change]);
89 host.commitUpdate(recorder);
90 }
91 }
92 // register SW in app module
93 const importText = core_1.tags.stripIndent `
94 ServiceWorkerModule.register('ngsw-worker.js', {
95 enabled: ${importModule}.production,
96 // Register the ServiceWorker as soon as the app is stable
97 // or after 30 seconds (whichever comes first).
98 registrationStrategy: 'registerWhenStable:30000'
99 })
100 `;
101 moduleSource = getTsSourceFile(host, modulePath);
102 const metadataChanges = ast_utils_1.addSymbolToNgModuleMetadata(moduleSource, modulePath, 'imports', importText);
103 if (metadataChanges) {
104 const recorder = host.beginUpdate(modulePath);
105 change_1.applyToUpdateRecorder(recorder, metadataChanges);
106 host.commitUpdate(recorder);
107 }
108 return host;
109 };
110}
111function getTsSourceFile(host, path) {
112 const buffer = host.read(path);
113 if (!buffer) {
114 throw new schematics_1.SchematicsException(`Could not read file (${path}).`);
115 }
116 const content = buffer.toString();
117 const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
118 return source;
119}
120function default_1(options) {
121 return async (host, context) => {
122 const workspace = await workspace_1.getWorkspace(host);
123 const project = workspace.projects.get(options.project);
124 if (!project) {
125 throw new schematics_1.SchematicsException(`Invalid project name (${options.project})`);
126 }
127 if (project.extensions.projectType !== 'application') {
128 throw new schematics_1.SchematicsException(`Service worker requires a project type of "application".`);
129 }
130 const buildTarget = project.targets.get('build');
131 if (!buildTarget) {
132 throw project_targets_1.targetBuildNotFoundError();
133 }
134 const buildOptions = (buildTarget.options || {});
135 const root = project.root;
136 buildOptions.serviceWorker = true;
137 buildOptions.ngswConfigPath = core_1.join(core_1.normalize(root), 'ngsw-config.json');
138 let { resourcesOutputPath = '' } = buildOptions;
139 if (resourcesOutputPath) {
140 resourcesOutputPath = core_1.normalize(`/${resourcesOutputPath}`);
141 }
142 const templateSource = schematics_1.apply(schematics_1.url('./files'), [
143 schematics_1.applyTemplates({
144 ...options,
145 resourcesOutputPath,
146 relativePathToWorkspaceRoot: paths_1.relativePathToWorkspaceRoot(project.root),
147 }),
148 schematics_1.move(project.root),
149 ]);
150 context.addTask(new tasks_1.NodePackageInstallTask());
151 return schematics_1.chain([
152 schematics_1.mergeWith(templateSource),
153 workspace_1.updateWorkspace(workspace),
154 addDependencies(),
155 updateAppModule(buildOptions.main),
156 ]);
157 };
158}
159exports.default = default_1;
Note: See TracBrowser for help on using the repository browser.