Directly use folder name as _id in Quest

This commit is contained in:
wancat committed 2023-07-20 16:25:10 +08:00
1 parent 0d9b068678
commit 3e9e14399a
4 files changed
+21 -30

No files matched your search

+16 -25
View File
@@ -12,12 +12,12 @@ const marked = new Marked({
const questDirectory = path.join(process.cwd(), 'quests')
async function parseQuests() {
export default async function loadAndUpdateQuests() {
const questNames = await fs.readdir(questDirectory)
const quests = Promise.all(
questNames.map(async (name) => {
const fullPath = path.join(questDirectory, name, 'README.md')
questNames.map(async (id) => {
const fullPath = path.join(questDirectory, id, 'README.md')
try {
// Read and parse the README.md file.
@@ -31,14 +31,20 @@ async function parseQuests() {
const { title, order } = metadata
return {
name,
title,
order,
content: htmlContent,
}
return Quest.findByIdAndUpdate(
id,
{
_id: id,
title,
order,
content: htmlContent,
},
{
upsert: true,
}
)
} catch (error) {
console.error(`Error parsing quest ${name}:`, error)
console.error(`Error parsing quest ${id}:`, error)
throw error
}
})
@@ -46,18 +52,3 @@ async function parseQuests() {
return quests
}
async function updateQuests(quests) {
Promise.all(
quests.map((quest) => {
return Quest.findOneAndUpdate({ name: quest.name }, quest, {
upsert: true,
})
})
)
}
export default async function loadAndUpdateQuests() {
const quests = await parseQuests()
await updateQuests(quests)
}