Files
linux-odyssey/server/src/utils/quest.js
T
wancat 0de4b6ac9d feat: design game file and parse it to mongo
* 取消用 README 來寫關卡說明
2023-08-08 20:38:27 +08:00

46 lines
1.0 KiB
JavaScript

import fs from 'fs/promises'
import path from 'path'
import yaml from 'yaml'
import Quest from '../models/quest.js'
const questDirectory = path.join(process.cwd(), 'quests')
export default async function loadAndUpdateQuests() {
const questNames = await fs.readdir(questDirectory)
const quests = Promise.all(
questNames.map(async (id) => {
// Check if id is directory
const stat = await fs.stat(path.join(questDirectory, id))
if (!stat.isDirectory()) {
return null
}
const fullPath = path.join(questDirectory, id, 'game.yml')
try {
// Read and parse the README.md file.
const body = await fs.readFile(fullPath, 'utf-8')
const quest = yaml.parse(body)
return Quest.findByIdAndUpdate(
id,
{
id,
...quest,
},
{
upsert: true,
}
)
} catch (error) {
console.error(`Error parsing quest ${id}:`, error)
throw error
}
})
)
return quests
}