@sixb/cli 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -18
- package/dist/commands/api.d.ts.map +1 -1
- package/dist/commands/app.d.ts.map +1 -1
- package/dist/commands/dev-runner.d.ts +14 -0
- package/dist/commands/dev-runner.d.ts.map +1 -0
- package/dist/commands/dev.d.ts +2 -12
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/init.d.ts +0 -1
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/login.d.ts +8 -0
- package/dist/commands/login.d.ts.map +1 -0
- package/dist/commands/logout.d.ts +6 -0
- package/dist/commands/logout.d.ts.map +1 -0
- package/dist/commands/profile.d.ts +7 -0
- package/dist/commands/profile.d.ts.map +1 -0
- package/dist/commands/service-account.d.ts +1 -0
- package/dist/commands/service-account.d.ts.map +1 -1
- package/dist/commands/status.d.ts +8 -0
- package/dist/commands/status.d.ts.map +1 -0
- package/dist/commands/token.d.ts +1 -0
- package/dist/commands/token.d.ts.map +1 -1
- package/dist/lib/api-client.d.ts +0 -11
- package/dist/lib/api-client.d.ts.map +1 -1
- package/dist/lib/command-line.d.ts +176 -0
- package/dist/lib/command-line.d.ts.map +1 -0
- package/dist/lib/dev-supervisor.d.ts +4 -0
- package/dist/lib/dev-supervisor.d.ts.map +1 -0
- package/dist/lib/dev-watch.d.ts +7 -0
- package/dist/lib/dev-watch.d.ts.map +1 -0
- package/dist/lib/errors.d.ts +6 -0
- package/dist/lib/errors.d.ts.map +1 -1
- package/dist/lib/instance-command.d.ts +3 -0
- package/dist/lib/instance-command.d.ts.map +1 -0
- package/dist/lib/loadSixb.d.ts.map +1 -1
- package/dist/lib/production.d.ts +1 -0
- package/dist/lib/production.d.ts.map +1 -1
- package/dist/lib/profile-api-client.d.ts +6 -0
- package/dist/lib/profile-api-client.d.ts.map +1 -0
- package/dist/lib/profiles.d.ts +34 -0
- package/dist/lib/profiles.d.ts.map +1 -0
- package/dist/lib/runtime.d.ts.map +1 -1
- package/dist/lib/worker-registry.d.ts.map +1 -1
- package/dist/ui/index.d.ts +5 -5
- package/dist/ui/index.d.ts.map +1 -1
- package/package.json +16 -15
- package/src/commands/api.tsx +3 -5
- package/src/commands/app.tsx +2 -8
- package/src/commands/dev-runner.tsx +181 -0
- package/src/commands/dev.tsx +27 -157
- package/src/commands/init.tsx +1 -13
- package/src/commands/login.tsx +196 -0
- package/src/commands/logout.tsx +36 -0
- package/src/commands/profile.tsx +101 -0
- package/src/commands/service-account.tsx +18 -15
- package/src/commands/status.tsx +93 -0
- package/src/commands/token.tsx +11 -8
- package/src/index.tsx +134 -156
- package/src/lib/api-client.ts +4 -65
- package/src/lib/command-line.ts +743 -0
- package/src/lib/dev-supervisor.ts +170 -0
- package/src/lib/dev-watch.ts +200 -0
- package/src/lib/errors.ts +13 -0
- package/src/lib/instance-command.ts +80 -0
- package/src/lib/loadSixb.ts +0 -2
- package/src/lib/production.ts +11 -0
- package/src/lib/profile-api-client.ts +38 -0
- package/src/lib/profiles.ts +262 -0
- package/src/lib/runtime.ts +10 -4
- package/src/lib/worker-registry.ts +7 -1
- package/src/ui/index.tsx +39 -140
- package/dist/commands/auth.d.ts +0 -8
- package/dist/commands/auth.d.ts.map +0 -1
- package/src/commands/auth.tsx +0 -96
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { spawn } from "node:child_process"
|
|
2
|
+
import { dirname, resolve } from "node:path"
|
|
3
|
+
import type { DevOptions } from "../commands/dev-runner"
|
|
4
|
+
import { watchDevSource } from "./dev-watch"
|
|
5
|
+
|
|
6
|
+
/** The parent never imports project modules: every generation gets a fresh module graph. */
|
|
7
|
+
export async function runDevSupervisor(options: DevOptions): Promise<void> {
|
|
8
|
+
const root = dirname(resolve(options.entry ?? "sixb.config.ts"))
|
|
9
|
+
let child: { process: ReturnType<typeof spawn>; exited: Promise<number | null> } | null = null
|
|
10
|
+
let stopping = false
|
|
11
|
+
let restarting = false
|
|
12
|
+
let pending = false
|
|
13
|
+
let hasCustomApp = false
|
|
14
|
+
let started = false
|
|
15
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
16
|
+
let startupTimer: ReturnType<typeof setTimeout> | undefined
|
|
17
|
+
let complete!: () => void
|
|
18
|
+
const finished = new Promise<void>((resolve) => {
|
|
19
|
+
complete = resolve
|
|
20
|
+
})
|
|
21
|
+
function terminate(current: NonNullable<typeof child>, signal: NodeJS.Signals) {
|
|
22
|
+
try {
|
|
23
|
+
if (process.platform !== "win32" && current.process.pid) {
|
|
24
|
+
process.kill(-current.process.pid, signal)
|
|
25
|
+
} else {
|
|
26
|
+
current.process.kill(signal)
|
|
27
|
+
}
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (!error || typeof error !== "object" || !("code" in error) || error.code !== "ESRCH") {
|
|
30
|
+
throw error
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function stopChild() {
|
|
36
|
+
clearTimeout(startupTimer)
|
|
37
|
+
const current = child
|
|
38
|
+
if (!current) return
|
|
39
|
+
// Give the runtime ownership of graceful shutdown. Kill the isolated process group
|
|
40
|
+
// only on the deadline (or after exit) to reap any compiler/sandbox descendants too.
|
|
41
|
+
current.process.kill("SIGTERM")
|
|
42
|
+
const deadline = setTimeout(() => {
|
|
43
|
+
console.error("[SixbDev] Shutdown timed out; terminating the dev process.")
|
|
44
|
+
terminate(current, "SIGKILL")
|
|
45
|
+
}, 10_000)
|
|
46
|
+
try {
|
|
47
|
+
await current.exited
|
|
48
|
+
} finally {
|
|
49
|
+
clearTimeout(deadline)
|
|
50
|
+
if (child === current) child = null
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function restart() {
|
|
55
|
+
if (stopping || restarting) return
|
|
56
|
+
restarting = true
|
|
57
|
+
try {
|
|
58
|
+
while (pending && !stopping) {
|
|
59
|
+
await stopChild()
|
|
60
|
+
if (stopping) break
|
|
61
|
+
hasCustomApp = false
|
|
62
|
+
if (started) await watcher.refresh()
|
|
63
|
+
else await watcher.ready
|
|
64
|
+
if (stopping) break
|
|
65
|
+
started = true
|
|
66
|
+
// All saves before this spawn are already in its fresh module graph.
|
|
67
|
+
pending = false
|
|
68
|
+
clearTimeout(timer)
|
|
69
|
+
const proc = spawn(process.execPath, [...process.execArgv, ...process.argv.slice(1)], {
|
|
70
|
+
env: {
|
|
71
|
+
...process.env,
|
|
72
|
+
SIXB_DEV_CHILD: "1",
|
|
73
|
+
SIXB_DEV_GENERATION: crypto.randomUUID(),
|
|
74
|
+
SIXB_DEV_READY: "",
|
|
75
|
+
},
|
|
76
|
+
detached: process.platform !== "win32",
|
|
77
|
+
stdio: ["ignore", "inherit", "inherit", "ipc"],
|
|
78
|
+
})
|
|
79
|
+
const current = {
|
|
80
|
+
process: proc,
|
|
81
|
+
exited: new Promise<number | null>((resolve, reject) => {
|
|
82
|
+
proc.once("exit", resolve)
|
|
83
|
+
proc.once("error", reject)
|
|
84
|
+
}),
|
|
85
|
+
}
|
|
86
|
+
proc.on("message", (message: unknown) => {
|
|
87
|
+
if (child !== current || !message || typeof message !== "object") return
|
|
88
|
+
if ("type" in message && message.type === "ready") {
|
|
89
|
+
clearTimeout(startupTimer)
|
|
90
|
+
hasCustomApp = "hasCustomApp" in message && message.hasCustomApp === true
|
|
91
|
+
console.log(`[SixbDev] Ready (pid ${proc.pid}). Watching ${root}`)
|
|
92
|
+
} else if ("type" in message && message.type === "startup-error") {
|
|
93
|
+
hasCustomApp = false
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
child = current
|
|
97
|
+
startupTimer = setTimeout(() => {
|
|
98
|
+
if (child === current) {
|
|
99
|
+
console.error("[SixbDev] Startup timed out. Edit a source file to retry.")
|
|
100
|
+
terminate(current, "SIGKILL")
|
|
101
|
+
}
|
|
102
|
+
}, 60_000)
|
|
103
|
+
void current.exited
|
|
104
|
+
.then((code) => {
|
|
105
|
+
terminate(current, "SIGKILL")
|
|
106
|
+
if (child !== current) return
|
|
107
|
+
clearTimeout(startupTimer)
|
|
108
|
+
child = null
|
|
109
|
+
hasCustomApp = false
|
|
110
|
+
if (!stopping && !restarting) {
|
|
111
|
+
console.error(`[SixbDev] Dev process exited (${code}). Waiting for source changes.`)
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
.catch(fail)
|
|
115
|
+
}
|
|
116
|
+
} finally {
|
|
117
|
+
restarting = false
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const watcher = watchDevSource(
|
|
122
|
+
root,
|
|
123
|
+
() => hasCustomApp,
|
|
124
|
+
(filename) => {
|
|
125
|
+
if (stopping) return
|
|
126
|
+
pending = true
|
|
127
|
+
clearTimeout(timer)
|
|
128
|
+
timer = setTimeout(() => {
|
|
129
|
+
console.log(`[SixbDev] Source changed${filename ? `: ${filename}` : ""}. Restarting…`)
|
|
130
|
+
void restart().catch(fail)
|
|
131
|
+
}, 150)
|
|
132
|
+
},
|
|
133
|
+
fail
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
async function shutdown() {
|
|
137
|
+
if (stopping) return
|
|
138
|
+
stopping = true
|
|
139
|
+
clearTimeout(timer)
|
|
140
|
+
try {
|
|
141
|
+
await watcher.close()
|
|
142
|
+
await stopChild()
|
|
143
|
+
} finally {
|
|
144
|
+
complete()
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function fail(error: unknown) {
|
|
149
|
+
console.error("[SixbDev]", error)
|
|
150
|
+
process.exitCode = 1
|
|
151
|
+
void shutdown().catch((shutdownError) =>
|
|
152
|
+
console.error("[SixbDev] Shutdown failed:", shutdownError)
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const requestShutdown = () => {
|
|
157
|
+
void shutdown().catch(fail)
|
|
158
|
+
}
|
|
159
|
+
process.on("SIGINT", requestShutdown)
|
|
160
|
+
process.on("SIGTERM", requestShutdown)
|
|
161
|
+
try {
|
|
162
|
+
pending = true
|
|
163
|
+
await restart()
|
|
164
|
+
await finished
|
|
165
|
+
} finally {
|
|
166
|
+
process.off("SIGINT", requestShutdown)
|
|
167
|
+
process.off("SIGTERM", requestShutdown)
|
|
168
|
+
await shutdown()
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { type Dirent, watch } from "node:fs"
|
|
2
|
+
import { lstat, readdir } from "node:fs/promises"
|
|
3
|
+
import { join, resolve, sep } from "node:path"
|
|
4
|
+
|
|
5
|
+
const ignoredDirectories = new Set([
|
|
6
|
+
".sixb",
|
|
7
|
+
".git",
|
|
8
|
+
"node_modules",
|
|
9
|
+
"dist",
|
|
10
|
+
"build",
|
|
11
|
+
"coverage",
|
|
12
|
+
".next",
|
|
13
|
+
".local",
|
|
14
|
+
])
|
|
15
|
+
const isAppSource = (path: string) => path.startsWith(`app${sep}`)
|
|
16
|
+
const isSource = (path: string) =>
|
|
17
|
+
/\.(?:[cm]?[jt]sx?|json|ya?ml|toml|css)$/.test(path) || path === "bun.lock"
|
|
18
|
+
const ignored = (path: string) => path.split(sep).some((part) => ignoredDirectories.has(part))
|
|
19
|
+
|
|
20
|
+
function missing(error: unknown): boolean {
|
|
21
|
+
return (
|
|
22
|
+
!!error &&
|
|
23
|
+
typeof error === "object" &&
|
|
24
|
+
"code" in error &&
|
|
25
|
+
(error.code === "ENOENT" || error.code === "ENOTDIR")
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function statPath(path: string) {
|
|
30
|
+
try {
|
|
31
|
+
return await lstat(path)
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (missing(error)) return undefined
|
|
34
|
+
throw error
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Targeted native checks, with a bounded-concurrency async scan for missed events. */
|
|
39
|
+
export function watchDevSource(
|
|
40
|
+
root: string,
|
|
41
|
+
hasCustomApp: () => boolean,
|
|
42
|
+
onChange: (path: string) => void,
|
|
43
|
+
onError: (error: unknown) => void
|
|
44
|
+
) {
|
|
45
|
+
const previous = new Map<string, string>()
|
|
46
|
+
const dirty = new Set<string>()
|
|
47
|
+
let closed = false
|
|
48
|
+
let fullCheck = false
|
|
49
|
+
let nativeQueued = false
|
|
50
|
+
let debounce: ReturnType<typeof setTimeout> | undefined
|
|
51
|
+
let fallback: ReturnType<typeof setTimeout> | undefined
|
|
52
|
+
let chain: Promise<void> = Promise.resolve()
|
|
53
|
+
|
|
54
|
+
async function snapshot(directory: string, skipApp: boolean): Promise<Map<string, string>> {
|
|
55
|
+
const files = new Map<string, string>()
|
|
56
|
+
async function scan(path: string) {
|
|
57
|
+
if (closed || ignored(path) || (skipApp && isAppSource(path))) return
|
|
58
|
+
// Dirents avoid stat'ing non-source files or following directory symlinks.
|
|
59
|
+
let children: Dirent[]
|
|
60
|
+
try {
|
|
61
|
+
children = await readdir(resolve(root, path), { withFileTypes: true })
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (missing(error)) return
|
|
64
|
+
throw error
|
|
65
|
+
}
|
|
66
|
+
if (path === "app") {
|
|
67
|
+
const stat = await statPath(resolve(root, path))
|
|
68
|
+
if (stat && !stat.isSymbolicLink()) files.set(path, String(stat.ino))
|
|
69
|
+
if (skipApp) return
|
|
70
|
+
}
|
|
71
|
+
for (let offset = 0; offset < children.length && !closed; offset += 32) {
|
|
72
|
+
const batch = children.slice(offset, offset + 32)
|
|
73
|
+
await Promise.all(
|
|
74
|
+
batch
|
|
75
|
+
.filter((entry) => entry.isFile() && isSource(join(path, entry.name)))
|
|
76
|
+
.map(async (entry) => {
|
|
77
|
+
const file = join(path, entry.name)
|
|
78
|
+
if (ignored(file)) return
|
|
79
|
+
const stat = await statPath(resolve(root, file))
|
|
80
|
+
if (stat?.isFile()) files.set(file, fingerprint(stat))
|
|
81
|
+
})
|
|
82
|
+
)
|
|
83
|
+
// Recurse serially: nested directories cannot multiply the IO concurrency.
|
|
84
|
+
for (const entry of batch) if (entry.isDirectory()) await scan(join(path, entry.name))
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
await scan(directory)
|
|
88
|
+
return files
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function fingerprint(stat: NonNullable<Awaited<ReturnType<typeof statPath>>>): string {
|
|
92
|
+
return `${stat.ino}:${stat.size}:${stat.mtimeMs}:${stat.ctimeMs}`
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function apply(next: Map<string, string>, directory: string, skipApp: boolean, notify: boolean) {
|
|
96
|
+
if (closed) return
|
|
97
|
+
let changed: string | undefined
|
|
98
|
+
const markChanged = (path: string) => {
|
|
99
|
+
if (!(hasCustomApp() && isAppSource(path))) changed ??= path
|
|
100
|
+
}
|
|
101
|
+
const inScope = (path: string) =>
|
|
102
|
+
(!directory || path === directory || path.startsWith(`${directory}${sep}`)) &&
|
|
103
|
+
!(skipApp && isAppSource(path))
|
|
104
|
+
for (const [path, value] of next) {
|
|
105
|
+
if (previous.get(path) !== value) markChanged(path)
|
|
106
|
+
previous.set(path, value)
|
|
107
|
+
}
|
|
108
|
+
for (const path of previous.keys()) {
|
|
109
|
+
if (inScope(path) && !next.has(path)) {
|
|
110
|
+
markChanged(path)
|
|
111
|
+
previous.delete(path)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (notify && changed) onChange(changed)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function reconcile(notify = true) {
|
|
118
|
+
const skipApp = hasCustomApp()
|
|
119
|
+
apply(await snapshot("", skipApp), "", skipApp, notify)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function checkPath(path: string) {
|
|
123
|
+
if (closed || ignored(path) || (hasCustomApp() && isAppSource(path))) return
|
|
124
|
+
const stat = await statPath(resolve(root, path))
|
|
125
|
+
if (stat?.isDirectory()) {
|
|
126
|
+
const skipApp = hasCustomApp()
|
|
127
|
+
apply(await snapshot(path, skipApp), path, skipApp, true)
|
|
128
|
+
} else if (stat?.isFile() && isSource(path)) {
|
|
129
|
+
const value = fingerprint(stat)
|
|
130
|
+
if (!closed && previous.get(path) !== value) {
|
|
131
|
+
previous.set(path, value)
|
|
132
|
+
if (!(hasCustomApp() && isAppSource(path))) onChange(path)
|
|
133
|
+
}
|
|
134
|
+
} else if (!stat || stat.isSymbolicLink()) {
|
|
135
|
+
apply(new Map(), path, hasCustomApp(), true)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function enqueue(work: () => Promise<void>): Promise<void> {
|
|
140
|
+
const result = chain.then(async () => {
|
|
141
|
+
if (!closed) await work()
|
|
142
|
+
})
|
|
143
|
+
chain = result.catch((error) => {
|
|
144
|
+
if (!closed) onError(error)
|
|
145
|
+
})
|
|
146
|
+
return result
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const watcher = watch(root, { recursive: true }, (_event, filename) => {
|
|
150
|
+
if (closed) return
|
|
151
|
+
if (filename) {
|
|
152
|
+
const path = String(filename)
|
|
153
|
+
if (ignored(path) || (hasCustomApp() && isAppSource(path))) return
|
|
154
|
+
dirty.add(path)
|
|
155
|
+
} else fullCheck = true
|
|
156
|
+
clearTimeout(debounce)
|
|
157
|
+
debounce = setTimeout(() => {
|
|
158
|
+
if (nativeQueued || closed) return
|
|
159
|
+
nativeQueued = true
|
|
160
|
+
void enqueue(async () => {
|
|
161
|
+
nativeQueued = false
|
|
162
|
+
const paths = [...dirty]
|
|
163
|
+
dirty.clear()
|
|
164
|
+
const full = fullCheck
|
|
165
|
+
fullCheck = false
|
|
166
|
+
if (full) await reconcile()
|
|
167
|
+
else for (const path of paths) await checkPath(path)
|
|
168
|
+
}).catch(() => {}) // enqueue reports failures and keeps the queue usable.
|
|
169
|
+
}, 100)
|
|
170
|
+
})
|
|
171
|
+
watcher.on("error", onError)
|
|
172
|
+
|
|
173
|
+
function scheduleFallback() {
|
|
174
|
+
if (closed) return
|
|
175
|
+
fallback = setTimeout(() => {
|
|
176
|
+
// Bun 1.3.14/macOS can omit recursive events. Keep recovery, but never queue
|
|
177
|
+
// overlapping scans or block the event loop with synchronous filesystem IO.
|
|
178
|
+
void enqueue(() => reconcile())
|
|
179
|
+
.catch(() => {})
|
|
180
|
+
.finally(scheduleFallback)
|
|
181
|
+
}, 5000)
|
|
182
|
+
}
|
|
183
|
+
const ready = enqueue(() => reconcile(false))
|
|
184
|
+
scheduleFallback()
|
|
185
|
+
return {
|
|
186
|
+
ready,
|
|
187
|
+
// Rebase before spawning: saves during shutdown are included in the next child,
|
|
188
|
+
// and app fingerprints are refreshed before handing its edits back to HMR.
|
|
189
|
+
refresh: () => enqueue(() => reconcile(false)),
|
|
190
|
+
async close() {
|
|
191
|
+
closed = true
|
|
192
|
+
clearTimeout(debounce)
|
|
193
|
+
clearTimeout(fallback)
|
|
194
|
+
watcher.close()
|
|
195
|
+
dirty.clear()
|
|
196
|
+
await chain
|
|
197
|
+
previous.clear()
|
|
198
|
+
},
|
|
199
|
+
}
|
|
200
|
+
}
|
package/src/lib/errors.ts
CHANGED
|
@@ -6,6 +6,19 @@ export interface SixbCliErrorOptions extends ErrorOptions {
|
|
|
6
6
|
readonly remediation?: string
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
/** An HTTP or transport failure returned by the Sixb API. */
|
|
10
|
+
export class SixbApiError extends Error {
|
|
11
|
+
readonly status: number | undefined
|
|
12
|
+
readonly responseBody: unknown
|
|
13
|
+
|
|
14
|
+
constructor(message: string, status: number | undefined, responseBody: unknown) {
|
|
15
|
+
super(message)
|
|
16
|
+
this.name = "SixbApiError"
|
|
17
|
+
this.status = status
|
|
18
|
+
this.responseBody = responseBody
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
9
22
|
/** A failure the CLI raised itself, as opposed to one it caught from the runtime. */
|
|
10
23
|
export class SixbCliError extends Error {
|
|
11
24
|
readonly remediation: string | undefined
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { fail, isInstanceCommand, reportError, requestsHelp, runInstanceCli } from "@sixb/cli-core"
|
|
2
|
+
import { resolveProfile } from "./profiles"
|
|
3
|
+
|
|
4
|
+
const CONNECTION_FLAGS = new Set(["api-url", "profile", "token"])
|
|
5
|
+
|
|
6
|
+
export function isLocalInstanceCommand(command: string): boolean {
|
|
7
|
+
return isInstanceCommand(command)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function runLocalInstanceCommand(args: readonly string[]): Promise<void> {
|
|
11
|
+
try {
|
|
12
|
+
const { commandArgs, apiUrl, profile, token } = extractConnectionOptions(args)
|
|
13
|
+
if (requestsHelp(commandArgs)) {
|
|
14
|
+
await runInstanceCli({ args: commandArgs, mode: { kind: "local", baseUrl: "" } })
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
const resolved = await resolveProfile({ apiUrl, profile, token })
|
|
18
|
+
await runInstanceCli({
|
|
19
|
+
args: commandArgs,
|
|
20
|
+
mode: {
|
|
21
|
+
kind: "local",
|
|
22
|
+
baseUrl: resolved.apiUrl,
|
|
23
|
+
...(resolved.token ? { token: resolved.token } : {}),
|
|
24
|
+
...(resolved.profile ? { profile: resolved.profile } : {}),
|
|
25
|
+
},
|
|
26
|
+
})
|
|
27
|
+
} catch (error) {
|
|
28
|
+
process.exitCode = reportError(error)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function extractConnectionOptions(args: readonly string[]): {
|
|
33
|
+
readonly commandArgs: readonly string[]
|
|
34
|
+
readonly apiUrl?: string
|
|
35
|
+
readonly profile?: string
|
|
36
|
+
readonly token?: string
|
|
37
|
+
} {
|
|
38
|
+
const commandArgs: string[] = []
|
|
39
|
+
const values: Record<string, string | undefined> = {}
|
|
40
|
+
|
|
41
|
+
for (let index = 0; index < args.length; index++) {
|
|
42
|
+
const argument = args[index]
|
|
43
|
+
if (argument === "--") {
|
|
44
|
+
commandArgs.push(...args.slice(index))
|
|
45
|
+
break
|
|
46
|
+
}
|
|
47
|
+
if (!argument?.startsWith("--")) {
|
|
48
|
+
if (argument !== undefined) commandArgs.push(argument)
|
|
49
|
+
continue
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const equalsIndex = argument.indexOf("=")
|
|
53
|
+
const name = argument.slice(2, equalsIndex < 0 ? undefined : equalsIndex)
|
|
54
|
+
if (!CONNECTION_FLAGS.has(name)) {
|
|
55
|
+
commandArgs.push(argument)
|
|
56
|
+
continue
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (values[name] !== undefined) {
|
|
60
|
+
fail(`--${name} may only be provided once.`)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const value = equalsIndex < 0 ? args[++index] : argument.slice(equalsIndex + 1)
|
|
64
|
+
if (!value || value.startsWith("--")) {
|
|
65
|
+
fail(`--${name} requires a value.`)
|
|
66
|
+
}
|
|
67
|
+
values[name] = value
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (values["api-url"] && values.profile) {
|
|
71
|
+
fail("--api-url and --profile cannot be used together.")
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
commandArgs,
|
|
76
|
+
...(values["api-url"] ? { apiUrl: values["api-url"] } : {}),
|
|
77
|
+
...(values.profile ? { profile: values.profile } : {}),
|
|
78
|
+
...(values.token ? { token: values.token } : {}),
|
|
79
|
+
}
|
|
80
|
+
}
|
package/src/lib/loadSixb.ts
CHANGED
|
@@ -78,8 +78,6 @@ function hasDefinitionCatalogs(value: Record<PropertyKey, unknown>): boolean {
|
|
|
78
78
|
]) &&
|
|
79
79
|
isRecord(definitions.workflows) &&
|
|
80
80
|
hasMethods(definitions.workflows, ["list", "getById"]) &&
|
|
81
|
-
isRecord(definitions.agents) &&
|
|
82
|
-
hasMethods(definitions.agents, ["list", "getById"]) &&
|
|
83
81
|
isRecord(definitions.security) &&
|
|
84
82
|
hasMethods(definitions.security, [
|
|
85
83
|
"listGroups",
|
package/src/lib/production.ts
CHANGED
|
@@ -83,6 +83,17 @@ export function builtAppOutdir(buildOutdir: string): string {
|
|
|
83
83
|
return resolve(buildOutdir, "app")
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
export async function hasBuiltCustomApp(appOutdir: string): Promise<boolean> {
|
|
87
|
+
const shells = await Promise.all(
|
|
88
|
+
["index.html", "shared-index.html"].map((file) =>
|
|
89
|
+
stat(resolve(appOutdir, file))
|
|
90
|
+
.then((info) => info.isFile())
|
|
91
|
+
.catch(() => false)
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
return shells.every(Boolean)
|
|
95
|
+
}
|
|
96
|
+
|
|
86
97
|
export function builtAtlasOutdir(buildOutdir: string): string {
|
|
87
98
|
return resolve(buildOutdir, "atlas")
|
|
88
99
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { SixbApiClientConfig } from "./api-client"
|
|
2
|
+
import { type ResolveProfileInput, resolveProfile } from "./profiles"
|
|
3
|
+
|
|
4
|
+
export interface ResolveProfileApiClientConfigOptions extends ResolveProfileInput {}
|
|
5
|
+
|
|
6
|
+
export async function resolveProfileApiClientConfig(
|
|
7
|
+
options: ResolveProfileApiClientConfigOptions = {}
|
|
8
|
+
): Promise<SixbApiClientConfig> {
|
|
9
|
+
const resolved = await resolveProfile(options)
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
apiUrl: resolved.apiUrl,
|
|
13
|
+
apiUrlSource: formatApiUrlSource(resolved),
|
|
14
|
+
...(resolved.token
|
|
15
|
+
? {
|
|
16
|
+
token: resolved.token,
|
|
17
|
+
tokenSource: resolved.tokenSource ?? "profile",
|
|
18
|
+
}
|
|
19
|
+
: {}),
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function formatApiUrlSource(resolved: Awaited<ReturnType<typeof resolveProfile>>): string {
|
|
24
|
+
switch (resolved.source) {
|
|
25
|
+
case "api-url-flag":
|
|
26
|
+
return "--api-url"
|
|
27
|
+
case "profile-flag":
|
|
28
|
+
return `profile:${resolved.profile}`
|
|
29
|
+
case "environment":
|
|
30
|
+
return "SIXB_API_URL"
|
|
31
|
+
case "profile-environment":
|
|
32
|
+
return `SIXB_PROFILE:${resolved.profile}`
|
|
33
|
+
case "current-profile":
|
|
34
|
+
return `profile:${resolved.profile}`
|
|
35
|
+
case "default":
|
|
36
|
+
return "default"
|
|
37
|
+
}
|
|
38
|
+
}
|