Migrate to Babel

main
Mari 2 years ago
parent fe448ce99c
commit 013fcdc57c
  1. 1
      .gitignore
  2. 11
      babel.config.json
  3. 9
      jest.config.ts
  4. 8575
      package-lock.json
  5. 7
      package.json
  6. 8
      src/commands/bot/index.ts
  7. 114
      src/commands/bot/rebuild.ts
  8. 14
      src/commands/bot/restart.ts
  9. 12
      src/commands/bot/shutdown.ts
  10. 16
      src/commands/character/create.ts
  11. 4
      src/commands/character/index.ts
  12. 10
      src/commands/index.spec.ts
  13. 6
      src/commands/index.ts
  14. 1
      src/ipc/restart.ts
  15. 8
      src/main.ts
  16. 6
      tsconfig.json

1
.gitignore vendored

@ -19,3 +19,4 @@ yarn-debug.log*
yarn-error.log* yarn-error.log*
/.idea /.idea
/output.log

@ -0,0 +1,11 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-typescript"
],
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread"
],
"sourceMaps": true
}

@ -102,7 +102,7 @@ export default {
// notifyMode: "failure-change", // notifyMode: "failure-change",
// A preset that is used as a base for Jest's configuration // A preset that is used as a base for Jest's configuration
preset: 'ts-jest', // preset: 'ts-jest',
// Run tests from one or more projects // Run tests from one or more projects
// projects: undefined, // projects: undefined,
@ -161,9 +161,10 @@ export default {
// ], // ],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [ testPathIgnorePatterns: [
// "/node_modules/" "/node_modules/",
// ], "/build/",
],
// The regexp pattern or array of patterns that Jest uses to detect test files // The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [], // testRegex: [],

8575
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -20,8 +20,15 @@
"ts-postgres": "^1.3.0" "ts-postgres": "^1.3.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/cli": "^7.19.3",
"@babel/core": "^7.19.3",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.18.9",
"@babel/preset-env": "^7.19.3",
"@babel/preset-typescript": "^7.18.6",
"@tsconfig/node18": "^1.0.1", "@tsconfig/node18": "^1.0.1",
"@types/pg": "^8.6.5", "@types/pg": "^8.6.5",
"babel-jest": "^29.1.2",
"jest": "^29.1.2", "jest": "^29.1.2",
"ts-jest": "^29.0.3", "ts-jest": "^29.0.3",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",

@ -1,8 +1,8 @@
import {BaseChatInputCommandData, CommandWithSubcommandsData} from "../types.js" import {BaseChatInputCommandData, CommandWithSubcommandsData} from "../types"
import {ApplicationCommandType} from "discord.js" import {ApplicationCommandType} from "discord.js"
import {commandBotRestart} from "./restart.js" import {commandBotRestart} from "./restart"
import {commandBotShutdown} from "./shutdown.js" import {commandBotShutdown} from "./shutdown"
import {commandBotRebuild} from "./rebuild.js" import {commandBotRebuild} from "./rebuild"
class BotCommandData extends CommandWithSubcommandsData { class BotCommandData extends CommandWithSubcommandsData {
readonly baseDefinition: BaseChatInputCommandData = { readonly baseDefinition: BaseChatInputCommandData = {

@ -1,13 +1,13 @@
import {adminId, SubcommandData} from "../types.js" import {adminId, SubcommandData} from "../types"
import { import {
ActivityType, ActivityType,
ApplicationCommandOptionType, ApplicationCommandOptionType,
ApplicationCommandSubCommandData, ApplicationCommandSubCommandData,
ChatInputCommandInteraction, ChatInputCommandInteraction,
} from "discord.js" } from "discord.js"
import {wrappedRestart} from "../../ipc/restart.js" import {wrappedRestart} from "../../ipc/restart"
import {spawn} from "child_process" import {fork} from "child_process"
import {defaultPresence} from "../../defaultPresence.js" import {defaultPresence} from "../../defaultPresence"
import {resolve as resolvePath} from "path" import {resolve as resolvePath} from "path"
class RebuildCommand extends SubcommandData { class RebuildCommand extends SubcommandData {
@ -20,8 +20,56 @@ class RebuildCommand extends SubcommandData {
async execute(b: ChatInputCommandInteraction): Promise<void> { async execute(b: ChatInputCommandInteraction): Promise<void> {
const user = b.user || b.member const user = b.user || b.member
if (!!user && user.id === adminId) { if (!!user && user.id === adminId) {
await b.reply("Mmm... let's see... this goes here...") await b.reply({
content: "I dunno... Let's check if this will work...",
ephemeral: true,
})
const self = b.client.user const self = b.client.user
self.setPresence({
status: "online",
afk: false,
activities: [
{
type: ActivityType.Playing,
name: "A Test of Your Codes",
},
],
})
try {
await new Promise<void>((resolve, reject) => {
const result = fork(resolvePath("./node_modules/jest-cli/bin/jest"), {
execPath: process.execPath,
execArgv: process.execArgv,
cwd: process.cwd(),
stdio: "inherit",
detached: true,
})
result.disconnect()
result.on("error", (ex) => {
reject(ex)
})
result.on("exit", (code) => {
if (code !== 0) {
reject(`Bad exit code ${code}`)
} else {
resolve()
}
})
})
} catch (ex) {
console.log(ex)
self.setPresence(defaultPresence)
await b.followUp({
content: "Oh no... It's not working... Oh well...",
ephemeral: true,
})
return
}
await b.followUp({
content: "Mmm... let's see... this goes here...",
ephemeral: true,
})
self.setPresence({ self.setPresence({
status: "online", status: "online",
afk: false, afk: false,
@ -34,12 +82,49 @@ class RebuildCommand extends SubcommandData {
}) })
try { try {
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
const result = spawn(resolvePath("./node_modules/.bin/tsc"), { const result = fork(resolvePath("./node_modules/typescript/bin/tsc"), {
execPath: process.execPath,
execArgv: process.execArgv,
cwd: process.cwd(), cwd: process.cwd(),
stdio: "inherit", stdio: "inherit",
detached: true, detached: true,
shell: true,
}) })
result.disconnect()
result.on("error", (ex) => {
reject(ex)
})
result.on("exit", (code) => {
if (code !== 0) {
reject(`Bad exit code ${code}`)
} else {
resolve()
}
})
})
} catch (ex) {
console.log(ex)
self.setPresence(defaultPresence)
await b.followUp({
ephemeral: true,
content: "I dunno... how to fit these together....",
})
return
}
await b.followUp({
content: "Hmm hmm~ Annnd some gluuue...",
ephemeral: true,
})
try {
await new Promise<void>((resolve, reject) => {
const result = fork(resolvePath("./node_modules/@babel/cli/bin/babel"),
["src", "--out-dir", "build", "--extensions", ".ts"], {
execPath: process.execPath,
execArgv: process.execArgv,
cwd: process.cwd(),
stdio: "inherit",
detached: true,
})
result.disconnect()
result.on("error", (ex) => { result.on("error", (ex) => {
reject(ex) reject(ex)
}) })
@ -54,7 +139,10 @@ class RebuildCommand extends SubcommandData {
} catch (ex) { } catch (ex) {
console.log(ex) console.log(ex)
self.setPresence(defaultPresence) self.setPresence(defaultPresence)
await b.followUp("Oops... I think it's broke...") await b.followUp({
ephemeral: true,
content: "Oops... I think it's broken... Sowwy...",
})
return return
} }
self.setPresence({ self.setPresence({
@ -67,10 +155,16 @@ class RebuildCommand extends SubcommandData {
}, },
], ],
}) })
await b.followUp("Phewwww... now I'll just... take a quick nap after all that hard work...") await b.followUp({
ephemeral: true,
content: "Phewwww... now I'll just... take a quick nap after all that hard work...",
})
await wrappedRestart(b) await wrappedRestart(b)
} else { } else {
await b.reply("Heeey... I don't gotta do what you say...") await b.reply({
ephemeral: true,
content: "Heeey... I don't gotta do what you say...",
})
} }
} }
} }

@ -1,11 +1,11 @@
import {adminId, SubcommandData} from "../types.js" import {adminId, SubcommandData} from "../types"
import { import {
ActivityType, ActivityType,
ApplicationCommandOptionType, ApplicationCommandOptionType,
ApplicationCommandSubCommandData, ApplicationCommandSubCommandData,
ChatInputCommandInteraction, ChatInputCommandInteraction,
} from "discord.js" } from "discord.js"
import {wrappedRestart} from "../../ipc/restart.js" import {wrappedRestart} from "../../ipc/restart"
class RestartCommand extends SubcommandData { class RestartCommand extends SubcommandData {
readonly definition: ApplicationCommandSubCommandData = { readonly definition: ApplicationCommandSubCommandData = {
@ -17,7 +17,10 @@ class RestartCommand extends SubcommandData {
async execute(b: ChatInputCommandInteraction): Promise<void> { async execute(b: ChatInputCommandInteraction): Promise<void> {
const user = b.user || b.member const user = b.user || b.member
if (!!user && user.id === adminId) { if (!!user && user.id === adminId) {
await b.reply("Yaaaawwn... Okay... Just a quick nap then...") await b.reply({
ephemeral: true,
content: "Yaaaawwn... Okay... Just a quick nap then...",
})
const self = b.client.user const self = b.client.user
self.setPresence({ self.setPresence({
status: "idle", status: "idle",
@ -31,7 +34,10 @@ class RestartCommand extends SubcommandData {
}) })
await wrappedRestart(b) await wrappedRestart(b)
} else { } else {
await b.reply("Heeey... I don't gotta do what you say...") await b.reply({
ephemeral: true,
content: "Heeey... I don't gotta do what you say...",
})
} }
} }
} }

@ -1,4 +1,4 @@
import {adminId, SubcommandData} from "../types.js" import {adminId, SubcommandData} from "../types"
import {ApplicationCommandOptionType, ApplicationCommandSubCommandData, ChatInputCommandInteraction} from "discord.js" import {ApplicationCommandOptionType, ApplicationCommandSubCommandData, ChatInputCommandInteraction} from "discord.js"
class ShutdownCommand extends SubcommandData { class ShutdownCommand extends SubcommandData {
@ -11,7 +11,10 @@ class ShutdownCommand extends SubcommandData {
async execute(b: ChatInputCommandInteraction): Promise<void> { async execute(b: ChatInputCommandInteraction): Promise<void> {
const user = b.user || b.member const user = b.user || b.member
if (!!user && user.id === adminId) { if (!!user && user.id === adminId) {
await b.reply("Good night =w=") await b.reply({
ephemeral: true,
content: "Good night =w=",
})
const self = b.client.user const self = b.client.user
self.presence.set({ self.presence.set({
status: "invisible", status: "invisible",
@ -19,7 +22,10 @@ class ShutdownCommand extends SubcommandData {
}) })
b.client.destroy() b.client.destroy()
} else { } else {
await b.reply("Heeey... I don't gotta do what you say...") await b.reply({
ephemeral: true,
content: "Heeey... I don't gotta do what you say...",
})
} }
} }
} }

@ -1,23 +1,29 @@
import {ApplicationCommandOptionType, ApplicationCommandSubCommandData, ChatInputCommandInteraction} from "discord.js" import {ApplicationCommandOptionType, ApplicationCommandSubCommandData, ChatInputCommandInteraction} from "discord.js"
import {SubcommandData} from "../types.js" import {SubcommandData} from "../types"
class CharacterCreateCommandData extends SubcommandData { class CharacterCreateCommandData extends SubcommandData {
readonly definition: ApplicationCommandSubCommandData = { readonly definition: ApplicationCommandSubCommandData = {
name: "create", name: "create",
type: ApplicationCommandOptionType.Subcommand, type: ApplicationCommandOptionType.Subcommand,
description: "Creates a new character. Activates them on the current server.", description: "Begins the process of creating a new character.",
options: [ options: [
{ {
name: "template", name: "name",
type: ApplicationCommandOptionType.String, type: ApplicationCommandOptionType.String,
description: "Optionally, an existing character of yours to use as a template.", description: "The character's name.",
autocomplete: true, required: false,
},
{
name: "title",
type: ApplicationCommandOptionType.String,
description: "A title for your character, optionally starting or ending with an ellipsis (...).",
required: false, required: false,
}, },
], ],
} }
async execute(b: ChatInputCommandInteraction) { async execute(b: ChatInputCommandInteraction) {
await b.deferReply({ephemeral: true})
await b.reply("Okaaaay, I'll make you a character ❤\n\nRight after this nap...") await b.reply("Okaaaay, I'll make you a character ❤\n\nRight after this nap...")
} }
} }

@ -1,6 +1,6 @@
import {ApplicationCommandOptionType, ApplicationCommandSubCommandData, ApplicationCommandType} from "discord.js" import {ApplicationCommandOptionType, ApplicationCommandSubCommandData, ApplicationCommandType} from "discord.js"
import {commandCharacterCreate} from "./create.js" import {commandCharacterCreate} from "./create"
import {BaseChatInputCommandData, CommandWithSubcommandsData} from "../types.js" import {BaseChatInputCommandData, CommandWithSubcommandsData} from "../types"
class CharacterCommandData extends CommandWithSubcommandsData { class CharacterCommandData extends CommandWithSubcommandsData {
readonly baseDefinition: BaseChatInputCommandData = { readonly baseDefinition: BaseChatInputCommandData = {

@ -0,0 +1,10 @@
import {commandDefinitions} from "./index"
import {describe, expect, test} from "@jest/globals"
describe("command definitions", () => {
test("has no descriptions over 100 characters", () => {
expect(commandDefinitions)
.not
.toContain(expect.objectContaining({"description": expect.stringMatching(/.{101,}/)}))
})
})

@ -6,9 +6,9 @@ import {
ChatInputCommandInteraction, ChatInputCommandInteraction,
Collection, Collection,
} from "discord.js" } from "discord.js"
import {commandCharacter} from "./character/index.js" import {commandCharacter} from "./character/index"
import {CommandData} from "./types.js" import {CommandData} from "./types"
import {commandBot} from "./bot/index.js" import {commandBot} from "./bot/index"
const commands: CommandData[] = [ const commands: CommandData[] = [
commandCharacter, commandCharacter,

@ -135,6 +135,7 @@ export async function doRestart(client: Client, appId: string, token: string,
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const child = fork(process.argv[1], process.argv.slice(2), { const child = fork(process.argv[1], process.argv.slice(2), {
execPath: process.execPath, execPath: process.execPath,
execArgv: process.execArgv,
cwd: process.cwd(), cwd: process.cwd(),
detached: true, detached: true,
stdio: "inherit", stdio: "inherit",

@ -1,9 +1,9 @@
import {BaseInteraction, Client} from "discord.js" import {BaseInteraction, Client} from "discord.js"
import {config} from "dotenv" import {config} from "dotenv"
import {isChatInputCommand} from "./types/interactions.js" import {isChatInputCommand} from "./types/interactions"
import {commandDefinitions, executeCommand, storeCachedCommands} from "./commands/index.js" import {commandDefinitions, executeCommand, storeCachedCommands} from "./commands/index"
import {checkIsRestart, reportFailed, reportReady, reportStarted} from "./ipc/restart.js" import {checkIsRestart, reportFailed, reportReady, reportStarted} from "./ipc/restart"
import {defaultPresence} from "./defaultPresence.js" import {defaultPresence} from "./defaultPresence"
async function main() { async function main() {
await checkIsRestart() await checkIsRestart()

@ -4,8 +4,9 @@
"lib": [ "lib": [
"esnext" "esnext"
], ],
"noEmit": true,
"allowJs": true, "allowJs": true,
"skipLibCheck": false, "skipLibCheck": true,
"esModuleInterop": true, "esModuleInterop": true,
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"strict": true, "strict": true,
@ -15,7 +16,7 @@
"module": "esnext", "module": "esnext",
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": false, "isolatedModules": true,
"outDir": "build", "outDir": "build",
"sourceMap": true "sourceMap": true
}, },
@ -24,6 +25,5 @@
], ],
"exclude": [ "exclude": [
"node_modules/**", "node_modules/**",
"**/*.spec.ts"
] ]
} }

Loading…
Cancel
Save