From 27a41510533ae8d37bfd9f738e8a29c707de9877 Mon Sep 17 00:00:00 2001 From: devthejo Date: Sat, 16 Dec 2023 00:01:28 +0100 Subject: [PATCH] fix: ignore hidden folders from oa --- packages/core/libs/build.js | 4 +-- packages/core/utils/fs/dirtree2static.js | 25 ++++++++++--------- plugins/oa/index.js | 31 ++++++++++++++---------- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/packages/core/libs/build.js b/packages/core/libs/build.js index a249848..04a14ce 100644 --- a/packages/core/libs/build.js +++ b/packages/core/libs/build.js @@ -39,11 +39,11 @@ function treeFileLoader(filename) { return `require("${src}")` } -function buildDirTree(treeDirs) { +function buildDirTree(treeDirs, { filter } = {}) { for (const { dir, pattern, dirName } of treeDirs) { const dirPath = path.resolve(srcDir, dir) const content = fs.existsSync(dirPath) - ? dirtree2static(dirPath, { pattern, loader: treeFileLoader }) + ? dirtree2static(dirPath, { pattern, loader: treeFileLoader, filter }) : JSON.stringify({}) const dest = path.join(buildDir, `${dirName || dir}.js`) ensureFileHasDir(dest) diff --git a/packages/core/utils/fs/dirtree2static.js b/packages/core/utils/fs/dirtree2static.js index e31fe53..8430c69 100644 --- a/packages/core/utils/fs/dirtree2static.js +++ b/packages/core/utils/fs/dirtree2static.js @@ -28,19 +28,22 @@ module.exports = function dirtree2static( defaultsDeep(options, defaultOptions) rootDir = filename } - const { pattern, loader } = options + const { pattern, loader, filter = () => true } = options const stats = fs.lstatSync(filename) if (stats.isDirectory()) { - const files = fs.readdirSync(filename).filter((file) => { - const absPath = `${filename}/${file}` - const relPath = absPath.slice(rootDir.length) - return ( - (typeof pattern === "function" - ? pattern(relPath) - : relPath.match(pattern)) || - fs.lstatSync(`${filename}/${file}`).isDirectory() - ) - }) + const files = fs + .readdirSync(filename) + .filter(filter) + .filter((file) => { + const absPath = `${filename}/${file}` + const relPath = absPath.slice(rootDir.length) + return ( + (typeof pattern === "function" + ? pattern(relPath) + : relPath.match(pattern)) || + fs.lstatSync(`${filename}/${file}`).isDirectory() + ) + }) const results = {} for (const file of files) { diff --git a/plugins/oa/index.js b/plugins/oa/index.js index ae761f7..51204c8 100644 --- a/plugins/oa/index.js +++ b/plugins/oa/index.js @@ -86,20 +86,25 @@ module.exports.build = (options = {}) => { compileDirList(apiPath) - buildDirTree([ - { - dir: apiPath, - pattern: - /^\/v\d+\/(formats|operations|security|spec|validators|services)\/(.*)\.(js|yaml|yml)$/, - dirName: "api", - }, + buildDirTree( + [ + { + dir: apiPath, + pattern: + /^\/v\d+\/(formats|operations|security|spec|validators|services)\/(.*)\.(js|yaml|yml)$/, + dirName: "api", + }, + { + dir: sharedApiPath, + pattern: + /^\/(formats|operations|security|spec|validators|services)\/(.*)\.(js|yaml|yml)$/, + dirName: "sharedApi", + }, + ], { - dir: sharedApiPath, - pattern: - /^\/(formats|operations|security|spec|validators|services)\/(.*)\.(js|yaml|yml)$/, - dirName: "sharedApi", - }, - ]) + filter: (p) => !/(^|\/)\.[^/]+/.test(p), + } + ) fs.copySync( path.dirname(require.resolve("swagger-ui-dist")),