重构数据库版本
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import { MOCK_DIR } from "./constants";
|
||||
import { loadMockFilePathsFromDb, upsertMockFilePathToDb } from "./db";
|
||||
import type { MockFileItem } from "./types";
|
||||
|
||||
export function normalizeMockFilePath(filePath: string): string {
|
||||
const trimmed = filePath.trim().replace(/\\/g, "/");
|
||||
if (!trimmed) {
|
||||
throw new Error("filePath is required");
|
||||
}
|
||||
const relative = trimmed.startsWith("mock/") ? trimmed : `mock/${trimmed}`;
|
||||
if (path.isAbsolute(relative)) {
|
||||
throw new Error("filePath must be a relative path");
|
||||
}
|
||||
return relative;
|
||||
}
|
||||
|
||||
export function resolveMockFullPath(relativePath: string): string {
|
||||
const normalized = normalizeMockFilePath(relativePath);
|
||||
const fullPath = path.resolve(__dirname, "..", normalized);
|
||||
const mockRoot = path.resolve(MOCK_DIR);
|
||||
if (!fullPath.startsWith(mockRoot)) {
|
||||
throw new Error("filePath is invalid");
|
||||
}
|
||||
if (path.basename(fullPath) === "api-list.json") {
|
||||
throw new Error("api-list.json is read-only in this panel");
|
||||
}
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
export function walkMockFiles(
|
||||
dir: string,
|
||||
baseDir: string,
|
||||
result: string[],
|
||||
): void {
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
walkMockFiles(fullPath, baseDir, result);
|
||||
continue;
|
||||
}
|
||||
if (!entry.isFile()) continue;
|
||||
if (entry.name === "api-list.json") continue;
|
||||
if (entry.name.endsWith(".sqlite3")) continue;
|
||||
const relative = path.relative(baseDir, fullPath).replace(/\\/g, "/");
|
||||
result.push(`mock/${relative}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadMockFiles(): Promise<MockFileItem[]> {
|
||||
let files = await loadMockFilePathsFromDb();
|
||||
files = files.filter(
|
||||
(item) =>
|
||||
item.file_path !== "mock/api-list.json" &&
|
||||
!item.file_path.endsWith(".sqlite3"),
|
||||
);
|
||||
if (files.length === 0 && fs.existsSync(MOCK_DIR)) {
|
||||
const fsFiles: string[] = [];
|
||||
walkMockFiles(MOCK_DIR, MOCK_DIR, fsFiles);
|
||||
fsFiles.sort((a, b) => a.localeCompare(b));
|
||||
for (const filePath of fsFiles) {
|
||||
await upsertMockFilePathToDb(filePath);
|
||||
}
|
||||
files = fsFiles.map((filePath) => ({ file_path: filePath, alias: "" }));
|
||||
}
|
||||
|
||||
const result: MockFileItem[] = [];
|
||||
for (const item of files) {
|
||||
const filePath = item.file_path;
|
||||
const fullPath = path.join(__dirname, "..", filePath);
|
||||
if (!fs.existsSync(fullPath)) continue;
|
||||
result.push({
|
||||
filePath,
|
||||
alias: String(item.alias || ""),
|
||||
content: fs.readFileSync(fullPath, "utf-8"),
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function initMockFilesFromFsIfNeeded(): Promise<void> {
|
||||
const filesInDb = await loadMockFilePathsFromDb();
|
||||
if (filesInDb.length > 0 || !fs.existsSync(MOCK_DIR)) return;
|
||||
if (!fs.existsSync(MOCK_DIR)) {
|
||||
return;
|
||||
}
|
||||
const files: string[] = [];
|
||||
walkMockFiles(MOCK_DIR, MOCK_DIR, files);
|
||||
files.sort((a, b) => a.localeCompare(b));
|
||||
for (const filePath of files) {
|
||||
await upsertMockFilePathToDb(filePath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user