UNPKG

openapi-mock-generator-cli

Version:
55 lines (54 loc) 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeFile = writeFile; exports.readOpenApiFiles = readOpenApiFiles; const fs_1 = require("fs"); const path_1 = require("path"); const logger_1 = require("./logger"); /** * Writes a JSON file to the specified path. If the directory does not exist, it creates it. * @param filePath - The path where the file will be written. * @param content - The content to write to the file. */ function writeFile(filePath, content) { const dir = (0, path_1.dirname)(filePath); if (!(0, fs_1.existsSync)(dir)) { (0, fs_1.mkdirSync)(dir, { recursive: true }); } (0, fs_1.writeFileSync)(filePath, JSON.stringify(content, null, 2), 'utf-8'); } /** * Recursively reads all files in a given folder and its subfolders. * @param folderPath The root folder to start searching for files. * @param fileExtension The file extension to filter by (e.g., ".yaml"). * @returns An array of file paths matching the given extension. */ function readFilesRecursively(folderPath, fileExtension) { const files = []; function readFolder(currentPath) { const entries = (0, fs_1.readdirSync)(currentPath, { withFileTypes: true }) || []; for (const entry of entries) { const entryPath = (0, path_1.join)(currentPath, entry.name); if (entry.isDirectory()) { readFolder(entryPath); } else if (entry.isFile() && entry.name.endsWith(fileExtension)) { logger_1.Logger.success(`Adding OpenAPI file: ${entryPath}`); files.push(entryPath); } } } readFolder(folderPath); return files; } /** * Reads all OpenAPI files in a given folder and its subfolders. * @param folderPath The root folder to start searching for OpenAPI files. * @returns An array of file paths to OpenAPI files. */ function readOpenApiFiles(folderPath) { logger_1.Logger.info(`Searching for OpenAPI files in folder: ${folderPath}`); const openApiYamlFiles = readFilesRecursively(folderPath, '.yaml'); const openApiYmlFiles = readFilesRecursively(folderPath, '.yml'); return [...openApiYamlFiles, ...openApiYmlFiles]; }