Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
fix #3 リファクタリング
Browse files Browse the repository at this point in the history
  • Loading branch information
yogarasu committed Aug 16, 2022
1 parent a7d5f33 commit 9c40ce8
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 97 deletions.
8 changes: 4 additions & 4 deletions command/build/message.mts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ interface Options {
}
spinner.success()

// チャンネルファイルを取得する
spinner.loading("Get channel file")
// チャンネルを取得する
spinner.loading("Get channel")
const { channels, ...getChannelFileResult } = await getChannelFile(
distChannelFilePath
)
Expand All @@ -50,8 +50,8 @@ interface Options {
}
spinner.success()

// ユーザーファイルを取得する
spinner.loading("Get user file")
// ユーザーを取得する
spinner.loading("Get user")
const { users, ...getUserFileResult } = await getUserFile(distUserFilePath)
if (getUserFileResult.status === "failed") {
spinner.failed(null, getUserFileResult.message)
Expand Down
4 changes: 2 additions & 2 deletions command/build/user.mts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ interface Options {
}
spinner.success()

// チャンネルファイルを取得する
spinner.loading("Get channel file")
// チャンネルを取得する
spinner.loading("Get channel")
const { channels, ...getChannelFileResult } = await getChannelFile(
distChannelFilePath
)
Expand Down
42 changes: 8 additions & 34 deletions command/deploy/channel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import dotenv from "dotenv"
import { resolve, join } from "node:path"
import { Spinner } from "../../libs/util/spinner.mjs"
import { createDiscordGuild } from "../../libs/client.mjs"
import {
createChannel,
getChannelFile,
createChannelFile,
} from "../../libs/channel.mjs"
import { createCategory, createCategoryFile } from "../../libs/category.mjs"
import { createChannel, getChannelFile } from "../../libs/channel.mjs"
import { createCategory } from "../../libs/category.mjs"

const __dirname = new URL(import.meta.url).pathname
const distDirPath = resolve(__dirname, "../../../.dist/")
Expand Down Expand Up @@ -69,8 +65,8 @@ interface Options {
}
spinner.success()

// チャンネルファイルを取得する
spinner.loading("Get channel file")
// チャンネルを取得する
spinner.loading("Get channel")
const { channels, ...getChannelFileResult } = await getChannelFile(
distChannelFilePath
)
Expand All @@ -87,13 +83,15 @@ interface Options {
[
{ id: "", name: "CHANNEL" },
{ id: "", name: "ARCHIVE" },
]
],
distCategoryFilePath
)
if (createCategoryResult.status === "failed") {
spinner.failed(null, createCategoryResult.message)
process.exit(0)
}

// デフォルトカテゴリーとアーカイブカテゴリーを取得する
const defaultCategory = categories.find(
(category) => category.name === "CHANNEL"
)
Expand All @@ -106,23 +104,12 @@ interface Options {
}
spinner.success()

// カテゴリーファイルを更新する
spinner.loading("Update category file")
const createCategoryFileResult = await createCategoryFile(
distCategoryFilePath,
categories
)
if (createCategoryFileResult.status === "failed") {
spinner.failed(null, createCategoryFileResult.message)
process.exit(0)
}
spinner.success()

