forked from wangziqi/gongxue-base
feat: harden pocketbase migration readiness checks
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
export interface PocketBaseField {
|
||||
name: string;
|
||||
@@ -24,8 +25,20 @@ export interface PocketBaseCollection {
|
||||
deleteRule?: string;
|
||||
}
|
||||
|
||||
export function readPocketBaseSchema(schemaPath = process.env.PB_SCHEMA_PATH || '../../docs/pb_schema.json') {
|
||||
const absPath = path.resolve(process.cwd(), schemaPath);
|
||||
function defaultSchemaPath() {
|
||||
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
const candidates = [
|
||||
path.resolve(process.cwd(), process.env.PB_SCHEMA_PATH || ''),
|
||||
path.resolve(process.cwd(), '../../docs/pb_schema.json'),
|
||||
path.resolve(process.cwd(), 'docs/pb_schema.json'),
|
||||
path.resolve(moduleDir, '../../../docs/pb_schema.json'),
|
||||
].filter(candidate => candidate && candidate !== process.cwd());
|
||||
|
||||
return candidates.find(candidate => fs.existsSync(candidate)) || candidates[candidates.length - 1];
|
||||
}
|
||||
|
||||
export function readPocketBaseSchema(schemaPath = process.env.PB_SCHEMA_PATH || '') {
|
||||
const absPath = schemaPath ? path.resolve(process.cwd(), schemaPath) : defaultSchemaPath();
|
||||
const raw = JSON.parse(fs.readFileSync(absPath, 'utf8'));
|
||||
const collections = (Array.isArray(raw) ? raw : raw.collections || []) as PocketBaseCollection[];
|
||||
const byId = new Map(collections.map(c => [c.id, c]));
|
||||
|
||||
Reference in New Issue
Block a user