Skip to content

Commit

Permalink
refactor: switch n-readlines to readline
Browse files Browse the repository at this point in the history
  • Loading branch information
adantoscano committed Oct 3, 2023
1 parent 43609fc commit 20514f8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"author": "",
"license": "MPL-2.0",
"dependencies": {
"n-readlines": "^1.0.1",
"open-api-mocker": "github:adantoscano/open-api-mocker"
}
}
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const main = async () => {
const testRepoHTTPS = "https://gitlab.sngular.com/os3/manatee/sirenia.git"; // TODO: replace by user input
await cloneGitRepository(testRepoSSH);

const schemas = findOasFromDir('./tests');
const schemas = await findOasFromDir('./tests');

console.log('Schemas found:');
schemas.forEach(el => console.log(`-- ${el.filePath}`));

const openApiMocker = new OpenApiMocker({
port: 5000,
Expand Down
31 changes: 21 additions & 10 deletions src/services/find-oas-from-dir.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
import path from 'path';
import * as fs from 'fs';
import nReadlines from 'n-readlines'
import * as readline from 'readline';

function isOas(filePath) {
const line = new nReadlines(filePath);
const firstLine = line.next().toString();
async function getFirstLine(filePath) {
const readable = fs.createReadStream(filePath);
const reader = readline.createInterface({ input: readable });
const line = await new Promise((resolve) => {
reader.on('line', (line) => {
reader.close();
resolve(line);
});
});
readable.close();
return line;
}

async function isOas(filePath) {
const firstLine = await getFirstLine(filePath)
const oasRegEx = /^openapi/i
return oasRegEx.test(firstLine);
}

export default function findOasFromDir(startPath) {
export default async function findOasFromDir(startPath) {
if (!fs.existsSync(startPath)) {
console.log("no dir ", startPath);
return [];
}

const files = fs.readdirSync(startPath);
return files.reduce((acc, file) => {
return files.reduce(async (acc, file) => {
const filePath = path.join(startPath, file);
const stat = fs.lstatSync(filePath);
if (stat.isDirectory()) {
return [...acc, ...findOasFromDir(filePath)];
return [...(await acc), ...(await findOasFromDir(filePath))];
}
if (file.endsWith('.yaml') && isOas(filePath)) {
console.log('-- found: ', filePath);
return [...acc, {
if (file.endsWith('.yaml') && (await isOas(filePath))) {
return [...(await acc), {
filename: file,
path: startPath,
filePath,
Expand Down

0 comments on commit 20514f8

Please sign in to comment.