client/commands: statically import commands

Dynamic imports won't work very well with modules and we don't
really need them, it's just there to save us an import statement.

Let's flip this to a static version.
This commit is contained in:
Reto Brunner 2024-05-02 08:57:02 +02:00
parent 9ae9482223
commit 3fbbc39cd6
6 changed files with 22 additions and 38 deletions

View file

@ -56,7 +56,7 @@
import Mousetrap from "mousetrap"; import Mousetrap from "mousetrap";
import {wrapCursor} from "undate"; import {wrapCursor} from "undate";
import autocompletion from "../js/autocompletion"; import autocompletion from "../js/autocompletion";
import commands from "../js/commands/index"; import {commands} from "../js/commands/index";
import socket from "../js/socket"; import socket from "../js/socket";
import upload from "../js/upload"; import upload from "../js/upload";
import eventbus from "../js/eventbus"; import eventbus from "../js/eventbus";
@ -185,10 +185,7 @@ export default defineComponent({
return false; return false;
} }
if ( if (Object.prototype.hasOwnProperty.call(commands, cmd) && commands[cmd](args)) {
Object.prototype.hasOwnProperty.call(commands, cmd) &&
commands[cmd].input(args)
) {
return false; return false;
} }
} }

View file

@ -1,9 +1,9 @@
import socket from "../socket"; import socket from "../socket";
import {store} from "../store"; import {store} from "../store";
function input() { export function input(): boolean {
if (!store.state.activeChannel) { if (!store.state.activeChannel) {
return; return false;
} }
const messageIds: number[] = []; const messageIds: number[] = [];
@ -34,5 +34,3 @@ function input() {
return true; return true;
} }
export default {input};

View file

@ -1,9 +1,9 @@
import socket from "../socket"; import socket from "../socket";
import {store} from "../store"; import {store} from "../store";
function input() { export function input(): boolean {
if (!store.state.activeChannel) { if (!store.state.activeChannel) {
return; return false;
} }
const messageIds: number[] = []; const messageIds: number[] = [];
@ -34,5 +34,3 @@ function input() {
return true; return true;
} }
export default {input};

View file

@ -1,19 +1,11 @@
// Taken from views/index.js import {input as collapse} from "./collapse";
import {input as expand} from "./expand";
import {input as join} from "./join";
import {input as search} from "./search";
// This creates a version of `require()` in the context of the current export const commands = {
// directory, so we iterate over its content, which is a map statically built by collapse: collapse,
// Webpack. expand: expand,
// Second argument says it's recursive, third makes sure we only load javascript. join: join,
const commands = require.context("./", true, /\.ts$/); search: search,
};
export default commands.keys().reduce<Record<string, unknown>>((acc, path) => {
const command = path.substring(2, path.length - 3);
if (command === "index") {
return acc;
}
acc[command] = commands(path).default;
return acc;
}, {});

View file

@ -1,8 +1,9 @@
import socket from "../socket"; import socket from "../socket";
import {store} from "../store"; import {store} from "../store";
import {switchToChannel} from "../router"; import {switchToChannel} from "../router";
import {ChanType} from "../../../shared/types/chan";
function input(args: string[]) { export function input(args: string[]): boolean {
if (args.length > 0) { if (args.length > 0) {
let channels = args[0]; let channels = args[0];
@ -35,7 +36,7 @@ function input(args: string[]) {
return true; return true;
} }
} }
} else if (store.state.activeChannel?.channel.type === "channel") { } else if (store.state.activeChannel?.channel.type === ChanType.CHANNEL) {
// If `/join` command is used without any arguments, re-join current channel // If `/join` command is used without any arguments, re-join current channel
socket.emit("input", { socket.emit("input", {
target: store.state.activeChannel.channel.id, target: store.state.activeChannel.channel.id,
@ -44,6 +45,6 @@ function input(args: string[]) {
return true; return true;
} }
}
export default {input}; return false;
}

View file

@ -1,7 +1,7 @@
import {store} from "../store"; import {store} from "../store";
import {router} from "../router"; import {router} from "../router";
function input(args: string[]) { export function input(args: string[]): boolean {
if (!store.state.settings.searchEnabled) { if (!store.state.settings.searchEnabled) {
return false; return false;
} }
@ -23,5 +23,3 @@ function input(args: string[]) {
return true; return true;
} }
export default {input};