-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathls.mjs
More file actions
126 lines (105 loc) · 2.65 KB
/
Copy pathls.mjs
File metadata and controls
126 lines (105 loc) · 2.65 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import process from "node:process";
import fs, { truncate } from "node:fs";
const argv = process.argv.slice(2);
const options = {
onePerLine: false,
all: false,
help: false,
};
const validFlags = {
onePerLine: {
short: "-1",
long: "--one-per-line",
description: "Lists one file per line",
},
all: {
short: "-a",
long: "--all",
description: "Include entries whose names begin with a dot",
},
help: {
short: "-h",
long: "--help",
description: "Display help information",
},
};
const paths = [];
let parsingFlags = true;
for (const arg of argv) {
if (arg === "--") {
parsingFlags = false;
} else if (parsingFlags && arg.startsWith("--")) {
const matchedFlag = Object.entries(validFlags).find(
([, definition]) => arg === definition.long,
);
if (matchedFlag) {
const [optionName] = matchedFlag;
options[optionName] = true;
} else {
process.stderr.write(`Invalid option -- '${arg}'\n`);
process.exit(1);
}
} else if (parsingFlags && arg.startsWith("-")) {
const shortFlags = arg.slice(1);
for (const shortFlag of shortFlags) {
const matchedFlag = Object.entries(validFlags).find(
([, definition]) => definition.short === `-${shortFlag}`,
);
if (matchedFlag) {
const [optionName] = matchedFlag;
options[optionName] = true;
} else {
process.stderr.write(`Invalid option -- '${arg}\n'`);
process.exit(1);
}
}
} else {
paths.push(arg);
}
}
if (options.help) {
process.stdout.write("Usage: ls [OPTION]... [FILE]...\n");
process.stdout.write(
"List information about the FILEs (the current directory by default).\n\n",
);
process.stdout.write("Options:\n");
for (const definition of Object.values(validFlags)) {
process.stdout.write(
` ${definition.short}, ${definition.long}\t${definition.description}\n`,
);
}
process.exit(0);
}
if (paths.length === 0) {
paths.push(".");
}
const multiplePaths = paths.length > 1;
const filePaths = [];
const directoryPaths = [];
for (const path of paths) {
const stats = fs.statSync(path);
if (stats.isDirectory()) {
directoryPaths.push(path);
} else {
filePaths.push(path);
}
}
if (filePaths.length > 0) {
const separator = options.onePerLine ? "\n" : " ";
process.stdout.write(`${filePaths.join(separator)}\n`);
}
for (const path of directoryPaths) {
if (multiplePaths) {
process.stdout.write(`\n${path}: \n`);
}
let content = fs.readdirSync(path);
if (options.all) {
content.unshift(".", "..");
} else {
content = content.filter((entry) => !entry.startsWith("."));
}
content.sort();
const separator = options.onePerLine ? "\n" : " ";
const output = content.join(separator);
process.stdout.write(`${output}\n`);
}