-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.ts
executable file
·81 lines (74 loc) · 2.2 KB
/
cli.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
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
#!/usr/bin/env -S deno run --allow-net=localhost,leaves.chiba.dev,leaves--develop.deno.dev
import { Input } from "https://deno.land/x/[email protected]/prompt/input.ts";
import { Select } from "https://deno.land/x/[email protected]/prompt/select.ts";
import {
clearScreen,
colors,
} from "https://deno.land/x/[email protected]/ansi/mod.ts";
const COMMAND_OPTIONS = [{ value: -1, name: ":Back" }, {
value: -2,
name: ":Exit",
}];
const keys = { next: ["j", "down"], previous: ["k", "up"] };
const API_PATH = Deno.args.at(0) === "dev"
? "http://localhost:8000/api/cli"
: "https://leaves.chiba.dev/api/cli";
let list: Array<{ value: number; name: string }> = [];
let searchSkip = false;
let word = "";
while (true) {
console.log(clearScreen);
if (!searchSkip) {
word = await Input.prompt({ message: "Search", suggestions: [word] });
if (!word) {
Deno.exit();
}
const url = new URL(`${API_PATH}/search`);
url.searchParams.set("q", word);
const res = await fetch(url);
list = await res.json() as Array<{ name: string; value: number }>;
if (list.length === 0) {
console.log("Not Found");
Deno.exit();
}
if (list.length >= 10) {
console.log(
colors.bold.yellow(
"More than 10 posts hits were found. Let's add search words!",
),
);
}
}
const postId: any = await Select.prompt({ // 型推論がおかしい
message: "Select",
maxRows: 12,
options: list.concat(COMMAND_OPTIONS),
keys,
});
if (postId === -1) {
searchSkip = false;
continue;
} else if (postId === -2) {
Deno.exit(0);
}
const res = await fetch(`${API_PATH}/posts/${postId}`);
const json = await res.json();
console.log(
"---------------------------------------------------------------------------------",
);
console.log(json.source);
console.log(
"---------------------------------------------------------------------------------",
);
console.log(`https://leaves.chiba.dev/posts/${postId}`);
const option: any = await Select.prompt({
message: "Select",
options: COMMAND_OPTIONS,
keys,
});
if (option === -2) {
Deno.exit(0);
} else if (option === -1) {
searchSkip = true;
}
}