-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.ls-advanced.js
More file actions
38 lines (31 loc) · 1.01 KB
/
8.ls-advanced.js
File metadata and controls
38 lines (31 loc) · 1.01 KB
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
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(pc.red(`No se pudo leer el archivo ${filePath}`))
process.exit(1)
}
const isDirectory = stats.isDirectory()
const fileType = isDirectory ? 'd' : 'f'
const fileSizes = stats.size.toString().padStart(10, ' ')
const fileModified = stats.mtime.toLocaleDateString()
return `${fileType} ${pc.blue(fileSizes)} ${pc.green(fileModified)} ${pc.yellow(file)}`
})
const filesInfo = await Promise.all(filesPromises)
filesInfo.forEach(fileInfo => console.log(fileInfo))
}
ls(folder)