forked from peas-dev/peas
216 lines
6.0 KiB
TypeScript
216 lines
6.0 KiB
TypeScript
import { input, number, select, password, editor, confirm } from "@inquirer/prompts";
|
|
import { Locale } from "../packages/web/src/lib/locale.js";
|
|
import { getDirname } from "../packages/web/src/lib/path.js";
|
|
import { verify as DBVerify } from "../packages/web/src/lib/database.js";
|
|
import { readFileSync, writeFileSync } from "fs";
|
|
import pool from "../packages/web/src/lib/database.js";
|
|
import type { RowDataPacket } from "mysql2";
|
|
|
|
async function wizard() {
|
|
try {
|
|
const lang = await select({
|
|
message: "🌐",
|
|
choices: [
|
|
{
|
|
name: "日本語",
|
|
value: "ja",
|
|
},
|
|
{
|
|
name: "English",
|
|
value: "en",
|
|
},
|
|
],
|
|
});
|
|
|
|
const origin = await input({
|
|
message: Locale({ text: "Q_origin" }, lang),
|
|
default: "http://localhost:3000",
|
|
required: true,
|
|
});
|
|
const port = await number({
|
|
message: Locale({ text: "Q_port" }, lang),
|
|
min: 0,
|
|
max: 65535,
|
|
default: 3000,
|
|
required: true,
|
|
});
|
|
|
|
let db: {
|
|
host?: string;
|
|
port?: number;
|
|
user?: string;
|
|
pass?: string;
|
|
db?: string;
|
|
} = {};
|
|
db.host = await input({
|
|
message: Locale({ text: "Q_db_host" }, lang),
|
|
default: "db.example.com",
|
|
required: true,
|
|
});
|
|
db.port = await number({
|
|
message: Locale({ text: "Q_db_port" }, lang),
|
|
min: 0,
|
|
max: 65535,
|
|
default: 3306,
|
|
required: true,
|
|
});
|
|
db.user = await input({
|
|
message: Locale({ text: "Q_db_user" }, lang),
|
|
default: "root",
|
|
required: true,
|
|
});
|
|
db.pass = await password({
|
|
message: Locale({ text: "Q_db_pass" }, lang),
|
|
mask: "*",
|
|
});
|
|
db.db = await input({
|
|
message: Locale({ text: "Q_db_db" }, lang),
|
|
default: "peas",
|
|
required: true,
|
|
});
|
|
|
|
await DBVerify({
|
|
host: db.host,
|
|
port: db.port,
|
|
user: db.user,
|
|
password: db.pass,
|
|
database: db.db,
|
|
waitForConnections: true,
|
|
connectionLimit: 0,
|
|
});
|
|
|
|
writeFileSync(`${getDirname(import.meta.url)}../packages/web/config/peas.config.ts`,
|
|
`import type ConfigType from "./config";
|
|
|
|
const Config: ConfigType = {
|
|
server: {
|
|
port: ${String(port)},
|
|
origin: "${origin}",
|
|
},
|
|
database: {
|
|
host: "${db.host}",
|
|
port: ${String(db.port)},
|
|
user: "${db.user}",
|
|
pass: "${db.pass}",
|
|
db: "${db.db}",
|
|
},
|
|
}
|
|
|
|
export default Config;`, "utf-8");
|
|
|
|
console.log(Locale({ text: "CLI_config_file_write_success" }, lang));
|
|
|
|
const sql = readFileSync(`${getDirname(import.meta.url)}peas.sql`, "utf-8");
|
|
await pool.query<RowDataPacket[]>(sql);
|
|
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('server_name', ?)",
|
|
[await input({
|
|
message: Locale({ text: "serverName" }, lang),
|
|
default: "Peas",
|
|
required: true,
|
|
})]
|
|
);
|
|
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('server_description', ?)",
|
|
[await editor({
|
|
message: Locale({ text: "serverDescription" }, lang),
|
|
default: "Peas server",
|
|
})]
|
|
);
|
|
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('server_icon', ?)",
|
|
[await input({
|
|
message: Locale({ text: "serverIconURL" }, lang),
|
|
default: `${origin}/peas.svg`,
|
|
required: true,
|
|
})]
|
|
);
|
|
|
|
const turnstileIsEnabled = await confirm({
|
|
message: Locale({ text: "Q_turnstile_is_enabled" }, lang),
|
|
default: true,
|
|
});
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('turnstile_isEnabled', ?)",
|
|
[turnstileIsEnabled ? "1" : "0"]
|
|
);
|
|
if (turnstileIsEnabled) {
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('turnstile_sitekey', ?)",
|
|
[await input({
|
|
message: Locale({ text: "Q_turnstile_sitekey" }, lang),
|
|
default: "1x00000000000000000000AA",
|
|
required: true,
|
|
})]
|
|
);
|
|
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('turnstile_secret', ?)",
|
|
[await password({
|
|
message: Locale({ text: "Q_turnstile_secret" }, lang),
|
|
mask: "*",
|
|
})]
|
|
);
|
|
}
|
|
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('smtp_host', ?)",
|
|
[await input({
|
|
message: Locale({ text: "Q_smtp_host" }, lang),
|
|
default: "smtp.mail.example.com",
|
|
required: true,
|
|
})]
|
|
);
|
|
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('smtp_port', ?)",
|
|
[String(await number({
|
|
message: Locale({ text: "Q_smtp_port" }, lang),
|
|
default: 465,
|
|
min: 0,
|
|
max: 65535,
|
|
required: true,
|
|
}))]
|
|
);
|
|
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('smtp_user', ?)",
|
|
[await input({
|
|
message: Locale({ text: "Q_smtp_user" }, lang),
|
|
default: "peas@mail.example.com",
|
|
required: true,
|
|
})]
|
|
);
|
|
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('smtp_password', ?)",
|
|
[await password({
|
|
message: Locale({ text: "Q_smtp_pass" }, lang),
|
|
mask: "*",
|
|
})]
|
|
);
|
|
|
|
await pool.execute<RowDataPacket[]>(
|
|
"INSERT INTO `config` (`name`, `value`) VALUES ('smtp_secure', ?)",
|
|
[await confirm({
|
|
message: Locale({ text: "Q_smtp_secure" }, lang),
|
|
default: true,
|
|
}) ? "1": "0"]
|
|
);
|
|
|
|
console.log(Locale({ text: "CLISETUP_success" }, lang));
|
|
process.exit();
|
|
} catch (err: any) {
|
|
if (err.name === 'ExitPromptError') {
|
|
process.exit();
|
|
} else {
|
|
console.error(err);
|
|
}
|
|
}
|
|
}
|
|
|
|
wizard();
|