From 321db7e2183bf3dd93d90e65689cb319cbd22138 Mon Sep 17 00:00:00 2001 From: Adan Toscano Date: Tue, 3 Oct 2023 14:38:36 +0100 Subject: [PATCH] feat: read just the first line --- package-lock.json | 9 +++++++++ package.json | 2 ++ src/services/find-oas-from-dir.js | 8 +++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 09370e5..2ee396c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.0", "license": "MPL-2.0", "dependencies": { + "n-readlines": "^1.0.1", "open-api-mocker": "github:adantoscano/open-api-mocker" } }, @@ -955,6 +956,14 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" }, + "node_modules/n-readlines": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/n-readlines/-/n-readlines-1.0.1.tgz", + "integrity": "sha512-z4SyAIVgMy7CkgsoNw7YVz40v0g4+WWvvqy8+ZdHrCtgevcEO758WQyrYcw3XPxcLxF+//RszTz/rO48nzD0wQ==", + "engines": { + "node": ">=6.x.x" + } + }, "node_modules/negotiator": { "version": "0.6.3", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", diff --git a/package.json b/package.json index d952641..c929955 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "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 ." @@ -10,6 +11,7 @@ "author": "", "license": "MPL-2.0", "dependencies": { + "n-readlines": "^1.0.1", "open-api-mocker": "github:adantoscano/open-api-mocker" } } diff --git a/src/services/find-oas-from-dir.js b/src/services/find-oas-from-dir.js index ddce8cf..7d77707 100644 --- a/src/services/find-oas-from-dir.js +++ b/src/services/find-oas-from-dir.js @@ -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) { @@ -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);