From f43fa1b5efe9761b69c8e46e519d4252b0545665 Mon Sep 17 00:00:00 2001 From: wancat Date: Fri, 11 Aug 2023 18:58:26 +0800 Subject: [PATCH] fix: limit 2 level to only 5 children --- packages/container/cli.js | 1 - packages/container/discover.js | 41 ++++++++++++++++------------ server/quests/helloworld/home/.zshrc | 4 +-- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/packages/container/cli.js b/packages/container/cli.js index 2dfcf0c..fac7eba 100644 --- a/packages/container/cli.js +++ b/packages/container/cli.js @@ -19,7 +19,6 @@ const commandListeners = { async function handleCommand(command) { const argv = minimist(command.split(' ')) - console.log(argv) const name = argv._[0] const listeners = commandListeners[name] if (!listeners) return {} diff --git a/packages/container/discover.js b/packages/container/discover.js index 4d71a84..59c6ca7 100644 --- a/packages/container/discover.js +++ b/packages/container/discover.js @@ -5,30 +5,35 @@ async function collectFilesInfo(inputPath, level = 0) { const stats = await fs.stat(inputPath) const discovered = level === 0 const name = path.basename(inputPath) - const file = { - path: inputPath, - name, - type: stats.isDirectory() ? 'folder' : 'file', - discovered, - } - if (stats.isFile()) { - return [file] - } - if (stats.isDirectory()) { - const entries = await fs.readdir(inputPath) - const result = await Promise.all( - entries.map((entry) => - collectFilesInfo(path.join(inputPath, entry), level + 1) + const files = [ + { + path: inputPath, + name, + type: stats.isDirectory() ? 'folder' : 'file', + discovered, + }, + ] + if (stats.isDirectory() && level < 2) { + try { + let entries = await fs.readdir(inputPath) + if (level > 0) { + entries = entries.slice(0, 5) + } + const result = await Promise.all( + entries.map((entry) => + collectFilesInfo(path.join(inputPath, entry), level + 1) + ) ) - ) - return [file, ...result.flat()] + files.push(...result.flat()) + } catch (err) { + // ignore + } } - return [] + return files } async function discoverFiles(argv) { const targetPath = argv._.length < 2 ? ['.'] : argv._.slice(1) - console.log(targetPath) const result = await Promise.all( targetPath.map((p) => path.resolve(process.cwd(), p)).map(collectFilesInfo) ) diff --git a/server/quests/helloworld/home/.zshrc b/server/quests/helloworld/home/.zshrc index 5ac25a5..eacba97 100644 --- a/server/quests/helloworld/home/.zshrc +++ b/server/quests/helloworld/home/.zshrc @@ -29,8 +29,6 @@ precmd() { export CMD_ERROR_FILE=/tmp/cmd.error # Now run your Node.js script with the result as an argument - node /usr/local/lib/container/cli.js - - rm /tmp/cmd.out /tmp/cmd.error 2>/dev/null # cleanup + node /usr/local/lib/container/cli.js; rm /tmp/cmd.out /tmp/cmd.error 2>/dev/null }