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