UNPKG

2.31 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.normalizeFileReplacements = exports.MissingFileReplacementException = void 0;
11const core_1 = require("@angular-devkit/core");
12const fs_1 = require("fs");
13class MissingFileReplacementException extends core_1.BaseException {
14 constructor(path) {
15 super(`The ${path} path in file replacements does not exist.`);
16 }
17}
18exports.MissingFileReplacementException = MissingFileReplacementException;
19function normalizeFileReplacements(fileReplacements, root) {
20 if (fileReplacements.length === 0) {
21 return [];
22 }
23 const normalizedReplacement = fileReplacements.map((replacement) => normalizeFileReplacement(replacement, root));
24 for (const { replace, with: replacementWith } of normalizedReplacement) {
25 if (!fs_1.existsSync(core_1.getSystemPath(replacementWith))) {
26 throw new MissingFileReplacementException(core_1.getSystemPath(replacementWith));
27 }
28 if (!fs_1.existsSync(core_1.getSystemPath(replace))) {
29 throw new MissingFileReplacementException(core_1.getSystemPath(replace));
30 }
31 }
32 return normalizedReplacement;
33}
34exports.normalizeFileReplacements = normalizeFileReplacements;
35function normalizeFileReplacement(fileReplacement, root) {
36 let replacePath;
37 let withPath;
38 if (fileReplacement.src && fileReplacement.replaceWith) {
39 replacePath = core_1.normalize(fileReplacement.src);
40 withPath = core_1.normalize(fileReplacement.replaceWith);
41 }
42 else if (fileReplacement.replace && fileReplacement.with) {
43 replacePath = core_1.normalize(fileReplacement.replace);
44 withPath = core_1.normalize(fileReplacement.with);
45 }
46 else {
47 throw new Error(`Invalid file replacement: ${JSON.stringify(fileReplacement)}`);
48 }
49 // TODO: For 7.x should this only happen if not absolute?
50 if (root) {
51 replacePath = core_1.join(root, replacePath);
52 }
53 if (root) {
54 withPath = core_1.join(root, withPath);
55 }
56 return { replace: replacePath, with: withPath };
57}