-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
70 lines (60 loc) · 2.05 KB
/
index.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
import {
type RealWorldAssetsAction,
reducer,
utils,
} from "document-model-libs/real-world-assets";
import { utils as docUtils } from "document-model/document";
import { readFileSync } from "node:fs";
import { readdir, rename } from "node:fs/promises";
import { join } from "node:path";
function updateOperations(
operations: {
global: Record<string, any>[];
local: Record<string, any>[];
},
scope: "global" | "local",
fieldsToChange?: Record<string, any>,
fieldsToRemove?: string[]
) {
for (const operation of operations[scope]) {
if (fieldsToChange) {
for (const [key, value] of Object.entries(fieldsToChange)) {
const _value = typeof value === "function" ? value() : value;
operation[key] = _value;
}
}
if (fieldsToRemove) {
for (const field of fieldsToRemove) {
delete operation[field];
}
}
}
}
const srcDir = "documents";
const targetDir = "documents-updated";
const documentDirNames = await readdir(srcDir);
const fieldsToChange = {
id: () => docUtils.hashKey(),
};
const fieldsToRemove: string[] = [];
for (const name of documentDirNames) {
const path = join(srcDir, name, "operations.json");
const file = readFileSync(path);
const operations = JSON.parse(file.toString()) as {
global: Record<string, any>[];
local: Record<string, any>[];
};
updateOperations(operations, "global", fieldsToChange, fieldsToRemove);
let document = utils.createDocument();
for (const operation of operations.global) {
document = reducer(document, operation as RealWorldAssetsAction);
}
await utils.saveToFile(document, targetDir, `${name}-updated`);
}
const updatedDocumentFileNames = await readdir(targetDir);
for (const name of updatedDocumentFileNames) {
const path = join(targetDir, name);
const fixedName = name.replace("..zip", ".zip");
const fixedNamePath = join(targetDir, fixedName);
await rename(path, fixedNamePath);
}