Files
harheimertc/server/utils/spielplan-publish.js
Torsten Schulz (local) 459da00820
All checks were successful
Code Analysis and Production Deploy / analyze (push) Successful in 2m47s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Successful in 2m2s
fix(import): publish season spielplan json after import
2026-05-20 18:12:32 +02:00

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
}
}