-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate-to-sqlite.js
60 lines (53 loc) · 1.68 KB
/
migrate-to-sqlite.js
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
import "dotenv/config";
import { existsSync, readFileSync } from "node:fs";
import Database from "./database/sqlite.js";
const handleExit = async (signal, code = 0, error = null) => {
if (error) console.error("Uncaught Exception:", error);
else console.log(`Received ${signal || `exit with code ${code}`}`);
Database.close();
process.exit(error ? 1 : code);
};
["SIGINT", "SIGTERM"].forEach((signal) =>
process.on(signal, () => handleExit(signal)),
);
process.on("uncaughtException", (err) => handleExit(null, 1, err));
process.on("exit", async (code) => handleExit(null, code));
const readJsonFile = (filePath) =>
existsSync(filePath) ? JSON.parse(readFileSync(filePath, "utf-8")) : null;
const migrateFollowerList = () => {
const followers = readJsonFile("lastFollowerList.json");
if (followers) {
Database.saveFollowerList(followers.followers || followers);
}
};
const migrateTokens = () => {
const tokens = readJsonFile(".tokens.json");
if (tokens) {
Database.setToken(
process.env.BROADCASTER_ID,
tokens.access_token,
tokens.refresh_token,
);
}
};
const migrations = {
followers: existsSync("lastFollowerList.json"),
tokens: existsSync(".tokens.json"),
};
switch (true) {
case migrations.followers && migrations.tokens:
console.log("Migrating lastFollowerList.json and .tokens.json...");
migrateFollowerList();
migrateTokens();
break;
case migrations.followers:
console.log("Migrating lastFollowerList.json...");
migrateFollowerList();
break;
case migrations.tokens:
console.log("Migrating .tokens.json...");
migrateTokens();
break;
default:
console.log("No migration files found. Exiting...");
}