mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: add build qeust images script
This commit is contained in:
1 parent
8bbd5da513
commit
7b1591a6e3
8 files changed
+114
-41
No files matched your search
+45
-36
@@ -6,51 +6,60 @@ import { Quest } from '@linux-odyssey/models'
|
||||
|
||||
const questDirectory = path.join(process.cwd(), '..', 'quests')
|
||||
|
||||
export default async function loadAndUpdateQuests() {
|
||||
async function mapQuests(callback) {
|
||||
const questNames = await fs.readdir(questDirectory, {
|
||||
withFileTypes: true,
|
||||
})
|
||||
|
||||
const quests = Promise.all(
|
||||
return Promise.all(
|
||||
questNames
|
||||
.filter((dirent) => dirent.isDirectory() && !dirent.name.startsWith('.'))
|
||||
.map(async (dirent) => {
|
||||
.map((dirent) => {
|
||||
const id = dirent.name
|
||||
const questPath = path.join(questDirectory, id)
|
||||
const files = await fs.readdir(questPath)
|
||||
if (!files.includes('game.yml')) {
|
||||
throw new Error(`Quest ${id} is missing game.yml`)
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await fs.readFile(
|
||||
path.join(questPath, 'game.yml'),
|
||||
'utf-8'
|
||||
)
|
||||
|
||||
const quest = yaml.parse(body, {
|
||||
merge: true,
|
||||
})
|
||||
|
||||
const image = files.includes('Dockerfile') ? id : 'base'
|
||||
|
||||
return Quest.findByIdAndUpdate(
|
||||
id,
|
||||
{
|
||||
_id: id,
|
||||
image,
|
||||
...quest,
|
||||
},
|
||||
{
|
||||
upsert: true,
|
||||
}
|
||||
)
|
||||
} catch (error) {
|
||||
console.error(`Error parsing quest ${id}:`, error)
|
||||
throw error
|
||||
}
|
||||
return callback(id, questPath)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
return quests
|
||||
export function loadAndUpdateQuests() {
|
||||
return mapQuests(async (id, questPath) => {
|
||||
const files = await fs.readdir(questPath)
|
||||
if (!files.includes('game.yml')) {
|
||||
throw new Error(`Quest ${id} is missing game.yml`)
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await fs.readFile(path.join(questPath, 'game.yml'), 'utf-8')
|
||||
|
||||
const quest = yaml.parse(body, {
|
||||
merge: true,
|
||||
})
|
||||
|
||||
const image = files.includes('Dockerfile') ? id : 'base'
|
||||
|
||||
return Quest.findByIdAndUpdate(
|
||||
id,
|
||||
{
|
||||
_id: id,
|
||||
image,
|
||||
...quest,
|
||||
},
|
||||
{
|
||||
upsert: true,
|
||||
}
|
||||
)
|
||||
} catch (error) {
|
||||
console.error(`Error parsing quest ${id}:`, error)
|
||||
throw error
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function getQuestDockerfiles() {
|
||||
const quests = await mapQuests(async (id, questPath) => {
|
||||
const files = await fs.readdir(questPath)
|
||||
return files.includes('Dockerfile') ? { id, questPath } : null
|
||||
})
|
||||
return quests.filter((quest) => quest !== null)
|
||||
}
|
||||
Reference in new issue
Block a user