UNPKG

4.38 kBJavaScriptView Raw
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 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.augmentAppWithServiceWorker = void 0;
11const core_1 = require("@angular-devkit/core");
12const crypto = require("crypto");
13const fs_1 = require("fs");
14const path = require("path");
15const stream_1 = require("stream");
16class CliFilesystem {
17 constructor(base) {
18 this.base = base;
19 }
20 list(dir) {
21 return this._recursiveList(this._resolve(dir), []);
22 }
23 read(file) {
24 return fs_1.promises.readFile(this._resolve(file), 'utf-8');
25 }
26 hash(file) {
27 return new Promise((resolve, reject) => {
28 const hash = crypto.createHash('sha1').setEncoding('hex');
29 stream_1.pipeline(fs_1.createReadStream(this._resolve(file)), hash, (error) => error ? reject(error) : resolve(hash.read()));
30 });
31 }
32 write(file, content) {
33 return fs_1.promises.writeFile(this._resolve(file), content);
34 }
35 _resolve(file) {
36 return path.join(this.base, file);
37 }
38 async _recursiveList(dir, items) {
39 const subdirectories = [];
40 for await (const entry of await fs_1.promises.opendir(dir)) {
41 if (entry.isFile()) {
42 // Uses posix paths since the service worker expects URLs
43 items.push('/' + path.relative(this.base, path.join(dir, entry.name)).replace(/\\/g, '/'));
44 }
45 else if (entry.isDirectory()) {
46 subdirectories.push(path.join(dir, entry.name));
47 }
48 }
49 for (const subdirectory of subdirectories) {
50 await this._recursiveList(subdirectory, items);
51 }
52 return items;
53 }
54}
55async function augmentAppWithServiceWorker(projectRoot, appRoot, outputPath, baseHref, ngswConfigPath) {
56 const distPath = core_1.getSystemPath(core_1.normalize(outputPath));
57 const systemProjectRoot = core_1.getSystemPath(projectRoot);
58 // Find the service worker package
59 const workerPath = require.resolve('@angular/service-worker/ngsw-worker.js', {
60 paths: [systemProjectRoot],
61 });
62 const swConfigPath = require.resolve('@angular/service-worker/config', {
63 paths: [systemProjectRoot],
64 });
65 // Determine the configuration file path
66 let configPath;
67 if (ngswConfigPath) {
68 configPath = core_1.getSystemPath(core_1.normalize(ngswConfigPath));
69 }
70 else {
71 configPath = path.join(core_1.getSystemPath(appRoot), 'ngsw-config.json');
72 }
73 // Read the configuration file
74 let config;
75 try {
76 const configurationData = await fs_1.promises.readFile(configPath, 'utf-8');
77 config = JSON.parse(configurationData);
78 }
79 catch (error) {
80 if (error.code === 'ENOENT') {
81 throw new Error('Error: Expected to find an ngsw-config.json configuration file' +
82 ` in the ${core_1.getSystemPath(appRoot)} folder. Either provide one or` +
83 ' disable Service Worker in the angular.json configuration file.');
84 }
85 else {
86 throw error;
87 }
88 }
89 // Generate the manifest
90 const GeneratorConstructor = require(swConfigPath).Generator;
91 const generator = new GeneratorConstructor(new CliFilesystem(distPath), baseHref);
92 const output = await generator.process(config);
93 // Write the manifest
94 const manifest = JSON.stringify(output, null, 2);
95 await fs_1.promises.writeFile(path.join(distPath, 'ngsw.json'), manifest);
96 // Write the worker code
97 await fs_1.promises.copyFile(workerPath, path.join(distPath, 'ngsw-worker.js'), fs_1.constants.COPYFILE_FICLONE);
98 // If present, write the safety worker code
99 const safetyPath = path.join(path.dirname(workerPath), 'safety-worker.js');
100 try {
101 await fs_1.promises.copyFile(safetyPath, path.join(distPath, 'worker-basic.min.js'), fs_1.constants.COPYFILE_FICLONE);
102 await fs_1.promises.copyFile(safetyPath, path.join(distPath, 'safety-worker.js'), fs_1.constants.COPYFILE_FICLONE);
103 }
104 catch (error) {
105 if (error.code !== 'ENOENT') {
106 throw error;
107 }
108 }
109}
110exports.augmentAppWithServiceWorker = augmentAppWithServiceWorker;