From 8a6fca3fd45c16d1653fb8d81c606f01049693a4 Mon Sep 17 00:00:00 2001 From: Adan Toscano Date: Tue, 3 Oct 2023 14:38:36 +0100 Subject: [PATCH] feature: get only oas files --- src/services/find-oas-from-dir.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/services/find-oas-from-dir.js b/src/services/find-oas-from-dir.js index c840677..ddce8cf 100644 --- a/src/services/find-oas-from-dir.js +++ b/src/services/find-oas-from-dir.js @@ -1,7 +1,13 @@ import path from 'path'; import * as fs from 'fs'; -export default function fromDir(startPath) { +function isOas(filePath) { + const fileString = fs.readFileSync(filePath, 'utf8') + const oasRegEx = /^openapi/i + return oasRegEx.test(fileString); +} + +export default function findOasFromDir(startPath) { if (!fs.existsSync(startPath)) { console.log("no dir ", startPath); return []; @@ -9,16 +15,18 @@ export default function fromDir(startPath) { const files = fs.readdirSync(startPath); return files.reduce((acc, file) => { - const filename = path.join(startPath, file); - const stat = fs.lstatSync(filename); + const filePath = path.join(startPath, file); + const stat = fs.lstatSync(filePath); if (stat.isDirectory()) { - return [...acc, ...fromDir(filename)]; //recurse - } else if (filename.endsWith('.yaml')) { - console.log('-- found: ', filename); + return [...acc, ...findOasFromDir(filePath)]; //recurse + } + if (file.endsWith('.yaml') && isOas(filePath)) { + console.log('-- found: ', filePath); return [...acc, { filename: file, path: startPath }] - }; + } + return [] }, []); }; \ No newline at end of file