Files
jerapah-flow/packages/server/fs-store.js
T
nsrb 9985ff3440 chore(deps): update pnpm version and clean up package.json; remove unused dependencies and scripts
- Updated package manager to pnpm@11.22.0 in package.json.
- Removed unused dependencies and scripts from package.json and pnpm-lock.yaml.
- Cleaned up fs-store.js and other files by removing commented-out code and unused imports.
- Updated workflow configurations to use environment variables for URLs.
2026-08-21 21:48:24 +07:00

129 lines
3.4 KiB
JavaScript

import fs from "fs";
import path from "path";
import yaml from "yaml";
import { SCRIPTS_DIR, WORKFLOWS_DIR } from "./paths.js";
export function assertScriptName(name) {
if (typeof name !== "string" || !/^[A-Za-z0-9._-]+\.js$/.test(name)) {
const err = new Error("invalid script name");
err.statusCode = 400;
throw err;
}
return name;
}
export function assertOwner(name) {
if (typeof name !== "string" || !/^[A-Za-z0-9_-]+$/.test(name)) {
const err = new Error("invalid owner");
err.statusCode = 400;
throw err;
}
return name;
}
export function assertWorkflowFile(name) {
if (
typeof name !== "string" ||
!/^[A-Za-z0-9._-]+\.ya?ml$/.test(name) ||
name === "registers.yaml"
) {
const err = new Error("invalid workflow file");
err.statusCode = 400;
throw err;
}
return name;
}
export function listScriptFiles() {
if (!fs.existsSync(SCRIPTS_DIR)) return [];
return fs
.readdirSync(SCRIPTS_DIR)
.filter((f) => f.endsWith(".js"))
.sort();
}
export function readScript(name) {
assertScriptName(name);
const filePath = path.join(SCRIPTS_DIR, name);
if (!fs.existsSync(filePath)) return null;
return fs.readFileSync(filePath, "utf8");
}
/**
* Icon next to the script: `fetch-html.js` → `fetch-html.png` or `.jpg`.
* @returns {{ filePath: string, contentType: string } | null}
*/
export function resolveScriptIcon(name) {
assertScriptName(name);
const base = name.slice(0, -3);
for (const { ext, contentType } of [
{ ext: "png", contentType: "image/png" },
{ ext: "jpg", contentType: "image/jpeg" },
]) {
const filePath = path.join(SCRIPTS_DIR, `${base}.${ext}`);
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
return { filePath, contentType };
}
}
return null;
}
export function scriptHasIcon(name) {
return resolveScriptIcon(name) != null;
}
export function listOwners() {
if (!fs.existsSync(WORKFLOWS_DIR)) return [];
return fs
.readdirSync(WORKFLOWS_DIR, { withFileTypes: true })
.filter((d) => d.isDirectory())
.map((d) => d.name)
.sort();
}
function registersPath(owner) {
return path.join(WORKFLOWS_DIR, owner, "registers.yaml");
}
export function readRegisters(owner) {
const filePath = registersPath(owner);
if (!fs.existsSync(filePath)) return [];
const parsed = yaml.parse(fs.readFileSync(filePath, "utf8")) ?? {};
return Array.isArray(parsed.scripts) ? parsed.scripts : [];
}
export function writeRegisters(owner, files) {
const ownerDir = path.join(WORKFLOWS_DIR, owner);
fs.mkdirSync(ownerDir, { recursive: true });
fs.writeFileSync(
registersPath(owner),
yaml.stringify({ scripts: files }),
"utf8",
);
}
export function readWorkflowYaml(owner, file) {
assertOwner(owner);
assertWorkflowFile(file);
const filePath = path.join(WORKFLOWS_DIR, owner, file);
if (!fs.existsSync(filePath)) return null;
return fs.readFileSync(filePath, "utf8");
}
export function writeWorkflowYaml(owner, file, content) {
assertOwner(owner);
assertWorkflowFile(file);
const ownerDir = path.join(WORKFLOWS_DIR, owner);
fs.mkdirSync(ownerDir, { recursive: true });
fs.writeFileSync(path.join(ownerDir, file), content, "utf8");
}
export function listOwnerYamlFiles(owner) {
const ownerDir = path.join(WORKFLOWS_DIR, owner);
if (!fs.existsSync(ownerDir)) return [];
return fs
.readdirSync(ownerDir)
.filter((f) => f.endsWith(".yaml") && f !== "registers.yaml")
.sort();
}