mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +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"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user