1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.augmentAppWithServiceWorker = void 0;
|
11 | const core_1 = require("@angular-devkit/core");
|
12 | const crypto = require("crypto");
|
13 | const fs_1 = require("fs");
|
14 | const path = require("path");
|
15 | const stream_1 = require("stream");
|
16 | class 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 |
|
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 | }
|
55 | async 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 |
|
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 |
|
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 |
|
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 |
|
90 | const GeneratorConstructor = require(swConfigPath).Generator;
|
91 | const generator = new GeneratorConstructor(new CliFilesystem(distPath), baseHref);
|
92 | const output = await generator.process(config);
|
93 |
|
94 | const manifest = JSON.stringify(output, null, 2);
|
95 | await fs_1.promises.writeFile(path.join(distPath, 'ngsw.json'), manifest);
|
96 |
|
97 | await fs_1.promises.copyFile(workerPath, path.join(distPath, 'ngsw-worker.js'), fs_1.constants.COPYFILE_FICLONE);
|
98 |
|
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 | }
|
110 | exports.augmentAppWithServiceWorker = augmentAppWithServiceWorker;
|