-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.js
49 lines (38 loc) · 1.39 KB
/
fs.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
42
43
44
45
46
47
48
49
const fs = require('fs');
const path = require('path');
const util = require('util');
const readdir = util.promisify(fs.readdir);
const lstat = util.promisify(fs.lstat);
const realpath = util.promisify(fs.realpath);
const access = util.promisify(fs.access);
async function printDir(dir, level = 0) {
const files = await readdir(dir);
console.log(' '.padStart((level - 1) * 2), `${path.basename(dir)}/`);
for (const file of files) {
const filepath = path.join(dir, file);
const stat = await lstat(filepath);
if (!stat.isDirectory()) {
console.log(filepath)
// const canRead = await access(filepath, fs.constants.R_OK);
// const canWrite = await access(filepath, fs.constants.W_OK);
// const canDoAll = await access(filepath, fs.constants.W_OK | fs.constants.W_OK);
if (stat.isSymbolicLink()) {
const realFilepath = await realpath(filepath);
console.log(' '.padStart(level * 2), file, '=>', realFilepath);
} else {
console.log(' '.padStart(level * 2), file, stat.mode);
}
} else {
// await printDir(filepath, level + 1);
}
}
}
(async function() {
await printDir(process.argv[2]);
})();
// fs.symlink('index.js', 'index-linked.js', console.log)
// fs.mkdir('a/b/c/d', { recursive: true }, console.log)
// fs.watchFile('fs.js', (curr, prev) => {
// console.log('curr', curr);
// console.log('prev', prev);
// });