-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
55 lines (46 loc) · 1.55 KB
/
test.ts
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
import { spawn } from "child_process";
import Path from "path";
import expandTilde from "expand-tilde";
function spawnAsync(command: string, args: string[], options = {}): Promise<{ stdout: string; stderr: string }> {
return new Promise((resolve, reject) => {
const child = spawn(command, args, options);
let stdout = "";
let stderr = "";
child.stdout.on("data", (data) => {
stdout += data;
});
child.stderr.on("data", (data) => {
stderr += data;
});
child.on("close", (code) => {
if (code === 0) {
resolve({ stdout, stderr });
} else {
const error = new Error(`Process exited with code ${code}`);
Object.assign(error, { stdout, stderr });
reject(error);
}
});
child.on("error", reject);
});
}
// package `tag` need to be installed via `brew install tag`
async function getTags(path: string): Promise<string[]> {
path = expandTilde(path);
const { stdout } = await spawnAsync("tag", ["-Nl", path]);
console.log("stdout", stdout);
return stdout.trim().split(",").map((tag) => tag.trim()).filter(Boolean);
}
async function setTags(path: string, tags: string[]) {
path = expandTilde(path);
if (!Path.isAbsolute(path)) {
path = Path.resolve(path);
}
const { stdout, stderr } = await spawnAsync("tag", ["-s", tags.join(","), path]);
return { stdout, stderr };
}
const tags = await getTags("~/wtmp/test");
console.log(tags);
const result = await setTags("/Users/shawn/Nutstore/Assist/2025-03-17-", ['blue']);
console.log([].join(","));
console.log(result);