69 lines
2.0 KiB
JavaScript
69 lines
2.0 KiB
JavaScript
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
const projectRoot = process.cwd()
|
|
const dataRoot = path.join(projectRoot, 'public', 'Data')
|
|
const outputPath = path.join(projectRoot, 'public', 'data.json')
|
|
|
|
const isDirectory = (target) => {
|
|
try {
|
|
return fs.statSync(target).isDirectory()
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
const listDirectories = (target) => {
|
|
if (!isDirectory(target)) return []
|
|
return fs.readdirSync(target).filter((entry) => isDirectory(path.join(target, entry)))
|
|
}
|
|
|
|
const buildManifest = () => {
|
|
const games = []
|
|
const gameNames = listDirectories(dataRoot)
|
|
gameNames.forEach((gameName) => {
|
|
const gamePath = path.join(dataRoot, gameName)
|
|
const categories = []
|
|
const categoryNames = listDirectories(gamePath)
|
|
categoryNames.forEach((categoryName) => {
|
|
const songsDir = path.join(gamePath, categoryName, 'Songs')
|
|
const answersDir = path.join(gamePath, categoryName, 'Answers')
|
|
const clues = []
|
|
for (let i = 1; i <= 5; i += 1) {
|
|
const songPath = path.join('Data', gameName, categoryName, 'Songs', `${i}.mp3`)
|
|
const answerPath = path.join('Data', gameName, categoryName, 'Answers', `${i}.mp3`)
|
|
const songExists = fs.existsSync(path.join(dataRoot, gameName, categoryName, 'Songs', `${i}.mp3`))
|
|
const answerExists = fs.existsSync(
|
|
path.join(dataRoot, gameName, categoryName, 'Answers', `${i}.mp3`)
|
|
)
|
|
clues.push({
|
|
number: i,
|
|
points: i * 100,
|
|
song: songExists ? `/${songPath.replace(/\\/g, '/')}` : null,
|
|
answer: answerExists ? `/${answerPath.replace(/\\/g, '/')}` : null
|
|
})
|
|
}
|
|
categories.push({
|
|
name: categoryName,
|
|
clues
|
|
})
|
|
})
|
|
games.push({
|
|
name: gameName,
|
|
categories
|
|
})
|
|
})
|
|
return games
|
|
}
|
|
|
|
const main = () => {
|
|
if (!isDirectory(dataRoot)) {
|
|
fs.writeFileSync(outputPath, JSON.stringify([], null, 2))
|
|
return
|
|
}
|
|
const manifest = buildManifest()
|
|
fs.writeFileSync(outputPath, JSON.stringify(manifest, null, 2))
|
|
}
|
|
|
|
main()
|