This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { readFile, rename, writeFile } from 'node:fs/promises';
|
||||
import { paths } from '../../config/paths.mjs';
|
||||
import { httpError } from '../../shared/http/httpError.mjs';
|
||||
|
||||
export async function readArticles() {
|
||||
const raw = await readFile(paths.articlesFile, 'utf8');
|
||||
const articles = JSON.parse(raw);
|
||||
|
||||
if (!Array.isArray(articles)) {
|
||||
throw httpError(500, 'Article database must be a JSON array');
|
||||
}
|
||||
|
||||
return articles;
|
||||
}
|
||||
|
||||
export async function writeArticles(articles) {
|
||||
const temporaryFile = `${paths.articlesFile}.${process.pid}.tmp`;
|
||||
await writeFile(temporaryFile, `${JSON.stringify(articles, null, 2)}\n`, 'utf8');
|
||||
await rename(temporaryFile, paths.articlesFile);
|
||||
}
|
||||
Reference in New Issue
Block a user