-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcli.ts
More file actions
executable file
·87 lines (76 loc) · 2.35 KB
/
Copy pathcli.ts
File metadata and controls
executable file
·87 lines (76 loc) · 2.35 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
#!/usr/bin/env bun
import { NodeContext, NodeRuntime } from "@effect/platform-node"
import { Effect } from "effect"
import process from "node:process"
import { cli } from "@/cli/app"
import { getExitCode } from "@/cli/errors"
import { renderCustomHelp } from "@/cli/help"
import { version } from "@/cli/version"
let args = process.argv.slice(2)
let help = renderCustomHelp(args, version)
if (help) {
process.stdout.write(`${help}\n`)
process.exit(0)
}
let { strippedArgs, envVars } = extractGlobalFlags(args)
for (let [key, value] of Object.entries(envVars)) {
if (!process.env[key]) process.env[key] = value
}
cli([process.argv[0], process.argv[1], ...strippedArgs]).pipe(
Effect.provide(NodeContext.layer),
Effect.catchAll(error =>
Effect.sync(() => {
process.exit(getExitCode(error))
}),
),
NodeRuntime.runMain,
)
type GlobalFlagResult = {
strippedArgs: string[]
envVars: Record<string, string>
}
function extractGlobalFlags(args: string[]): GlobalFlagResult {
let envVars: Record<string, string> = {}
let strippedArgs: string[] = []
let index = 0
while (index < args.length) {
let arg = args[index]
let nextArg = args[index + 1]
if (arg === "--server" && nextArg && !nextArg.startsWith("-")) {
envVars.ALKALYE_SERVER = nextArg
index += 2
} else if (arg.startsWith("--server=")) {
envVars.ALKALYE_SERVER = arg.slice("--server=".length)
index += 1
} else if (arg === "--sync-peer" && nextArg && !nextArg.startsWith("-")) {
envVars.ALKALYE_SYNC_PEER = nextArg
index += 2
} else if (arg.startsWith("--sync-peer=")) {
envVars.ALKALYE_SYNC_PEER = arg.slice("--sync-peer=".length)
index += 1
} else if (arg === "--home" && nextArg && !nextArg.startsWith("-")) {
envVars.ALKALYE_CLI_HOME = nextArg
index += 2
} else if (arg.startsWith("--home=")) {
envVars.ALKALYE_CLI_HOME = arg.slice("--home=".length)
index += 1
} else if (arg === "--timeout" && nextArg && !nextArg.startsWith("-")) {
index += 2
} else if (arg.startsWith("--timeout=")) {
index += 1
} else if (arg === "--json" || arg === "-j") {
strippedArgs.push(arg)
index += 1
} else if (arg === "--verbose" || arg === "-v") {
strippedArgs.push(arg)
index += 1
} else if (arg === "--quiet" || arg === "-q") {
strippedArgs.push(arg)
index += 1
} else {
strippedArgs.push(arg)
index += 1
}
}
return { strippedArgs, envVars }
}