-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspeechflow-main-cli.ts
More file actions
260 lines (245 loc) · 9.3 KB
/
speechflow-main-cli.ts
File metadata and controls
260 lines (245 loc) · 9.3 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
** SpeechFlow - Speech Processing Flow Graph
** Copyright (c) 2024-2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
*/
/* standard dependencies */
import path from "node:path"
/* external dependencies */
import CLIio from "cli-io"
import yargs from "yargs"
import { hideBin } from "yargs/helpers"
import jsYAML from "js-yaml"
import dotenvx from "@dotenvx/dotenvx"
import syspath from "syspath"
import chalk from "chalk"
/* internal dependencies */
import * as util from "./speechflow-util"
import pkg from "../../package.json"
/* command-line options */
export interface CLIOptions {
V: boolean
S: boolean
v: string
a: string
p: number
C: string
d: string
o: string
e: string
f: string
c: string
_: (string | number)[]
}
/* CLI context class */
export class CLIContext {
public cli: CLIio | null = null
public args: CLIOptions | null = null
public config: string | null = null
public debug = false
/* type guard for initialization */
isInitialized (): this is CLIContext & { cli: CLIio; args: CLIOptions; config: string } {
return this.cli !== null && this.args !== null && this.config !== null
}
/* initialization of CLI */
async init (): Promise<void> {
/* determine system paths */
const { dataDir } = syspath({
appName: "speechflow",
dataDirAutoCreate: true
})
/* parse command-line arguments */
const coerce = (arg: string) => Array.isArray(arg) ? arg[arg.length - 1] : arg
this.args = await yargs()
/* eslint @stylistic/indent: off */
.usage(
"Usage: $0 " +
"[-h|--help] " +
"[-V|--version] " +
"[-S|--status] " +
"[-v|--verbose <level>] " +
"[-a|--address <ip-address>] " +
"[-p|--port <tcp-port>] " +
"[-C|--cache <directory>] " +
"[-d|--dashboard <type>:<id>:<name>[,...]] " +
"[-o|--osc <ip-address>:<udp-port>] " +
"[-e|--expression <expression>] " +
"[-f|--file <file>] " +
"[-c|--config <id>@<yaml-config-file>] " +
"[<argument> [...]]"
)
.version(false)
.option("V", {
alias: "version",
type: "boolean",
array: false,
coerce,
default: false,
describe: "show program version information"
})
.option("S", {
alias: "status",
type: "boolean",
array: false,
coerce,
default: false,
describe: "show one-time status of nodes"
})
.option("v", {
alias: "log-level",
type: "string",
array: false,
coerce,
nargs: 1,
default: "warning",
describe: "level for verbose logging ('none', 'error', 'warning', 'info', 'debug')"
})
.option("a", {
alias: "address",
type: "string",
array: false,
coerce,
nargs: 1,
default: "0.0.0.0",
describe: "IP address for REST/WebSocket API"
})
.option("p", {
alias: "port",
type: "number",
array: false,
coerce,
nargs: 1,
default: 8484,
describe: "TCP port for REST/WebSocket API"
})
.option("C", {
alias: "cache",
type: "string",
array: false,
coerce,
nargs: 1,
default: path.join(dataDir, "cache"),
describe: "directory for cached files (primarily AI model files)"
})
.option("d", {
alias: "dashboard",
type: "string",
array: false,
coerce,
nargs: 1,
default: "",
describe: "list of dashboard block types and names"
})
.option("o", {
alias: "osc",
type: "string",
array: false,
coerce,
nargs: 1,
default: "",
describe: "OSC/UDP endpoint to send dashboard information"
})
.option("e", {
alias: "expression",
type: "string",
array: false,
coerce,
nargs: 1,
default: "",
describe: "FlowLink expression string"
})
.option("f", {
alias: "file",
type: "string",
array: false,
coerce,
nargs: 1,
default: "",
describe: "FlowLink expression file"
})
.option("c", {
alias: "config",
type: "string",
array: false,
coerce,
nargs: 1,
default: "",
describe: "FlowLink expression reference into YAML file (in format <id>@<file>)"
})
.help("h", "show usage help")
.alias("h", "help")
.showHelpOnFail(true)
.strict()
.demand(0)
.parse(hideBin(process.argv)) as CLIOptions
/* short-circuit version request */
if (this.args.V) {
process.stderr.write(`SpeechFlow ${pkg["x-stdver"]} (${pkg["x-release"]}) <${pkg.homepage}>\n`)
process.stderr.write(`${pkg.description}\n`)
process.stderr.write(`Copyright (c) 2024-2026 ${pkg.author.name} <${pkg.author.url}>\n`)
process.stderr.write(`Licensed under ${pkg.license} <http://spdx.org/licenses/${pkg.license}.html>\n`)
process.exit(0)
}
/* establish CLI environment */
this.cli = new CLIio({
encoding: "utf8",
logLevel: this.args.v,
logTime: true,
logPrefix: pkg.name
})
if (this.args.v.match(/^(?:info|debug)$/))
this.debug = true
/* provide startup information */
this.cli.log("info", `starting SpeechFlow ${pkg["x-stdver"]} (${pkg["x-release"]})`)
/* load .env files */
const result = dotenvx.config({
encoding: "utf8",
ignore: [ "MISSING_ENV_FILE" ],
quiet: true
})
if (result?.parsed !== undefined)
for (const key of Object.keys(result.parsed))
this.cli.log("info", `loaded environment variable "${key}" from ".env" files`)
/* sanity check configuration situation */
let n = 0
if (typeof this.args.e === "string" && this.args.e !== "") n++
if (typeof this.args.f === "string" && this.args.f !== "") n++
if (typeof this.args.c === "string" && this.args.c !== "") n++
if (n === 0)
throw new Error("need at least one FlowLink specification source (use one of the options -e, -f or -c)")
else if (n !== 1)
throw new Error("cannot use more than one FlowLink specification source (use only one of the options -e, -f or -c)")
/* read configuration */
if (typeof this.args.e === "string" && this.args.e !== "")
this.config = this.args.e
else if (typeof this.args.f === "string" && this.args.f !== "")
this.config = await this.cli.input(this.args.f, { encoding: "utf8" })
else if (typeof this.args.c === "string" && this.args.c !== "") {
const m = this.args.c.match(/^(.+?)@(.+)$/)
if (m === null)
throw new Error("invalid configuration file specification (expected \"<id>@<yaml-config-file>\")")
const [ , id, file ] = m
const yaml = await this.cli.input(file, { encoding: "utf8" })
const obj: any = util.run("parsing YAML configuration", () => jsYAML.load(yaml))
if (obj[id] === undefined)
throw new Error(`no such id "${id}" found in configuration file "${file}"`)
this.config = obj[id] as string
}
}
/* utility function for handling a top-level error */
handleTopLevelError (err: Error): never {
if (this.cli !== null) {
if (this.debug)
this.cli.log("error", `${err.message}\n${err.stack}`)
else
this.cli.log("error", `${err.message}`)
}
else {
if (this.debug)
process.stderr.write(`${pkg.name}: ${chalk.red("ERROR")}: ${err.message}\n${err.stack}\n`)
else
process.stderr.write(`${pkg.name}: ${chalk.red("ERROR")}: ${err.message}\n`)
}
process.exit(1)
}
}