-
Notifications
You must be signed in to change notification settings - Fork 2
/
mutations.ts
296 lines (265 loc) · 8.19 KB
/
mutations.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import {
Action,
ADD_ALIAS,
ADD_LINK,
ADD_MODULE,
REMOVE_ALIAS,
REMOVE_LINK,
REMOVE_MODULE,
UPDATE_MODULE,
} from "./actions.ts";
import { duplicateStore, Store } from "./store.ts";
import { compareModules, Module, moduleEquals } from "./module.ts";
import { Repository } from "./repository.ts";
import { createURL } from "./net.ts";
import * as path from "./vendor/https/deno.land/std/path/mod.ts";
import { hasDefaultExportLocal, hasDefaultExportRemote } from "./ast.ts";
const vendorDirectoryPath = "vendor";
// mutateStore mutates store. This affects only for memory.
export function mutateStore(store: Store, actions: Action[]): Store {
const s = duplicateStore(store);
const { modules } = s.config;
const aliases = { ...s.config.aliases };
for (const action of actions) {
switch (action.type) {
case ADD_MODULE: {
const { module } = action.payload;
for (const mod of modules) {
if (moduleEquals(mod, module)) {
throw new Error(`module already exists: ${module.toString()}
to update module, please use 'dem update'.`);
}
}
modules.push(module);
break;
}
case REMOVE_MODULE: {
const { moduleProtocol, modulePath } = action.payload;
const urlStr = `${moduleProtocol}://${modulePath}`;
let foundModIndex = 0;
let foundMod: Module | undefined;
for (const mod of modules) {
if (urlStr.startsWith(mod.toString())) {
foundMod = mod;
break;
}
foundModIndex++;
}
if (!foundMod) {
throw new Error(`remove module: module not found for: ${urlStr}`);
}
modules.splice(foundModIndex, 1);
break;
}
case ADD_LINK: {
const { link } = action.payload;
const foundMod = modules.find((mod) => {
return link.startsWith(mod.toString());
});
if (!foundMod) {
throw new Error(`add link: module not found for: ${link}`);
}
const filePath = link.split(foundMod.toString())[1];
if (foundMod.files.includes(filePath)) {
break;
}
foundMod.files.push(filePath);
break;
}
case REMOVE_LINK: {
const { link } = action.payload;
const foundMod = modules.find((mod) => {
return link.startsWith(mod.toString());
});
if (!foundMod) {
throw new Error(`remove link: module not found for: ${link}`);
}
const filePath = link.split(foundMod.toString())[1];
const foundFileIndex = foundMod.files.indexOf(filePath);
if (foundFileIndex < 0) {
throw new Error(`file not linked: ${link}`);
}
foundMod.files.splice(foundFileIndex, 1);
break;
}
case ADD_ALIAS: {
const { aliasPath, aliasTargetPath } = action.payload;
if (aliases[aliasPath]) {
throw new Error(
`alias already exists for: ${aliasPath}. please execute dem unalias before this.`,
);
}
const foundMod = modules.find((mod) => {
return aliasTargetPath.startsWith(mod.toString());
});
if (!foundMod) {
throw new Error(
`add alias: module not found for: ${aliasTargetPath}`,
);
}
aliases[aliasPath] = aliasTargetPath;
break;
}
case REMOVE_ALIAS: {
const { aliasPath } = action.payload;
if (!aliases[aliasPath]) {
throw new Error(`alias does not exist for: ${aliasPath}.`);
}
delete aliases[aliasPath];
break;
}
case UPDATE_MODULE: {
const { moduleProtocol, modulePath, moduleVersion } = action.payload;
const urlStr = `${moduleProtocol}://${modulePath}`;
const foundMod = modules.find((mod) => {
return mod.protocol === moduleProtocol && mod.path === modulePath;
});
if (!foundMod) {
throw new Error(`update module: module not found for: ${urlStr}`);
}
foundMod.version = moduleVersion;
break;
}
}
}
// sort modules
modules.sort(compareModules);
for (const mod of modules) {
mod.files.sort();
}
// sort aliases
const aliasesEntries = Object.entries(aliases);
aliasesEntries.sort((a, b) => {
return a[0].localeCompare(b[0]);
});
s.config.aliases = Object.fromEntries(aliasesEntries);
return s;
}
// mutateRepository mutates files controlled by repository.
// This persists mutated files.
export async function mutateRepository(
repository: Repository,
store: Store,
actions: Action[],
) {
const { modules } = store.config;
for (const action of actions) {
switch (action.type) {
case ADD_MODULE: {
// Do nothing for add module.
break;
}
case REMOVE_MODULE: {
const { moduleProtocol, modulePath } = action.payload;
await repository.removeModule(moduleProtocol, modulePath);
break;
}
case ADD_LINK: {
const { link } = action.payload;
// find module
const foundMod = modules.find((mod) => {
return link.startsWith(mod.toString());
});
if (!foundMod) {
throw new Error(`add link: module not found for: ${link}`);
}
const filePath = link.split(foundMod.toString())[1];
const url = createURL(
foundMod.protocol,
foundMod.path,
foundMod.version,
filePath,
);
const hasDefault = await hasDefaultExportRemote(url);
await repository.addLink(
foundMod.protocol,
foundMod.path,
foundMod.version,
filePath,
hasDefault,
);
break;
}
case REMOVE_LINK: {
const { link } = action.payload;
const foundMod = modules.find((mod) => {
return link.startsWith(mod.toString());
});
if (!foundMod) {
throw new Error(`remove link: module not found for: ${link}`);
}
// create file path
const filePath = link.split(foundMod.toString())[1];
await repository.removeLink(foundMod.protocol, foundMod.path, filePath);
break;
}
case ADD_ALIAS: {
const { aliasPath, aliasTargetPath } = action.payload;
const foundMod = modules.find((mod) => {
return aliasTargetPath.startsWith(mod.toString());
});
if (!foundMod) {
throw new Error(
`add alias: module not found for: ${aliasTargetPath}`,
);
}
const linkedFilePath = aliasTargetPath.split(foundMod.toString())[1];
const fp = path.join(
vendorDirectoryPath,
foundMod.protocol,
foundMod.path,
linkedFilePath,
);
let hasDefault = false;
try {
hasDefault = await hasDefaultExportLocal(fp);
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) {
throw e;
}
// let has default as false
}
await repository.addAlias(
foundMod.protocol,
foundMod.path,
linkedFilePath,
aliasPath,
hasDefault,
);
break;
}
case REMOVE_ALIAS: {
const { aliasPath } = action.payload;
await repository.removeAlias(aliasPath);
break;
}
case UPDATE_MODULE: {
const { moduleProtocol, modulePath, moduleVersion } = action.payload;
const urlStr = `${moduleProtocol}://${modulePath}`;
const foundMod = modules.find((mod) => {
return mod.protocol === moduleProtocol && mod.path === modulePath;
});
if (!foundMod) {
throw new Error(`update module: module not found for: ${urlStr}`);
}
for (const filePath of foundMod.files) {
const url = createURL(
moduleProtocol,
modulePath,
moduleVersion,
filePath,
);
const hasDefault = await hasDefaultExportRemote(url);
await repository.updateLink(
moduleProtocol,
modulePath,
moduleVersion,
filePath,
hasDefault,
);
}
break;
}
}
}
}