-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathaction.ts
194 lines (159 loc) · 5.11 KB
/
action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { BaseException, Path } from '@angular-devkit/core';
export class UnknownActionException extends BaseException {
constructor(action: Action) { super(`Unknown action: "${action.kind}".`); }
}
export type Action = CreateFileAction
| OverwriteFileAction
| RenameFileAction
| DeleteFileAction;
export interface ActionBase {
readonly id: number;
readonly parent: number;
readonly path: Path;
}
let _id = 1;
export class ActionList implements Iterable<Action> {
private _actions: Action[] = [];
protected _action(action: Partial<Action>) {
this._actions.push({
...action as Action,
id: _id++,
parent: this._actions[this._actions.length - 1]?.id ?? 0,
});
}
create(path: Path, content: Buffer) {
this._action({ kind: 'c', path, content });
}
overwrite(path: Path, content: Buffer) {
this._action({ kind: 'o', path, content });
}
rename(path: Path, to: Path) {
this._action({ kind: 'r', path, to });
}
delete(path: Path) {
this._action({ kind: 'd', path });
}
optimize() {
const toCreate = new Map<Path, Buffer>();
const toRename = new Map<Path, Path>();
const toOverwrite = new Map<Path, Buffer>();
const toDelete = new Set<Path>();
for (const action of this._actions) {
switch (action.kind) {
case 'c':
toCreate.set(action.path, action.content);
break;
case 'o':
if (toCreate.has(action.path)) {
toCreate.set(action.path, action.content);
} else {
toOverwrite.set(action.path, action.content);
}
break;
case 'd':
toDelete.add(action.path);
break;
case 'r':
const maybeCreate = toCreate.get(action.path);
const maybeOverwrite = toOverwrite.get(action.path);
if (maybeCreate) {
toCreate.delete(action.path);
toCreate.set(action.to, maybeCreate);
}
if (maybeOverwrite) {
toOverwrite.delete(action.path);
toOverwrite.set(action.to, maybeOverwrite);
}
let maybeRename: Path | undefined = undefined;
for (const [from, to] of toRename.entries()) {
if (to == action.path) {
maybeRename = from;
break;
}
}
if (maybeRename) {
toRename.set(maybeRename, action.to);
}
if (!maybeCreate && !maybeOverwrite && !maybeRename) {
toRename.set(action.path, action.to);
}
break;
}
}
this._actions = [];
toDelete.forEach(x => {
this.delete(x);
});
toRename.forEach((to, from) => {
this.rename(from, to);
});
toCreate.forEach((content, path) => {
this.create(path, content);
});
toOverwrite.forEach((content, path) => {
this.overwrite(path, content);
});
}
push(action: Action) { this._actions.push(action); }
get(i: number) { return this._actions[i]; }
has(action: Action) {
for (let i = 0; i < this._actions.length; i++) {
const a = this._actions[i];
if (a.id == action.id) {
return true;
}
if (a.id > action.id) {
return false;
}
}
return false;
}
find(predicate: (value: Action) => boolean): Action | null {
return this._actions.find(predicate) || null;
}
forEach(fn: (value: Action, index: number, array: Action[]) => void, thisArg?: {}) {
this._actions.forEach(fn, thisArg);
}
get length() { return this._actions.length; }
[Symbol.iterator]() { return this._actions[Symbol.iterator](); }
}
export function isContentAction(action: Action): action is CreateFileAction | OverwriteFileAction {
return action.kind == 'c' || action.kind == 'o';
}
/**
* @deprecated since version 11.0. not used anymore can be removed in future version.
*/
export function isAction(action: any): action is Action { // tslint:disable-line:no-any
const kind = action && action.kind;
return action !== null
&& typeof action.id == 'number'
&& typeof action.path == 'string'
&& (kind == 'c' || kind == 'o' || kind == 'r' || kind == 'd');
}
// Create a file. If the file already exists then this is an error.
export interface CreateFileAction extends ActionBase {
readonly kind: 'c';
readonly content: Buffer;
}
// Overwrite a file. If the file does not already exist, this is an error.
export interface OverwriteFileAction extends ActionBase {
readonly kind: 'o';
readonly content: Buffer;
}
// Move a file from one path to another. If the source files does not exist, this is an error.
// If the target path already exists, this is an error.
export interface RenameFileAction extends ActionBase {
readonly kind: 'r';
readonly to: Path;
}
// Delete a file. If the file does not exist, this is an error.
export interface DeleteFileAction extends ActionBase {
readonly kind: 'd';
}