Skip to content

Commit

Permalink
Add config migration script (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wissididom authored Oct 12, 2024
1 parent f13b25a commit 37aaebe
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions migrate-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import "dotenv/config";
import fs from "node:fs";
import readline from "node:readline";

function readConfigIfExists(filePath) {
if (fs.existsSync(filePath)) {
return JSON.parse(fs.readFileSync(filePath));
} else {
return [];
}
}

function runMigration(cron) {
const configObject = {
twitchLogin: process.env.BROADCASTER_LOGIN,
cron: cron,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
discordWebhook: process.env.DISCORD_WEBHOOK_URL,
pollingInterval: process.env.POLLING_INTERVAL,
suppressUntitled: process.env.SUPPRESS_UNTITLED.toLowerCase() == "true",
showCreatedDate: process.env.SHOW_CREATED_DATE.toLowerCase() == "true",
};

const config = readConfigIfExists(".config.json");

config.push(configObject);

fs.writeFileSync(".config.json", JSON.stringify(config, null, 2));
console.log(JSON.stringify(config, null, 2));

const newEnv = `TWITCH_CLIENT_ID=${process.env.TWITCH_CLIENT_ID ?? "[0-9a-z]"}\nTWITCH_CLIENT_SECRET=${process.env.TWITCH_CLIENT_SECRET ?? "[0-9a-z]"}`;

fs.writeFileSync(".env", newEnv);
console.log(newEnv);
}

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

rl.question(
"Please enter the cron expression you want to have it run as:\n",
(cron) => {
rl.close();
runMigration(cron);
},
);

0 comments on commit 37aaebe

Please sign in to comment.