Skip to content

Commit

Permalink
extract file operations
Browse files Browse the repository at this point in the history
  • Loading branch information
soryy708 committed May 27, 2023
1 parent d1ceadc commit 132e5a8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
36 changes: 11 additions & 25 deletions src/server/discord/command/draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'discord.js';
import { DiscordCommand, DiscordCommandOption } from './interface';
import { randomInRange } from '../../entropy';
import { getFilesInDirectory, getSubdirectories } from '../../file';

export class DrawCommand implements DiscordCommand {
public name = 'draw';
Expand All @@ -22,12 +23,8 @@ export class DrawCommand implements DiscordCommand {
cardsPath,
);
}
const cardsDirectoryContents = await fs.promises.readdir(
const games = await getSubdirectories(
path.join(process.cwd(), 'cards'),
{ withFileTypes: true },
);
const games = cardsDirectoryContents.filter((dirent) =>
dirent.isDirectory(),
);
const options = [
{
Expand All @@ -36,11 +33,11 @@ export class DrawCommand implements DiscordCommand {
description: 'What game are we drawing from?',
options: await Promise.all(
games.map(async (game) => {
const cards = await this.getCardsOfGame(game.name);
const cards = await this.getCardsOfGame(game);
return {
type: ApplicationCommandOptionType.Subcommand,
name: this.directoryToCommand(game.name),
description: game.name,
name: this.directoryToCommand(game),
description: game,
options: cards.map((card) => ({
type: ApplicationCommandOptionType.Number,
name: this.directoryToCommand(card),
Expand All @@ -66,15 +63,8 @@ export class DrawCommand implements DiscordCommand {
}
}

private async getCardsOfGame(game: string): Promise<string[]> {
const subdirectoryContents = await fs.promises.readdir(
path.join(process.cwd(), 'cards', game),
{ withFileTypes: true },
);
const cardTypes = subdirectoryContents.filter((dirent) =>
dirent.isDirectory(),
);
return cardTypes.map((cardType) => cardType.name);
private getCardsOfGame(game: string): Promise<string[]> {
return getSubdirectories(path.join(process.cwd(), 'cards', game));
}

private directoryToCommand(directory: string): string {
Expand Down Expand Up @@ -108,19 +98,15 @@ export class DrawCommand implements DiscordCommand {

private async getCard(game: string, type: string): Promise<string> {
const cardsDirectory = path.join(process.cwd(), 'cards', game, type);
const cardsDirectoryContents = await fs.promises.readdir(
cardsDirectory,
{ withFileTypes: true },
);
const images = cardsDirectoryContents.filter(
(dirent) =>
dirent.isFile() && this.fileExtensionIsImage(dirent.name),
const cardFiles = await getFilesInDirectory(cardsDirectory);
const images = cardFiles.filter((file) =>
this.fileExtensionIsImage(file),
);
if (images.length === 0) {
return null;
}
const index = randomInRange(0, images.length);
const imageName = images[index].name;
const imageName = images[index];
return path.join(cardsDirectory, imageName);
}

Expand Down
21 changes: 21 additions & 0 deletions src/server/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs';

export async function getSubdirectories(directory: string): Promise<string[]> {
const directoryContents = await fs.promises.readdir(directory, {
withFileTypes: true,
});
const subdirectories = directoryContents.filter((dirent) =>
dirent.isDirectory(),
);
return subdirectories.map((dirent) => dirent.name);
}

export async function getFilesInDirectory(
directory: string,
): Promise<string[]> {
const subdirectoryContents = await fs.promises.readdir(directory, {
withFileTypes: true,
});
const files = subdirectoryContents.filter((dirent) => dirent.isFile());
return files.map((dirent) => dirent.name);
}

0 comments on commit 132e5a8

Please sign in to comment.