UNPKG

microcms_sdk_generator

Version:
31 lines (30 loc) 1.31 kB
import * as dntShim from "./_dnt.shims.js"; import { join } from "./deps/deno.land/std@0.182.0/path/mod.js"; import { existsSync, expandGlobSync, } from "./deps/deno.land/std@0.182.0/fs/mod.js"; import { ApiSchemaSchema } from "./schemaParser.js"; import { walkSync } from "./deps/deno.land/std@0.182.0/fs/walk.js"; const apiTypes = ["list", "object"]; export function parseSchemaFiles(dirPath) { return apiTypes .flatMap((apiType) => { const dir = join(dirPath, apiType); if (!existsSync(dir)) { console.warn(`Directory ${dir} does not exist.`); return []; } walkSync(dir, { includeDirs: false, exts: [".json"] }); const schemaFiles = Array.from(expandGlobSync(join(dir, "*.json"))); return schemaFiles.map((entry) => { const schemaFileName = entry.name; const schemaFilePath = entry.path; const schemaFileContent = dntShim.Deno.readTextFileSync(schemaFilePath); const apiSchema = ApiSchemaSchema.parse(JSON.parse(schemaFileContent)); return { endpointName: schemaFileName.replace(/\..*/, ""), apiSchema, apiType, }; }); }) .sort((a, b) => a.endpointName.localeCompare(b.endpointName)); }