-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathls.ts
More file actions
51 lines (42 loc) · 1.89 KB
/
ls.ts
File metadata and controls
51 lines (42 loc) · 1.89 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
import { Arg, Command, Env, Opt } from "./lib/command.js";
import { stdout, stderr } from "process";
import { getDevice } from "./util.js";
import chalk from "chalk";
const cmd = new Command("List files in a directory", {
action: async (options: Record<string, string | boolean>, args: Record<string, string>, env: Env) => {
const port = options["port"] as string;
const baudrate = options["baudrate"] as string;
const socket = options["socket"] as string;
const ble = options["ble"] as string | undefined;
const path = args["path"] as string;
const directoryFlag = options["directory"] as boolean;
const sizeFlag = options["size"] as boolean;
const flags = (directoryFlag ? "d" : "") + (sizeFlag ? "s" : "");
const device = await getDevice(port, baudrate, socket, ble, env);
await device.controller.lock().catch((err) => {
stderr.write("Error locking device: " + err + "\n");
throw 1;
});
const listing = await device.uploader.listDirectory(path, flags).catch((err) => {
stderr.write("Error: " + err + "\n");
throw 1;
});
stderr.write("Listing of " + path + ":\n");
for (const [name, isDir, size] of listing) {
stdout.write(" " + (isDir ? chalk.blueBright(name) : name) + (sizeFlag ? " (" + size + " bytes)" : "") + "\n");
}
await device.controller.unlock().catch((err) => {
stderr.write("Error unlocking device: " + err + "\n");
throw 1;
});
},
args: [
new Arg("path", "Directory to list", { required: true }),
],
options: {
"directory": new Opt("list directories themselves, not their contents", { isFlag: true }),
"size": new Opt("print the allocated size of each file, in bytes", { isFlag: true })
},
chainable: true
});
export default cmd;