UNPKG

touhou-tagger

Version:

从 THBWiki 自动填写东方 Project CD 曲目信息.

34 lines (33 loc) 1.12 kB
import { existsSync } from 'fs'; import { readFile, writeFile } from 'fs/promises'; import { resolve } from 'path'; const AlbumOptionsFileName = 'thtag.json'; export const getAlbumOptions = async (workingDir, baseOptions = {}) => { const albumOptionsPath = resolve(workingDir, AlbumOptionsFileName); if (!existsSync(albumOptionsPath)) { return baseOptions; } try { const albumOptions = JSON.parse(await readFile(albumOptionsPath, { encoding: 'utf-8' })); return { ...baseOptions, ...albumOptions, }; } catch { return baseOptions; } }; export const setAlbumOptions = async (workingDir, options) => { const albumOptionsPath = resolve(workingDir, AlbumOptionsFileName); try { const albumOptions = JSON.parse(await readFile(albumOptionsPath, { encoding: 'utf-8' })); await writeFile(albumOptionsPath, JSON.stringify({ ...albumOptions, ...options, }, undefined, 2)); } catch { await writeFile(albumOptionsPath, JSON.stringify(options, undefined, 2)); } };