-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.ts
127 lines (109 loc) · 3.27 KB
/
args.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
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
127
import { CLIFlags } from "../../common/cliFlags";
import { LauncherMode } from "../../common/modes";
import { readConfig } from "./config";
import Logger from "./logger";
import { Option, program } from "commander";
export class ArgsProvider {
static stdinArgs: string[] = [];
static flags: CLIFlags = {};
static isReadingStdinDone = false;
static isReadingFlagsDone = false;
private constructor() {}
}
export const readCLIFlags = () => {
Logger.debug("Reading CLI flags...");
const startTime = process.hrtime();
if (!ArgsProvider.isReadingFlagsDone) {
program
.name("based-launcher")
.version("1.0.5")
.addOption(
new Option("-t, --theme <path>", "Path to the theme file (CSS)")
)
.addOption(
new Option("-m, --mode <mode>", "Mode to run the launcher in")
.default("dmenu")
.choices(Object.values(LauncherMode))
)
.addOption(
new Option(
"--input-prefix <prefix>",
"Prefix label for the input field"
)
)
.addOption(
new Option(
"--input-format <format>",
"Format of the input data (MIME type, several are supported)"
)
.default("text/plain")
.choices(["text/plain", "application/json"])
)
.addOption(
new Option(
"--item-size <size>",
"Height of each item in the list (in pixels), needed for theming"
)
.preset(28)
.argParser(parseInt)
)
.addOption(
new Option(
"--item-size-with-description <size>",
"Height of each item with description in the list (in pixels), needed for theming"
)
.preset(40)
.argParser(parseInt)
);
// @ts-ignore
const resolvedArgv = import.meta.env.PROD
? process.argv
: process.argv.slice(6);
program.allowUnknownOption().parse(resolvedArgv, { from: "user" });
const config = readConfig();
const opts = program.opts();
Logger.debug("CLI flags:", opts);
ArgsProvider.flags = {
...config,
...opts,
};
ArgsProvider.isReadingFlagsDone = true;
}
const endTime = process.hrtime(startTime);
Logger.debug(
`Read CLI flags in ${(endTime[0] * 1000 + endTime[1] / 1000000).toFixed(
0
)}ms`
);
return ArgsProvider.flags;
};
function onlyUnique(value: string, index: number, self: string[]) {
return self.indexOf(value) === index;
}
export const readPipedArgs = async () => {
Logger.debug("Reading piped args...");
const startTime = process.hrtime();
if (!ArgsProvider.isReadingStdinDone && !process.stdin.isTTY) {
ArgsProvider.stdinArgs = await new Promise((resolve) => {
let text = "";
process.stdin.setEncoding("utf8");
process.stdin.on("readable", function () {
var chunk = process.stdin.read();
if (chunk !== null) {
text += chunk;
}
});
process.stdin.on("end", function () {
resolve(text.split("\n").filter(Boolean).filter(onlyUnique));
});
});
ArgsProvider.isReadingStdinDone = true;
}
const endTime = process.hrtime(startTime);
Logger.debug(
`Read piped args in ${(endTime[0] * 1000 + endTime[1] / 1000000).toFixed(
0
)}ms`
);
return ArgsProvider.stdinArgs;
};