Skip to content

Commit

Permalink
feature: get only oas files
Browse files Browse the repository at this point in the history
  • Loading branch information
adantoscano committed Oct 3, 2023
1 parent 367edbd commit 8a6fca3
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/services/find-oas-from-dir.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
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 [];
}

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 []
}, []);
};

0 comments on commit 8a6fca3

Please # to comment.