-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathls.js
More file actions
77 lines (59 loc) · 1.43 KB
/
Copy pathls.js
File metadata and controls
77 lines (59 loc) · 1.43 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const fs = require("fs");
const pathModule = require("path");
const args = process.argv.slice(2);
let mode = "normal";
let showHidden = false;
let paths = [];
for (const arg of args) {
if (arg === "-1") {
mode = "onePerLine";
} else if (arg === "-a") {
showHidden = true;
} else {
paths.push(arg);
}
}
if (paths.length === 0) {
paths.push(".");
}
function listDirectory(dir) {
let items = fs.readdirSync(dir);
if (showHidden) {
items.unshift(".", "..");
} else {
items = items.filter(item => !item.startsWith("."));
}
if (mode === "onePerLine") {
items.forEach(item => console.log(item));
} else {
console.log(items.join(" "));
}
}
function expandWildcard(input) {
if (!input.includes("*")) {
return [input];
}
const dir = pathModule.dirname(input);
const pattern = pathModule.basename(input);
const files = fs.readdirSync(dir);
return files
.filter(file => {
if (pattern === "*") {
return showHidden || !file.startsWith(".");
}
return file === pattern;
})
.map(file => pathModule.join(dir, file));
}
for (const originalPath of paths) {
const expandedPaths = expandWildcard(originalPath);
for (const currentPath of expandedPaths) {
const info = fs.statSync(currentPath);
if (info.isDirectory()) {
listDirectory(currentPath);
}
else if (info.isFile()) {
console.log(pathModule.basename(currentPath));
}
}
}