mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
const fs = require('fs').promises
|
|
const path = require('path')
|
|
|
|
async function collectFilesInfo(inputPath, level = 0, hiddenFiles = false) {
|
|
const stats = await fs.stat(inputPath)
|
|
const name = path.basename(inputPath)
|
|
const discovered = level <= 1 && (hiddenFiles || !name.startsWith('.'))
|
|
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, hiddenFiles)
|
|
)
|
|
)
|
|
files.push(...result.flat())
|
|
} catch (err) {
|
|
// ignore
|
|
}
|
|
}
|
|
return files
|
|
}
|
|
|
|
async function discoverFiles(argv) {
|
|
console.log(argv)
|
|
const targetPath = argv._.length < 2 ? ['.'] : argv._.slice(1)
|
|
const result = await Promise.all(
|
|
targetPath
|
|
.map((p) => path.resolve(process.cwd(), p))
|
|
.map((p) => collectFilesInfo(p, 0, argv.a || argv.all))
|
|
)
|
|
return { discover: result.flat() }
|
|
}
|
|
|
|
module.exports = discoverFiles
|