42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
import { promises as fs } from 'fs'
|
|
import { convertImportedSpielplanToJson, validateImportedSpielplan } from './spielplan-data.js'
|
|
import { getProjectPath, getServerDataPath } from './paths.js'
|
|
|
|
const DEFAULT_INPUT_PATH = getServerDataPath('spielplan-import', 'harheimer_tc_spielplan.json')
|
|
|
|
async function writeJsonAtomic(filePath, data) {
|
|
await fs.mkdir(filePath.substring(0, filePath.lastIndexOf('/')), { recursive: true })
|
|
const tmpPath = `${filePath}.tmp-${process.pid}-${Date.now()}`
|
|
const content = `${JSON.stringify(data, null, 2)}\n`
|
|
|
|
await fs.writeFile(tmpPath, content, 'utf8')
|
|
await fs.rename(tmpPath, filePath)
|
|
}
|
|
|
|
export async function publishImportedSpielplan(options = {}) {
|
|
const inputPath = options.inputPath || DEFAULT_INPUT_PATH
|
|
const raw = await fs.readFile(inputPath, 'utf8')
|
|
const imported = JSON.parse(raw)
|
|
|
|
validateImportedSpielplan(imported)
|
|
|
|
const spielplan = convertImportedSpielplanToJson(imported)
|
|
const seasonSlug = imported.source?.season?.seasonSlug
|
|
if (!seasonSlug) {
|
|
throw new Error('Saison-Slug fehlt in der Import-Datei')
|
|
}
|
|
|
|
const internalSeasonPath = getServerDataPath('public-data', 'spielplaene', `spielplan-${seasonSlug}.json`)
|
|
const publicSeasonPath = getProjectPath('public', 'data', 'spielplaene', `spielplan-${seasonSlug}.json`)
|
|
|
|
await writeJsonAtomic(internalSeasonPath, spielplan)
|
|
await writeJsonAtomic(publicSeasonPath, spielplan)
|
|
|
|
return {
|
|
seasonSlug,
|
|
matchCount: imported.matchCount,
|
|
inputPath,
|
|
internalSeasonPath,
|
|
publicSeasonPath
|
|
}
|
|
} |