2024-05-12 03:27:28 +02:00
|
|
|
import confirm from "@inquirer/confirm";
|
2024-05-29 03:14:24 +02:00
|
|
|
import { Flags } from "@oclif/core";
|
2024-05-09 04:07:33 +02:00
|
|
|
import chalk from "chalk";
|
2024-05-29 03:14:24 +02:00
|
|
|
import { eq } from "drizzle-orm";
|
2024-05-12 03:27:28 +02:00
|
|
|
import ora from "ora";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { EmojiFinderCommand } from "~/cli/classes";
|
|
|
|
|
import { formatArray } from "~/cli/utils/format";
|
|
|
|
|
import { db } from "~/drizzle/db";
|
|
|
|
|
import { Emojis } from "~/drizzle/schema";
|
|
|
|
|
import { config } from "~/packages/config-manager";
|
|
|
|
|
import { MediaBackend } from "~/packages/media-manager";
|
2024-05-09 04:07:33 +02:00
|
|
|
|
|
|
|
|
export default class EmojiDelete extends EmojiFinderCommand<
|
|
|
|
|
typeof EmojiDelete
|
|
|
|
|
> {
|
|
|
|
|
static override args = {
|
|
|
|
|
identifier: EmojiFinderCommand.baseArgs.identifier,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static override description = "Deletes an emoji";
|
|
|
|
|
|
|
|
|
|
static override examples = [
|
|
|
|
|
"<%= config.bin %> <%= command.id %> baba_yassie",
|
|
|
|
|
'<%= config.bin %> <%= command.id %> "baba\\*" --pattern',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
static override flags = {
|
|
|
|
|
confirm: Flags.boolean({
|
|
|
|
|
description:
|
|
|
|
|
"Ask for confirmation before deleting the emoji (default yes)",
|
|
|
|
|
allowNo: true,
|
|
|
|
|
default: true,
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public async run(): Promise<void> {
|
2024-05-29 03:14:24 +02:00
|
|
|
const { flags } = await this.parse(EmojiDelete);
|
2024-05-09 04:07:33 +02:00
|
|
|
|
|
|
|
|
const emojis = await this.findEmojis();
|
|
|
|
|
|
|
|
|
|
if (!emojis || emojis.length === 0) {
|
|
|
|
|
this.log(chalk.bold(`${chalk.red("✗")} No emojis found`));
|
|
|
|
|
this.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Display user
|
|
|
|
|
flags.print &&
|
|
|
|
|
this.log(
|
|
|
|
|
chalk.bold(
|
|
|
|
|
`${chalk.green("✓")} Found ${chalk.green(
|
|
|
|
|
emojis.length,
|
|
|
|
|
)} emoji(s)`,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
flags.print &&
|
|
|
|
|
this.log(
|
|
|
|
|
formatArray(emojis, [
|
|
|
|
|
"id",
|
|
|
|
|
"shortcode",
|
|
|
|
|
"alt",
|
|
|
|
|
"contentType",
|
|
|
|
|
"instanceUrl",
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (flags.confirm) {
|
|
|
|
|
const choice = await confirm({
|
|
|
|
|
message: `Are you sure you want to delete these emojis? ${chalk.red(
|
|
|
|
|
"This is irreversible.",
|
|
|
|
|
)}`,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!choice) {
|
|
|
|
|
this.log(chalk.bold(`${chalk.red("✗")} Aborted operation`));
|
|
|
|
|
return this.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const spinner = ora("Deleting emoji(s)").start();
|
|
|
|
|
|
2024-05-13 04:52:19 +02:00
|
|
|
for (const emoji of emojis) {
|
|
|
|
|
spinner.text = `Deleting emoji ${chalk.gray(emoji.shortcode)} (${
|
|
|
|
|
emojis.findIndex((e) => e.id === emoji.id) + 1
|
|
|
|
|
}/${emojis.length})`;
|
2024-05-09 04:07:33 +02:00
|
|
|
|
2024-05-13 04:52:19 +02:00
|
|
|
const mediaBackend = await MediaBackend.fromBackendType(
|
|
|
|
|
config.media.backend,
|
|
|
|
|
config,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await mediaBackend.deleteFileByUrl(emoji.url);
|
|
|
|
|
|
|
|
|
|
await db.delete(Emojis).where(eq(Emojis.id, emoji.id));
|
|
|
|
|
}
|
2024-05-09 04:07:33 +02:00
|
|
|
|
2024-05-13 04:52:19 +02:00
|
|
|
spinner.succeed("Emoji(s) deleted");
|
2024-05-09 04:07:33 +02:00
|
|
|
|
|
|
|
|
this.exit(0);
|
|
|
|
|
}
|
|
|
|
|
}
|