- 계산 환경을 기본값으로 저장해 새 내역서에 적용 - 저장 작업을 data/removed 로 옮겨 목록에서만 뺌 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
55 lines
2.0 KiB
JavaScript
55 lines
2.0 KiB
JavaScript
// 원본 양식목록(TFRMFORMATFILELIST: 등록·삭제·열기)의 사용자 양식 보관함 — 이 앱 data/forms.json 한 파일.
|
|
// 종류(kind)별 이름 목록. 같은 종류·이름이면 바꿔 쓴다.
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const kinds = new Set(["wonga", "document", "machformula", "options"]);
|
|
const LIMIT = 2 * 1024 * 1024;
|
|
|
|
function file(directory) {
|
|
return path.join(directory, "forms.json");
|
|
}
|
|
function read(directory) {
|
|
const target = file(directory);
|
|
if (!fs.existsSync(target)) return [];
|
|
const list = JSON.parse(fs.readFileSync(target, "utf8"));
|
|
return Array.isArray(list) ? list : [];
|
|
}
|
|
function write(directory, list) {
|
|
fs.mkdirSync(directory, { recursive: true });
|
|
const target = file(directory),
|
|
temporary = `${target}.tmp`;
|
|
fs.writeFileSync(temporary, JSON.stringify(list), "utf8");
|
|
fs.renameSync(temporary, target);
|
|
}
|
|
function check(kind, name) {
|
|
if (!kinds.has(kind)) throw Error("양식 종류가 올바르지 않습니다.");
|
|
if (typeof name !== "string" || !name.trim() || name.length > 100)
|
|
throw Error("양식 이름은 1~100자입니다.");
|
|
}
|
|
function save(directory, entry) {
|
|
check(entry?.kind, entry?.name);
|
|
if (entry.data == null) throw Error("양식 내용이 없습니다.");
|
|
if (JSON.stringify(entry.data).length > LIMIT) throw Error("양식은 2MB 이하만 등록합니다.");
|
|
const list = read(directory).filter((f) => !(f.kind === entry.kind && f.name === entry.name));
|
|
list.push({
|
|
kind: entry.kind,
|
|
name: entry.name.trim(),
|
|
form: entry.form ?? null,
|
|
data: entry.data,
|
|
saved: new Date().toISOString(),
|
|
});
|
|
write(directory, list);
|
|
return list;
|
|
}
|
|
function remove(directory, kind, name) {
|
|
check(kind, name);
|
|
const list = read(directory),
|
|
next = list.filter((f) => !(f.kind === kind && f.name === name));
|
|
if (next.length === list.length) throw Error("지울 양식이 없습니다.");
|
|
write(directory, next);
|
|
return next;
|
|
}
|
|
|
|
module.exports = { read, save, remove };
|