mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03: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 */
|
/* eslint-disable no-restricted-syntax */
|
||||||
const fs = require('fs').promises
|
const fs = require('fs').promises
|
||||||
const { exit } = require('process')
|
const { exit } = require('process')
|
||||||
|
const minimist = require('minimist')
|
||||||
const { colorize, printResponses } = require('./print.js')
|
const { colorize, printResponses } = require('./print.js')
|
||||||
const discoverFiles = require('./discover.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 = {
|
const commandListeners = {
|
||||||
ls: [discoverFiles],
|
ls: [discoverFiles],
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleCommand(command) {
|
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]
|
const listeners = commandListeners[name]
|
||||||
if (!listeners) return {}
|
if (!listeners) return {}
|
||||||
return listeners.reduce(async (result, listener) => {
|
return listeners.reduce(async (result, listener) => {
|
||||||
const res = await listener(name, args)
|
const res = await listener(argv)
|
||||||
return { ...res, ...result }
|
return { ...res, ...result }
|
||||||
}, {})
|
}, {})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +1,38 @@
|
|||||||
const fs = require('fs').promises
|
const fs = require('fs').promises
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
|
||||||
async function collectFilesInfo(directoryPath, level = 0) {
|
async function collectFilesInfo(inputPath, level = 0) {
|
||||||
const entries = await fs.readdir(directoryPath, { withFileTypes: true })
|
const stats = await fs.stat(inputPath)
|
||||||
|
const discovered = level === 0
|
||||||
const promises = entries.map(async (entry) => {
|
const name = path.basename(inputPath)
|
||||||
const filePath = path.join(directoryPath, entry.name)
|
const file = {
|
||||||
const isDirectory = entry.isDirectory()
|
path: inputPath,
|
||||||
const discovered = level === 0 && !entry.name.startsWith('.')
|
name,
|
||||||
|
type: stats.isDirectory() ? 'folder' : 'file',
|
||||||
const file = {
|
discovered,
|
||||||
path: filePath,
|
}
|
||||||
name: entry.name,
|
if (stats.isFile()) {
|
||||||
type: isDirectory ? 'folder' : 'file',
|
return [file]
|
||||||
discovered,
|
}
|
||||||
}
|
if (stats.isDirectory()) {
|
||||||
|
const entries = await fs.readdir(inputPath)
|
||||||
if (isDirectory && level < 2) {
|
const result = await Promise.all(
|
||||||
// Get up to two levels deep
|
entries.map((entry) =>
|
||||||
const subFilesInfo = await collectFilesInfo(filePath, level + 1)
|
collectFilesInfo(path.join(inputPath, entry), level + 1)
|
||||||
return [file, ...subFilesInfo]
|
)
|
||||||
}
|
)
|
||||||
|
return [file, ...result.flat()]
|
||||||
return file
|
}
|
||||||
})
|
return []
|
||||||
|
|
||||||
const results = await Promise.all(promises)
|
|
||||||
return results.flat()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function discoverFiles(name, args) {
|
async function discoverFiles(argv) {
|
||||||
let targetPath = args[0] || '.'
|
const targetPath = argv._.length < 2 ? ['.'] : argv._.slice(1)
|
||||||
targetPath = path.resolve(process.cwd(), targetPath)
|
console.log(targetPath)
|
||||||
return { discover: await collectFilesInfo(targetPath) }
|
const result = await Promise.all(
|
||||||
|
targetPath.map((p) => path.resolve(process.cwd(), p)).map(collectFilesInfo)
|
||||||
|
)
|
||||||
|
return { discover: result.flat() }
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = discoverFiles
|
module.exports = discoverFiles
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"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 \
|
zsh \
|
||||||
jq
|
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
|
RUN adduser --disabled-password --gecos '' --shell /bin/zsh rudeus
|
||||||
|
|
||||||
USER rudeus
|
USER rudeus
|
||||||
@@ -14,6 +24,4 @@ WORKDIR /home/rudeus
|
|||||||
|
|
||||||
COPY ./server/quests/helloworld/home /home/rudeus/
|
COPY ./server/quests/helloworld/home /home/rudeus/
|
||||||
|
|
||||||
COPY ./packages/container /usr/local/lib/container
|
|
||||||
|
|
||||||
CMD [ "/bin/zsh" ]
|
CMD [ "/bin/zsh" ]
|
||||||
@@ -3509,7 +3509,7 @@ minimatch@^3.0.4, minimatch@^3.0.5, minimatch@^3.1.1, minimatch@^3.1.2:
|
|||||||
dependencies:
|
dependencies:
|
||||||
brace-expansion "^1.1.7"
|
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"
|
version "1.2.8"
|
||||||
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
|
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
|
||||||
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==
|
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==
|
||||||
|
|||||||
Reference in new issue
Block a user