mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: use minimist to refactor cli
This commit is contained in:
1 parent
6ace18475b
commit
34d5ace664
5 files changed
+48
-41
No files matched your search
@@ -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 }
|
||||
}, {})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -4,6 +4,7 @@
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"chokidar": "^3.5.3"
|
||||
"chokidar": "^3.5.3",
|
||||
"minimist": "^1.2.8"
|
||||
}
|
||||
}
|
||||
@@ -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" ]
|
||||
@@ -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==
|
||||
|
||||
Reference in new issue
Block a user