-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.ls-advanced.js
41 lines (33 loc) · 1.04 KB
/
8.ls-advanced.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const fs = require('node:fs/promises')
const path = require('node:path')
const pc = require('picocolors')
const folder = process.argv[2] ?? '.'
async function ls (folder) {
let files
try {
files = await fs.readdir(folder)
} catch {
console.error(pc.red(`No se pudo leer el directorio ${folder}`))
process.exit(1)
}
const filesPromises = files.map(async file => {
const filePath = path.join(folder, file)
let stats
try {
stats = await fs.stat(filePath)
} catch {
console.error(`No se pudo leer el archivo ${filePath}`)
process.exit(1)
}
const isDirectory = stats.isDirectory()
const fileType = isDirectory ? 'd' : 'f'
const fileSize = stats.size.toString()
const fileModified = stats.mtime.toLocaleString()
return `${pc.bgMagenta(fileType)} ${pc.blue(file.padEnd(20))} ${pc.green(
fileSize.toString().padStart(15)
)} ${pc.yellow(fileModified)}`
})
const filesInfo = await Promise.all(filesPromises)
filesInfo.forEach(fileInfo => console.log(fileInfo))
}
ls(folder)