-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathmigrate.ts
More file actions
77 lines (62 loc) · 2.16 KB
/
migrate.ts
File metadata and controls
77 lines (62 loc) · 2.16 KB
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
import { sep } from "@tauri-apps/api/path";
import { exists, mkdir, readTextFile, remove } from "@tauri-apps/plugin-fs";
import {
commands as exportCommands,
type FrontmatterInput,
type JsonValue,
} from "@hypr/plugin-export";
import type { OrganizationStorage } from "@hypr/store";
import { isFileNotFoundError } from "../utils";
import { getOrganizationDir, getOrganizationFilePath } from "./utils";
export async function migrateOrganizationsJsonIfNeeded(
dataDir: string,
): Promise<void> {
const organizationsJsonPath = [dataDir, "organizations.json"].join(sep());
const organizationsDir = getOrganizationDir(dataDir);
const jsonExists = await exists(organizationsJsonPath);
if (!jsonExists) {
return;
}
const dirExists = await exists(organizationsDir);
if (dirExists) {
return;
}
console.log(
"[OrganizationPersister] Migrating from organizations.json to organizations/*.md",
);
try {
const content = await readTextFile(organizationsJsonPath);
const organizations = JSON.parse(content) as Record<
string,
OrganizationStorage
>;
await mkdir(organizationsDir, { recursive: true });
const batchItems: [FrontmatterInput, string][] = [];
for (const [orgId, org] of Object.entries(organizations)) {
const frontmatter: Record<string, JsonValue> = {
created_at: org.created_at ?? "",
name: org.name ?? "",
user_id: org.user_id ?? "",
};
const body = "";
const filePath = getOrganizationFilePath(dataDir, orgId);
batchItems.push([{ frontmatter, content: body }, filePath]);
}
if (batchItems.length > 0) {
const result = await exportCommands.exportFrontmatterBatch(batchItems);
if (result.status === "error") {
throw new Error(
`Failed to export migrated organizations: ${result.error}`,
);
}
}
await remove(organizationsJsonPath);
console.log(
`[OrganizationPersister] Migration complete: ${Object.keys(organizations).length} organizations migrated`,
);
} catch (error) {
if (!isFileNotFoundError(error)) {
console.error("[OrganizationPersister] Migration failed:", error);
}
}
}