UNPKG

4.54 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.isContentAction = exports.ActionList = exports.UnknownActionException = void 0;
11const core_1 = require("@angular-devkit/core");
12class UnknownActionException extends core_1.BaseException {
13 constructor(action) {
14 super(`Unknown action: "${action.kind}".`);
15 }
16}
17exports.UnknownActionException = UnknownActionException;
18let _id = 1;
19class ActionList {
20 constructor() {
21 this._actions = [];
22 }
23 _action(action) {
24 var _a, _b;
25 this._actions.push({
26 ...action,
27 id: _id++,
28 parent: (_b = (_a = this._actions[this._actions.length - 1]) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0,
29 });
30 }
31 create(path, content) {
32 this._action({ kind: 'c', path, content });
33 }
34 overwrite(path, content) {
35 this._action({ kind: 'o', path, content });
36 }
37 rename(path, to) {
38 this._action({ kind: 'r', path, to });
39 }
40 delete(path) {
41 this._action({ kind: 'd', path });
42 }
43 optimize() {
44 const toCreate = new Map();
45 const toRename = new Map();
46 const toOverwrite = new Map();
47 const toDelete = new Set();
48 for (const action of this._actions) {
49 switch (action.kind) {
50 case 'c':
51 toCreate.set(action.path, action.content);
52 break;
53 case 'o':
54 if (toCreate.has(action.path)) {
55 toCreate.set(action.path, action.content);
56 }
57 else {
58 toOverwrite.set(action.path, action.content);
59 }
60 break;
61 case 'd':
62 toDelete.add(action.path);
63 break;
64 case 'r':
65 const maybeCreate = toCreate.get(action.path);
66 const maybeOverwrite = toOverwrite.get(action.path);
67 if (maybeCreate) {
68 toCreate.delete(action.path);
69 toCreate.set(action.to, maybeCreate);
70 }
71 if (maybeOverwrite) {
72 toOverwrite.delete(action.path);
73 toOverwrite.set(action.to, maybeOverwrite);
74 }
75 let maybeRename = undefined;
76 for (const [from, to] of toRename.entries()) {
77 if (to == action.path) {
78 maybeRename = from;
79 break;
80 }
81 }
82 if (maybeRename) {
83 toRename.set(maybeRename, action.to);
84 }
85 if (!maybeCreate && !maybeOverwrite && !maybeRename) {
86 toRename.set(action.path, action.to);
87 }
88 break;
89 }
90 }
91 this._actions = [];
92 toDelete.forEach((x) => {
93 this.delete(x);
94 });
95 toRename.forEach((to, from) => {
96 this.rename(from, to);
97 });
98 toCreate.forEach((content, path) => {
99 this.create(path, content);
100 });
101 toOverwrite.forEach((content, path) => {
102 this.overwrite(path, content);
103 });
104 }
105 push(action) {
106 this._actions.push(action);
107 }
108 get(i) {
109 return this._actions[i];
110 }
111 has(action) {
112 for (let i = 0; i < this._actions.length; i++) {
113 const a = this._actions[i];
114 if (a.id == action.id) {
115 return true;
116 }
117 if (a.id > action.id) {
118 return false;
119 }
120 }
121 return false;
122 }
123 find(predicate) {
124 return this._actions.find(predicate) || null;
125 }
126 forEach(fn, thisArg) {
127 this._actions.forEach(fn, thisArg);
128 }
129 get length() {
130 return this._actions.length;
131 }
132 [Symbol.iterator]() {
133 return this._actions[Symbol.iterator]();
134 }
135}
136exports.ActionList = ActionList;
137function isContentAction(action) {
138 return action.kind == 'c' || action.kind == 'o';
139}
140exports.isContentAction = isContentAction;