diff --git a/scripts/db_migrations.js b/scripts/db_migrations.js
new file mode 100644
index 00000000..df5196b1
--- /dev/null
+++ b/scripts/db_migrations.js
@@ -0,0 +1,80 @@
+#!/usr/bin/node
+const path = require("path");
+const fs = require("fs");
+const { stdout, exit } = require("process");
+const { execIn } = require("./utils.js");
+const { ask } = require("./utils/ask.js");
+
+async function main() {
+ let filename;
+ if(process.argv[2]) filename = process.argv[2];
+ else filename = await ask("Please enter the name of your migration: ");
+ let dbconf;
+ try {
+ dbconf = JSON.parse(fs.readFileSync("dbconf.json"));
+ } catch (e) {
+ console.log("No dbconf.json found!");
+ dbconf = {};
+ }
+
+ if(!dbconf["sqlite"])
+ dbconf.sqlite = {
+ conn_str: "migrations.db",
+ migrations_dir: "sqlite",
+ package: "sqlite3"
+ }
+ if(!dbconf["postgres"] && process.env.FC_DB_POSTGRES) {
+ console.log("Found FC_DB_POSTGRES environment variable. Using it!");
+ dbconf.postgres = {
+ conn_str: process.env.FC_DB_POSTGRES,
+ migrations_dir: "postgres",
+ package: "pg"
+ }
+ }
+ if(!dbconf["mariadb"] && process.env.FC_DB_MARIADB){
+ console.log("Found FC_DB_MARIADB environment variable. Using it!");
+ dbconf.mariadb = {
+ conn_str: process.env.FC_DB_MARIADB,
+ migrations_dir: "mariadb",
+ package: "mysql2"
+ }
+ }
+ fs.writeFileSync("dbconf.json", JSON.stringify(dbconf, null, 4));
+
+ //build
+ execIn(`node scripts/build_new.js`, process.cwd(), {stdio: "inherit"});
+
+ if(fs.existsSync(".env") && !fs.existsSync(".env.bak"))
+ fs.renameSync(".env", ".env.bak");
+ Object.keys(dbconf).forEach((db) => {
+ console.log(`Applying migrations for ${db}`);
+ if(!fs.existsSync(path.join("node_modules", dbconf[db].package)))
+ execIn(`npm i ${dbconf[db].package}`, process.cwd());
+ fs.writeFileSync(
+ `.env`,
+ `DATABASE=${dbconf[db].conn_str}
+ THREADS=1
+ DB_MIGRATE=true
+ DB_VERBOSE=true`
+ );
+ execIn(`node dist/start.js`, process.cwd(), {stdio: "inherit"});
+ });
+
+ Object.keys(dbconf).forEach((db) => {
+ console.log(`Generating new migrations for ${db}`);
+ fs.writeFileSync(
+ `.env`,
+ `DATABASE=${dbconf[db].conn_str}
+ THREADS=1
+ DB_MIGRATE=true
+ DB_VERBOSE=true`
+ );
+ execIn(`node node_modules/typeorm/cli.js migration:generate "src/util/migrations/${db}/${filename}" -d dist/util/util/Database.js -p`, process.cwd(), {stdio: "inherit"});
+ });
+ if(fs.existsSync(".env.bak")) {
+ fs.rmSync(".env");
+ fs.renameSync(".env.bak", ".env");
+ }
+ exit(0);
+}
+main();
\ No newline at end of file
diff --git a/scripts/db_migrations.sh b/scripts/db_migrations.sh
deleted file mode 100755
index 9ec8230a..00000000
--- a/scripts/db_migrations.sh
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/bin/sh
-
-if [ ! -z "$1" ]
-then
- FILENAME="$1"
- echo "Using filename: $FILENAME"
-else
- read -p "Enter migration filename: " FILENAME
-fi
-
-[ -f ".env" ] && (
- mv .env .env.tmp 2>/dev/null
- source .env.tmp 2>/dev/null
-)
-npm run build clean logerrors pretty-errors
-
-make_migration() {
- echo "Creating migrations for $2"
- mkdir "src/util/migrations/$2" 2>/dev/null
-# npm run build clean logerrors pretty-errors
- THREADS=1 DATABASE="$1" DB_MIGRATE=a npm run start:bundle
- THREADS=1 DATABASE="$1" DB_MIGRATE=a npx typeorm-ts-node-commonjs migration:generate "src/util/migrations/$2/$FILENAME" -d src/util/util/Database.ts -p
- #npm run build clean logerrors pretty-errors
- #THREADS=1 DATABASE="$1" DB_MIGRATE=a npm run start:bundle
-}
-
-npm i sqlite3
-make_migration "database.db" "sqlite"
-
-[ -z "$FC_DB_POSTGRES" ] || (
- npm i pg
- make_migration "$FC_DB_POSTGRES" "postgres"
-)
-
-[ -z "$FC_DB_MARIADB" ] || (
- npm i mysql2
- make_migration "$FC_DB_MARIADB" "mariadb"
-)
-
-[ -f ".env.tmp" ] && mv .env.tmp .env 2>/dev/null
-
diff --git a/scripts/first_setup.js b/scripts/first_setup.js
index d320445d..4ce6e7de 100755
--- a/scripts/first_setup.js
+++ b/scripts/first_setup.js
@@ -2,10 +2,10 @@
const path = require("path");
const fs = require("fs");
const { stdout, exit } = require("process");
-const readline = require("readline");
const { execIn } = require("./utils.js");
+const { ask } = require("./utils/ask.js");
+
-const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const data = { env: [], config: { register: {} }, extra_pkgs: [] };
let rights = [];
@@ -128,7 +128,7 @@ async function main() {
printTitle("Step 5: extra options");
if (/y?/i.test(await ask("Use fast BCrypt implementation (requires a compiler) (Y/n): "))) data.extra_pkgs.push("bcrypt");
- if (/y?/.test(await ask("Enable support for widgets (requires compiler, known to fail on some ARM devices.) (Y/n): ")))
+ if (/y?/i.test(await ask("Enable support for widgets (requires compiler, known to fail on some ARM devices.) (Y/n): ")))
data.extra_pkgs.push("canvas");
printTitle("Step 6: finalizing...");
@@ -201,12 +201,7 @@ async function askRights() {
return selectedRights;
}
-async function askRight(right) {
- let answer = await ask(`${right}: `);
- if (answer == "y") return true;
- else if (answer == "n") return false;
- else return askRight(right);
-}
+
function printTitle(input) {
let width = stdout.columns / 2 - 1; //40
@@ -214,15 +209,7 @@ function printTitle(input) {
console.log("-".repeat(width - input.length / 2), input, "-".repeat(width - input.length / 2));
console.log();
}
-async function ask(question) {
- return new Promise((resolve, _reject) => {
- return rl.question(question, (answer) => {
- resolve(answer);
- });
- }).catch((err) => {
- console.log(err);
- });
-}
+
function BitFlag(int) {
return 1n << BigInt(int);
diff --git a/scripts/utils/ask.js b/scripts/utils/ask.js
new file mode 100644
index 00000000..cb8a29f6
--- /dev/null
+++ b/scripts/utils/ask.js
@@ -0,0 +1,20 @@
+const readline = require("readline");
+const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
+
+async function ask(question) {
+ return new Promise((resolve, _reject) => {
+ return rl.question(question, (answer) => {
+ resolve(answer);
+ });
+ }).catch((err) => {
+ console.log(err);
+ });
+}
+async function askBool(question) {
+ return /y?/i.test(await ask(question));
+}
+
+module.exports = {
+ ask,
+ askBool
+}
\ No newline at end of file
|