-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (39 loc) · 1.15 KB
/
index.js
File metadata and controls
46 lines (39 loc) · 1.15 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
39
40
41
42
43
#!/usr/bin/env node
const fs = require('fs');
const util = require('util');
const chalk = require('chalk');
const path = require('path');
//method # 2
// const lstat = util.promisify(fs.lstat);
//const lstat = fs.promises.lstat;
//Method # 3
const {lstat} = fs.promises;
const targetDir = process.argv[2] || process.cwd();
// method 1 for wrapping lstat in a promise
// const lstat = (filename) => {
// return new Promise((resolve, reject) => {
// fs.lstat(filename, (err, stats)=> {
// if(err){
// reject(err);
// }
// resolve(stats);
// });
// });
// }
fs.readdir(targetDir, async (err, filenames) => {
if(err){
console.log(err);
}
const statPromises = filenames.map(filename => {
return lstat(path.join(targetDir, filename));
});
const allStats = await Promise.all(statPromises);
for(let stats of allStats){
const index = allStats.indexOf(stats);
if(stats.isFile()){
console.log(chalk.blueBright(filenames[index]));
} else {
console.log(chalk.redBright(filenames[index]));
}
}
});