From 34d5ace6646722f62ae5f8b0dd4dd4d401f4a71f Mon Sep 17 00:00:00 2001 From: wancat Date: Fri, 11 Aug 2023 18:24:43 +0800 Subject: [PATCH] feat: use minimist to refactor cli --- packages/container/cli.js | 12 +++--- packages/container/discover.js | 60 ++++++++++++++--------------- packages/container/package.json | 3 +- server/quests/helloworld/Dockerfile | 12 +++++- yarn.lock | 2 +- 5 files changed, 48 insertions(+), 41 deletions(-) diff --git a/packages/container/cli.js b/packages/container/cli.js index 5b2c546..2dfcf0c 100644 --- a/packages/container/cli.js +++ b/packages/container/cli.js @@ -1,6 +1,7 @@ /* eslint-disable no-restricted-syntax */ const fs = require('fs').promises const { exit } = require('process') +const minimist = require('minimist') const { colorize, printResponses } = require('./print.js') const discoverFiles = require('./discover.js') @@ -12,21 +13,18 @@ async function readOrNone(file) { } } -function parseCommand(command) { - const [name, ...args] = command.trim().split(' ') - return { name, args } -} - const commandListeners = { ls: [discoverFiles], } async function handleCommand(command) { - const { name, args } = parseCommand(command) + const argv = minimist(command.split(' ')) + console.log(argv) + const name = argv._[0] const listeners = commandListeners[name] if (!listeners) return {} return listeners.reduce(async (result, listener) => { - const res = await listener(name, args) + const res = await listener(argv) return { ...res, ...result } }, {}) } diff --git a/packages/container/discover.js b/packages/container/discover.js index 06d9d28..4d71a84 100644 --- a/packages/container/discover.js +++ b/packages/container/discover.js @@ -1,38 +1,38 @@ const fs = require('fs').promises const path = require('path') -async function collectFilesInfo(directoryPath, level = 0) { - const entries = await fs.readdir(directoryPath, { withFileTypes: true }) - - const promises = entries.map(async (entry) => { - const filePath = path.join(directoryPath, entry.name) - const isDirectory = entry.isDirectory() - const discovered = level === 0 && !entry.name.startsWith('.') - - const file = { - path: filePath, - name: entry.name, - type: isDirectory ? 'folder' : 'file', - discovered, - } - - if (isDirectory && level < 2) { - // Get up to two levels deep - const subFilesInfo = await collectFilesInfo(filePath, level + 1) - return [file, ...subFilesInfo] - } - - return file - }) - - const results = await Promise.all(promises) - return results.flat() +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) + ) + ) + return [file, ...result.flat()] + } + return [] } -async function discoverFiles(name, args) { - let targetPath = args[0] || '.' - targetPath = path.resolve(process.cwd(), targetPath) - return { discover: await collectFilesInfo(targetPath) } +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) + ) + return { discover: result.flat() } } module.exports = discoverFiles diff --git a/packages/container/package.json b/packages/container/package.json index f6089d0..2c6bc8a 100644 --- a/packages/container/package.json +++ b/packages/container/package.json @@ -4,6 +4,7 @@ "main": "index.js", "license": "MIT", "dependencies": { - "chokidar": "^3.5.3" + "chokidar": "^3.5.3", + "minimist": "^1.2.8" } } diff --git a/server/quests/helloworld/Dockerfile b/server/quests/helloworld/Dockerfile index 86b5ae6..cd7b038 100644 --- a/server/quests/helloworld/Dockerfile +++ b/server/quests/helloworld/Dockerfile @@ -6,6 +6,16 @@ RUN apt-get update && apt-get install -y \ zsh \ jq +WORKDIR /usr/local/lib/container + +COPY ./packages/container/package.json ./yarn.lock ./ + +RUN yarn install --modules-folder /usr/src/node_modules + +ENV NODE_PATH=/usr/src/node_modules + +COPY ./packages/container /usr/local/lib/container + RUN adduser --disabled-password --gecos '' --shell /bin/zsh rudeus USER rudeus @@ -14,6 +24,4 @@ WORKDIR /home/rudeus COPY ./server/quests/helloworld/home /home/rudeus/ -COPY ./packages/container /usr/local/lib/container - CMD [ "/bin/zsh" ] diff --git a/yarn.lock b/yarn.lock index e66d6dc..264332c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3509,7 +3509,7 @@ minimatch@^3.0.4, minimatch@^3.0.5, minimatch@^3.1.1, minimatch@^3.1.2: dependencies: brace-expansion "^1.1.7" -minimist@^1.2.0, minimist@^1.2.6: +minimist@^1.2.0, minimist@^1.2.6, minimist@^1.2.8: version "1.2.8" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c" integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==