// チャンネルを作成する
spinner.loading("Create channel")
const createChannelResult = await createChannel(
discordGuild,
channels,
distChannelFilePath,
defaultCategory,
archiveCategory,
migrateArchive
Expand All @@ -131,19 +118,6 @@ interface Options {
spinner.failed(null, createChannelResult.message)
process.exit(0)
}
const newChannels = createChannelResult.channels
spinner.success()

// チャンネルファイルを更新する
spinner.loading("Update channel file")
const createChannelFileResult = await createChannelFile(
distChannelFilePath,
newChannels
)
if (createChannelFileResult.status === "failed") {
spinner.failed(null, createChannelFileResult.message)
process.exit(0)
}
spinner.success()

process.exit(0)
Expand Down
4 changes: 2 additions & 2 deletions command/deploy/message.mts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ interface Options {
}
spinner.success()

// チャンネルファイルを取得する
spinner.loading("Get channel file")
// チャンネルを取得する
spinner.loading("Get channel")
const { channels, ...getChannelFileResult } = await getChannelFile(
distChannelFilePath
)
Expand Down
60 changes: 17 additions & 43 deletions command/destroy/channel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@ import dotenv from "dotenv"
import { resolve, join } from "node:path"
import { Spinner } from "../../libs/util/spinner.mjs"
import { createDiscordGuild } from "../../libs/client.mjs"
import {
createChannelFile,
deleteChannel,
getChannelFile,
} from "../../libs/channel.mjs"
import {
createCategoryFile,
deleteCategory,
getCategoryFile,
} from "../../libs/category.mjs"
import { deleteChannel, getChannelFile } from "../../libs/channel.mjs"
import { deleteCategory, getCategoryFile } from "../../libs/category.mjs"

const __dirname = new URL(import.meta.url).pathname
const distDirPath = resolve(__dirname, "../../../.dist/")
Expand Down Expand Up @@ -63,8 +55,8 @@ interface Options {
}
spinner.success()

// チャンネルファイルを取得する
spinner.loading("Get channel file")
// チャンネルを取得する
spinner.loading("Get channel")
const { channels, ...getChannelFileResult } = await getChannelFile(
distChannelFilePath
)
Expand All @@ -74,8 +66,8 @@ interface Options {
}
spinner.success()

// カテゴリーファイルを取得する
spinner.loading("Get category file")
// カテゴリーを取得する
spinner.loading("Get category")
const { categories, ...getCategoryFileResult } = await getCategoryFile(
distCategoryFilePath
)
Expand All @@ -87,46 +79,28 @@ interface Options {

// チャンネルを削除する
spinner.loading("Delete channel")
const deleteChannelResult = await deleteChannel(discordGuild, channels)
const deleteChannelResult = await deleteChannel(
discordGuild,
channels,
distChannelFilePath
)
if (deleteChannelResult.status === "failed") {
spinner.failed(null, deleteChannelResult.message)
process.exit(0)
}
const newChannels = deleteChannelResult.channels
spinner.success()

// チャンネルファイルを更新する
spinner.loading("Update channel file")
const createChannelFileResult = await createChannelFile(
distChannelFilePath,
newChannels
)
if (createChannelFileResult.status === "failed") {
spinner.failed(null, createChannelFileResult.message)
process.exit(0)
}
spinner.success()

// Discordのカテゴリーを削除する
// カテゴリーを削除する
spinner.loading("Delete category")
const deleteCategoryResult = await deleteCategory(discordGuild, categories)
const deleteCategoryResult = await deleteCategory(
discordGuild,
categories,
distCategoryFilePath
)
if (deleteCategoryResult.status === "failed") {
spinner.failed(null, deleteCategoryResult.message)
process.exit(0)
}
const newCategories = deleteCategoryResult.categories
spinner.success()

// カテゴリーファイルを更新する
spinner.loading("Update category file")
const createCategoryFileResult = await createCategoryFile(
distCategoryFilePath,
newCategories
)
if (createCategoryFileResult.status === "failed") {
spinner.failed(null, createCategoryFileResult.message)
process.exit(0)
}
spinner.success()

process.exit(0)
Expand Down
42 changes: 37 additions & 5 deletions libs/category.mts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,19 @@ export const createCategoryFile = async (
* Create category
* @param discordGuild
* @param categories
* @param distCategoryFilePath
*/
export const createCategory = async (
discordGuild: Guild,
categories: Category[]
categories: Category[],
distCategoryFilePath: string
): Promise<{
categories: Category[]
status: "success" | "failed"
message?: any
}> => {
try {
// カテゴリーを作成する
const newCategories: Category[] = []
for (const category of categories) {
const rusult = await discordGuild.channels.create({
Expand All @@ -78,6 +81,20 @@ export const createCategory = async (
name: category.name,
})
}

// カテゴリーファイルを作成する
const createCategoryFileResult = await createCategoryFile(
distCategoryFilePath,
categories
)
if (createCategoryFileResult.status === "failed") {
return {
categories: [],
status: "failed",
message: createCategoryFileResult.message,
}
}

return { categories: newCategories, status: "success" }
} catch (error) {
return { categories: [], status: "failed", message: error }
Expand All @@ -88,26 +105,41 @@ export const createCategory = async (
* Delete category
* @param discordGuild
* @param categories
* @param distCategoryFilePath
*/
export const deleteCategory = async (
discordGuild: Guild,
categories: Category[]
categories: Category[],
distCategoryFilePath: string
): Promise<{
categories: Category[]
status: "success" | "failed"
message?: any
}> => {
const newCategories: Category[] = []
try {
// カテゴリーを削除する
const newCategories: Category[] = []
for (const category of categories) {
if (category.id) {
// カテゴリーを削除する
await discordGuild.channels.delete(category.id)
// カテゴリーのIDを削除する
category.id = ""
}
newCategories.push(category)
}

// カテゴリーファイルを更新する
const createCategoryFileResult = await createCategoryFile(
distCategoryFilePath,
newCategories
)
if (createCategoryFileResult.status === "failed") {
return {
categories: [],
status: "failed",
message: createCategoryFileResult.message,
}
}

return { categories: newCategories, status: "success" }
} catch (error) {
return { categories: [], status: "failed", message: error }
Expand Down
Loading

0 comments on commit 9c40ce8

Please sign in to comment.