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