pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 1 | import fs from "fs"; |
| 2 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 3 | // @ts-expect-error |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 4 | import * as readLine from "node:readline/promises"; |
| 5 | |
| 6 | const defaultDict = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 7 | developmentToken: |
| 8 | "Your development bot token (Used for testing in one server, rather than production)", |
| 9 | developmentGuildID: "Your development guild ID", |
| 10 | enableDevelopment: true, |
| 11 | token: "Your bot token", |
| 12 | managementGuildID: |
| 13 | "Your management guild ID (Used for running management commands on the bot)", |
| 14 | owners: [], |
| 15 | verifySecret: |
| 16 | "If using verify, enter a code here which matches the secret sent back by your website. You can use a random code if you do not have one already. (Optional)", |
| 17 | mongoUrl: "Your Mongo connection string, e.g. mongodb://127.0.0.1:27017", |
| 18 | baseUrl: |
| 19 | "Your website where buttons such as Verify and Role menu will link to, e.g. https://example.com", |
| 20 | pastebinApiKey: "An API key for pastebin (optional)", |
| 21 | pastebinUsername: "Your pastebin username (optional)", |
| 22 | pastebinPassword: "Your pastebin password (optional)" |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | const readline = readLine.createInterface({ |
| 26 | input: process.stdin, |
| 27 | output: process.stdout |
| 28 | }); |
| 29 | |
| 30 | async function getInput(prompt: string): Promise<string> { |
| 31 | process.stdout.write(prompt); |
| 32 | |
| 33 | const answer = await readline.question(prompt); |
| 34 | return answer.toString(); |
| 35 | } |
| 36 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 37 | export default async function (walkthrough = false) { |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 38 | if (walkthrough) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 39 | console.log( |
| 40 | "\x1b[33m🛈 Entering walkthrough mode for any missing values." |
| 41 | ); |
| 42 | console.log( |
| 43 | " \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n" |
| 44 | ); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 45 | |
| 46 | // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > "); |
| 47 | // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i"; |
| 48 | // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") { |
| 49 | // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`); |
| 50 | // await exec(toUse); |
| 51 | // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`); |
| 52 | // } else { |
| 53 | // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m"); |
| 54 | // } |
| 55 | } |
| 56 | |
| 57 | let json; |
| 58 | let out = true; |
| 59 | try { |
| 60 | json = JSON.parse(fs.readFileSync("./src/config/main.json", "utf8")); |
| 61 | } catch (e) { |
| 62 | console.log("\x1b[31mâš No main.json found, creating one."); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 63 | console.log( |
| 64 | " \x1b[2mYou can edit src/config/main.json directly using template written to the file.\x1b[0m\n" |
| 65 | ); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 66 | out = false; |
| 67 | json = {}; |
| 68 | } |
| 69 | for (const key in defaultDict) { |
| 70 | if (!json[key]) { |
| 71 | if (walkthrough) { |
| 72 | switch (key) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 73 | case "enableDevelopment": { |
| 74 | json[key] = |
| 75 | ( |
| 76 | (await getInput( |
| 77 | "\x1b[36mEnable development mode? This registers commands in a single server making it easier to test\x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > " |
| 78 | )) || "Y" |
| 79 | ).toLowerCase() === "y"; |
| 80 | break; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 81 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 82 | case "owners": { |
| 83 | let chosen = "!"; |
| 84 | const toWrite = []; |
| 85 | while (chosen !== "") { |
| 86 | chosen = await getInput( |
| 87 | "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > " |
| 88 | ); |
| 89 | if (chosen !== "") { |
| 90 | toWrite.push(chosen); |
| 91 | } |
| 92 | } |
| 93 | json[key] = toWrite; |
| 94 | break; |
| 95 | } |
| 96 | default: { |
| 97 | json[key] = await getInput( |
| 98 | `\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > ` |
| 99 | ); |
| 100 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 101 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 102 | } else { |
| 103 | json[key] = defaultDict[key]; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 104 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 107 | if (walkthrough && !json.mongoUrl) |
| 108 | json.mongoUrl = "mongodb://127.0.0.1:27017"; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 109 | if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/"; |
| 110 | if (!json.baseUrl.endsWith("/")) json.baseUrl += "/"; |
| 111 | let hosts; |
| 112 | try { |
| 113 | hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n"); |
| 114 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 115 | return console.log( |
| 116 | "\x1b[31mâš No /etc/hosts found. Please ensure the file exists and is readable. (Windows is not supported, Mac and Linux users should not experience this error)" |
| 117 | ); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 118 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 119 | let localhost = hosts.find((line) => line.split(" ")[1] === "localhost"); |
| 120 | if (localhost) { |
| 121 | localhost = localhost.split(" ")[0]; |
| 122 | } else { |
| 123 | localhost = "127.0.0.1"; |
| 124 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 125 | json.mongoUrl = json.mongoUrl.replace("localhost", localhost); |
| 126 | json.baseUrl = json.baseUrl.replace("localhost", localhost); |
| 127 | |
| 128 | fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4)); |
| 129 | |
| 130 | if (walkthrough) { |
| 131 | console.log("\x1b[32m✓ All properties added.\x1b[0m"); |
| 132 | } |
| 133 | return out; |
| 134 | } |