|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import type { WorkspaceConfig } from "../types/config.js"; |
| 4 | +import { logger } from "./logger.js"; |
| 5 | + |
| 6 | +const WORKSPACE_DIR = ".ub"; |
| 7 | +const CONFIG_FILE = "config.json"; |
| 8 | +const SCHEMAS_DIR = "schemas"; |
| 9 | + |
| 10 | +export function getWorkspaceDir(): string { |
| 11 | + return path.join(process.cwd(), WORKSPACE_DIR); |
| 12 | +} |
| 13 | + |
| 14 | +export function getWorkspaceConfigPath(): string { |
| 15 | + return path.join(getWorkspaceDir(), CONFIG_FILE); |
| 16 | +} |
| 17 | + |
| 18 | +export function getSchemasDir(): string { |
| 19 | + return path.join(getWorkspaceDir(), SCHEMAS_DIR); |
| 20 | +} |
| 21 | + |
| 22 | +export function loadWorkspaceConfig(): WorkspaceConfig | null { |
| 23 | + const configPath = getWorkspaceConfigPath(); |
| 24 | + if (!fs.existsSync(configPath)) { |
| 25 | + return null; |
| 26 | + } |
| 27 | + try { |
| 28 | + const raw = fs.readFileSync(configPath, "utf8"); |
| 29 | + return JSON.parse(raw) as WorkspaceConfig; |
| 30 | + } catch { |
| 31 | + return null; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export function saveWorkspaceConfig(config: WorkspaceConfig): void { |
| 36 | + const dir = getWorkspaceDir(); |
| 37 | + if (!fs.existsSync(dir)) { |
| 38 | + fs.mkdirSync(dir, { recursive: true }); |
| 39 | + } |
| 40 | + |
| 41 | + const configPath = getWorkspaceConfigPath(); |
| 42 | + fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf8"); |
| 43 | +} |
| 44 | + |
| 45 | +export function isValidCollectionName(collectionName: string): boolean { |
| 46 | + return typeof collectionName === "string" && !collectionName.includes("/") && !collectionName.includes("\\") && !collectionName.includes(".."); |
| 47 | +} |
| 48 | + |
| 49 | +export function saveSchemaFile(collectionName: string, schema: any): void { |
| 50 | + if (!isValidCollectionName(collectionName)) { |
| 51 | + throw new Error(`Invalid collection name: ${collectionName}`); |
| 52 | + } |
| 53 | + |
| 54 | + const schemasDir = getSchemasDir(); |
| 55 | + if (!fs.existsSync(schemasDir)) { |
| 56 | + fs.mkdirSync(schemasDir, { recursive: true }); |
| 57 | + } |
| 58 | + |
| 59 | + const filePath = path.join(schemasDir, `${collectionName}.json`); |
| 60 | + fs.writeFileSync(filePath, JSON.stringify(schema, null, 2), "utf8"); |
| 61 | +} |
| 62 | + |
| 63 | +export function getLocalSchemas(): { name: string; schema: any }[] { |
| 64 | + const schemasDir = getSchemasDir(); |
| 65 | + if (!fs.existsSync(schemasDir)) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + const files = fs.readdirSync(schemasDir).filter((file) => file.endsWith(".json")); |
| 70 | + const schemas = []; |
| 71 | + |
| 72 | + for (const file of files) { |
| 73 | + try { |
| 74 | + const filePath = path.join(schemasDir, file); |
| 75 | + const raw = fs.readFileSync(filePath, "utf8"); |
| 76 | + schemas.push({ |
| 77 | + name: file.replace(".json", ""), |
| 78 | + schema: JSON.parse(raw), |
| 79 | + }); |
| 80 | + } catch (err) { |
| 81 | + logger.warn(`Failed to parse schema file: ${file}. Skipping.`); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return schemas; |
| 86 | +} |
| 87 | + |
| 88 | +export function clearSchemaFiles(): void { |
| 89 | + const schemasDir = getSchemasDir(); |
| 90 | + if (!fs.existsSync(schemasDir)) return; |
| 91 | + |
| 92 | + const files = fs.readdirSync(schemasDir); |
| 93 | + for (const file of files) { |
| 94 | + if (file.endsWith(".json")) { |
| 95 | + fs.unlinkSync(path.join(schemasDir, file)); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments