29 lines
1.3 KiB
JavaScript
29 lines
1.3 KiB
JavaScript
// 把预设文件导入正在运行的应用(默认 127.0.0.1:23337),导入后不切换当前预设。
|
|
// 用法: node import-to-app.mjs <preset.json> [--port 23337]
|
|
import { readFileSync } from "node:fs";
|
|
import path from "node:path";
|
|
|
|
const file = process.argv[2];
|
|
const portIndex = process.argv.indexOf("--port");
|
|
const port = portIndex > 0 ? Number(process.argv[portIndex + 1]) : 23337;
|
|
const raw = JSON.parse(readFileSync(file, "utf8"));
|
|
const name = path.basename(file, ".json");
|
|
|
|
const res = await fetch("http://127.0.0.1:" + port + "/api/presets/import", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ raw, name, activate: false }),
|
|
});
|
|
const text = await res.text();
|
|
let data = null;
|
|
try { data = JSON.parse(text); } catch (e) { data = { raw: text.slice(0, 800) }; }
|
|
if (!res.ok) {
|
|
console.error("导入失败 HTTP " + res.status + ": " + JSON.stringify(data).slice(0, 800));
|
|
process.exitCode = 1;
|
|
} else {
|
|
const p = data.preset || {};
|
|
console.log("已导入: " + (p.name || name) + " id=" + (p.id || "?"));
|
|
console.log("条目数: " + ((p.prompts || []).length || "?") + " 正文合计: " + ((p.prompts || []).reduce((n, x) => n + String(x.content || "").length, 0) || "?") + " 字");
|
|
if (data.warnings && data.warnings.length) console.log("导入提示:\n - " + data.warnings.join("\n - "));
|
|
}
|