Skip to content

Commit

Permalink
feat: read just the first line
Browse files Browse the repository at this point in the history
  • Loading branch information
adantoscano committed Oct 3, 2023
1 parent 8a6fca3 commit 321db7e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"version": "0.0.0",
"description": "Create mocks servers with its schemas",
"main": "src/index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ."
},
"author": "",
"license": "MPL-2.0",
"dependencies": {
"n-readlines": "^1.0.1",
"open-api-mocker": "github:adantoscano/open-api-mocker"
}
}
8 changes: 5 additions & 3 deletions src/services/find-oas-from-dir.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import path from 'path';
import * as fs from 'fs';
import nReadlines from 'n-readlines'

function isOas(filePath) {
const fileString = fs.readFileSync(filePath, 'utf8')
const line = new nReadlines(filePath);
const firstLine = line.next().toString();
const oasRegEx = /^openapi/i
return oasRegEx.test(fileString);
return oasRegEx.test(firstLine);
}

export default function findOasFromDir(startPath) {
Expand All @@ -18,7 +20,7 @@ export default function findOasFromDir(startPath) {
const filePath = path.join(startPath, file);
const stat = fs.lstatSync(filePath);
if (stat.isDirectory()) {
return [...acc, ...findOasFromDir(filePath)]; //recurse
return [...acc, ...findOasFromDir(filePath)];
}
if (file.endsWith('.yaml') && isOas(filePath)) {
console.log('-- found: ', filePath);
Expand Down

0 comments on commit 321db7e

Please sign in to comment.