-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathget-examples.ts
More file actions
85 lines (70 loc) · 2.63 KB
/
get-examples.ts
File metadata and controls
85 lines (70 loc) · 2.63 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
import { Arg, Command, Env } from "./lib/command.js";
import { stderr } from "process";
import { getDevice } from "./util.js";
import fs from "fs";
import * as tar from "tar-stream";
import * as zlib from "zlib";
import path from "path";
const cmd = new Command("Get example project from device", {
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 device = await getDevice(port, baudrate, socket, ble, env);
await device.controller.lock().catch((err) => {
stderr.write("Error locking device: " + err + "\n");
throw 1;
});
const data = await device.uploader.readResource("ts-examples").catch((err) => {
stderr.write("Error: " + err + "\n");
throw 1;
});
await device.controller.unlock().catch((err) => {
stderr.write("Error unlocking device: " + err + "\n");
throw 1;
});
const extract = tar.extract();
await new Promise((resolve, reject) => {
extract.on("entry", (header, stream, next) => {
if (header.type !== "file") {
next();
return;
}
const chunks: Buffer[] = [];
stream.on("data", (chunk) => {
chunks.push(chunk);
});
stream.on("end", () => {
const data = Buffer.concat(chunks);
const outpath = path.join(path_, header.name);
const dir = path.dirname(outpath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(outpath, data);
next();
});
stream.on("error", (err) => {
reject(err);
});
});
extract.on("finish", () => {
resolve(null);
});
extract.on("error", (err) => {
reject(err);
});
const gunzip = zlib.createGunzip();
gunzip.pipe(extract);
gunzip.write(data);
gunzip.end();
});
},
args: [
new Arg("path", "Name of the resource", { required: true }),
],
chainable: true
});
export default cmd;