@sixb/cli 0.1.26 → 0.2.1
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/dist/commands/agent.d.ts.map +1 -1
- package/dist/commands/api.d.ts.map +1 -1
- package/dist/commands/check.d.ts.map +1 -1
- package/dist/commands/deploy.d.ts.map +1 -1
- package/dist/commands/dev-runner.d.ts.map +1 -1
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/env.d.ts +31 -0
- package/dist/commands/env.d.ts.map +1 -0
- package/dist/lib/command-line.d.ts +26 -1
- package/dist/lib/command-line.d.ts.map +1 -1
- package/dist/lib/deploy-release.d.ts +2 -0
- package/dist/lib/deploy-release.d.ts.map +1 -1
- package/dist/lib/deploy-source.d.ts +2 -0
- package/dist/lib/deploy-source.d.ts.map +1 -1
- package/dist/lib/env-config.d.ts +17 -0
- package/dist/lib/env-config.d.ts.map +1 -0
- package/dist/lib/env-file.d.ts +16 -0
- package/dist/lib/env-file.d.ts.map +1 -0
- package/dist/lib/hidden-prompt.d.ts +19 -0
- package/dist/lib/hidden-prompt.d.ts.map +1 -0
- package/dist/tui/app.d.ts +8 -0
- package/dist/tui/app.d.ts.map +1 -1
- package/dist/tui/computer.d.ts +5 -0
- package/dist/tui/computer.d.ts.map +1 -1
- package/dist/tui/context.d.ts +13 -0
- package/dist/tui/context.d.ts.map +1 -0
- package/dist/tui/format.d.ts +85 -0
- package/dist/tui/format.d.ts.map +1 -1
- package/dist/tui/index.d.ts.map +1 -1
- package/dist/tui/markdown.d.ts +18 -0
- package/dist/tui/markdown.d.ts.map +1 -0
- package/dist/tui/parts.d.ts +8 -0
- package/dist/tui/parts.d.ts.map +1 -1
- package/dist/tui/runner.d.ts +37 -14
- package/dist/tui/runner.d.ts.map +1 -1
- package/dist/tui/settings.d.ts +13 -1
- package/dist/tui/settings.d.ts.map +1 -1
- package/dist/tui/transcript.d.ts +1 -1
- package/dist/tui/transcript.d.ts.map +1 -1
- package/dist/tui/ui.d.ts +7 -0
- package/dist/tui/ui.d.ts.map +1 -1
- package/dist/ui/deploy-screens.d.ts +3 -1
- package/dist/ui/deploy-screens.d.ts.map +1 -1
- package/dist/ui/env-screens.d.ts +37 -0
- package/dist/ui/env-screens.d.ts.map +1 -0
- package/package.json +19 -19
- package/src/commands/agent.ts +17 -10
- package/src/commands/api.ts +2 -1
- package/src/commands/check.ts +5 -1
- package/src/commands/deploy.ts +29 -3
- package/src/commands/dev-runner.ts +3 -1
- package/src/commands/dev.ts +3 -0
- package/src/commands/env.ts +422 -0
- package/src/index.tsx +39 -1
- package/src/lib/command-line.ts +59 -1
- package/src/lib/deploy-release.ts +3 -0
- package/src/lib/deploy-source.ts +10 -0
- package/src/lib/env-config.ts +96 -0
- package/src/lib/env-file.ts +104 -0
- package/src/lib/hidden-prompt.ts +97 -0
- package/src/tui/app.tsx +315 -179
- package/src/tui/computer.ts +37 -0
- package/src/tui/context.tsx +149 -0
- package/src/tui/format.ts +218 -1
- package/src/tui/index.tsx +13 -2
- package/src/tui/markdown.ts +91 -0
- package/src/tui/parts.ts +23 -3
- package/src/tui/runner.ts +113 -30
- package/src/tui/settings.ts +54 -5
- package/src/tui/transcript.tsx +90 -8
- package/src/tui/ui.tsx +40 -11
- package/src/ui/deploy-screens.ts +12 -1
- package/src/ui/env-screens.ts +81 -0
- package/dist/tui/folders.d.ts +0 -7
- package/dist/tui/folders.d.ts.map +0 -1
- package/src/tui/folders.ts +0 -41
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { access, constants, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises"
|
|
2
|
+
import { tmpdir } from "node:os"
|
|
3
|
+
import { join, resolve } from "node:path"
|
|
4
|
+
import type { EnvName } from "@sixb/core/env"
|
|
5
|
+
import { SixbCliError } from "./errors"
|
|
6
|
+
|
|
7
|
+
const GENERATED_MARK = "# Generated by sixb env pull"
|
|
8
|
+
const READ_TIMEOUT_MS = 10_000
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* What Bun reads from a `.env` file, by asking Bun: a second parser would disagree with the one the
|
|
12
|
+
* app runs on. The child has an empty environment and an empty directory, so the result holds this
|
|
13
|
+
* file's values and nothing else. A `$NAME` in the file sees only that file.
|
|
14
|
+
*/
|
|
15
|
+
export async function readEnvFile(path: string): Promise<Record<string, string>> {
|
|
16
|
+
// Bun reads a file it can't open as an empty one, which would read as having nothing to lose.
|
|
17
|
+
await access(path, constants.R_OK).catch(() => {
|
|
18
|
+
throw new SixbCliError(`[SixbEnv] Could not read ${path}.`)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const directory = await mkdtemp(join(tmpdir(), "sixb-env-read-"))
|
|
22
|
+
try {
|
|
23
|
+
const child = Bun.spawn(
|
|
24
|
+
[
|
|
25
|
+
process.execPath,
|
|
26
|
+
`--env-file=${resolve(path)}`,
|
|
27
|
+
"-e",
|
|
28
|
+
"process.stdout.write(JSON.stringify(process.env))",
|
|
29
|
+
],
|
|
30
|
+
{
|
|
31
|
+
cwd: directory,
|
|
32
|
+
env: {},
|
|
33
|
+
stdin: "ignore",
|
|
34
|
+
stdout: "pipe",
|
|
35
|
+
stderr: "ignore",
|
|
36
|
+
timeout: READ_TIMEOUT_MS,
|
|
37
|
+
}
|
|
38
|
+
)
|
|
39
|
+
const [output, code] = await Promise.all([new Response(child.stdout).text(), child.exited])
|
|
40
|
+
// Never quote the output: it holds the file's values.
|
|
41
|
+
if (code !== 0) throw new SixbCliError(`[SixbEnv] Could not read ${path}.`)
|
|
42
|
+
return JSON.parse(output) as Record<string, string>
|
|
43
|
+
} finally {
|
|
44
|
+
await rm(directory, { recursive: true, force: true })
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Whether this `.env` came from `sixb env pull`, so overwriting it loses nothing. */
|
|
49
|
+
export async function isGeneratedEnvFile(path: string): Promise<boolean> {
|
|
50
|
+
const text = await readFile(path, "utf8").catch(() => "")
|
|
51
|
+
return text.startsWith(GENERATED_MARK)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Writes `.env` readable only by its owner, or not at all. The new file is read back through Bun
|
|
56
|
+
* before it replaces the old one, because Bun's `.env` format can't hold every value: a trailing
|
|
57
|
+
* backslash swallows the closing quote and the next line, and a carriage return becomes a newline.
|
|
58
|
+
*/
|
|
59
|
+
export async function writeEnvFile(
|
|
60
|
+
path: string,
|
|
61
|
+
values: Readonly<Record<string, string>>,
|
|
62
|
+
env: EnvName
|
|
63
|
+
): Promise<void> {
|
|
64
|
+
// One fixed name, so a pull that was killed leaves no copy of the values behind for long.
|
|
65
|
+
const temporary = `${path}.pull.tmp`
|
|
66
|
+
try {
|
|
67
|
+
await rm(temporary, { force: true })
|
|
68
|
+
await writeFile(temporary, renderEnvFile(values, env), { mode: 0o600, flag: "wx" })
|
|
69
|
+
|
|
70
|
+
const written = await readEnvFile(temporary)
|
|
71
|
+
const names = new Set([...Object.keys(values), ...Object.keys(written)])
|
|
72
|
+
const unreadable = [...names].filter((name) => written[name] !== values[name]).sort()
|
|
73
|
+
if (unreadable.length > 0) {
|
|
74
|
+
throw new SixbCliError(
|
|
75
|
+
`[SixbEnv] A .env file can't hold the value of ${unreadable.join(", ")}.`,
|
|
76
|
+
{
|
|
77
|
+
remediation:
|
|
78
|
+
"Change the value so it has no carriage return and doesn't end in a backslash, and doesn't use all of ' \" and `.",
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
await rename(temporary, path)
|
|
84
|
+
} finally {
|
|
85
|
+
await rm(temporary, { force: true })
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function renderEnvFile(values: Readonly<Record<string, string>>, env: EnvName): string {
|
|
90
|
+
const lines = Object.entries(values)
|
|
91
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
92
|
+
.map(([name, value]) => `${name}=${quoted(value)}`)
|
|
93
|
+
const header = `${GENERATED_MARK} (${env}). Edits are overwritten: use .env.local for your own.`
|
|
94
|
+
return `${[header, ...lines].join("\n")}\n`
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Bun expands `$` in every quote style, so it's always escaped. Single quotes and backticks keep
|
|
99
|
+
* everything else as written; double quotes also turn `\n` into a newline, so they come last.
|
|
100
|
+
*/
|
|
101
|
+
function quoted(value: string): string {
|
|
102
|
+
const quote = ["'", "`", '"'].find((candidate) => !value.includes(candidate)) ?? "'"
|
|
103
|
+
return `${quote}${value.replaceAll("$", "\\$")}${quote}`
|
|
104
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
const CTRL_C = "\u0003"
|
|
2
|
+
const CTRL_D = "\u0004"
|
|
3
|
+
const BACKSPACE = "\u007f"
|
|
4
|
+
const PASTE_START = "\u001b[200~"
|
|
5
|
+
const PASTE_END = "\u001b[201~"
|
|
6
|
+
/** Keys such as the arrows, which are not part of a value. */
|
|
7
|
+
const ESCAPE_SEQUENCE = /^\u001b\[[0-9;]*[A-Za-z~]/
|
|
8
|
+
/** A long value still shows that something arrived, without filling the screen. */
|
|
9
|
+
const MOST_DOTS = 40
|
|
10
|
+
|
|
11
|
+
export interface HiddenInput {
|
|
12
|
+
readonly value: string
|
|
13
|
+
/** The start of a paste that hasn't finished arriving. */
|
|
14
|
+
readonly pending: string
|
|
15
|
+
/** `refused`: lines arrived that this terminal didn't mark as one paste. */
|
|
16
|
+
readonly outcome?: "done" | "cancelled" | "refused"
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Applies what the terminal sent. A terminal that supports it marks a paste, so a multi-line value
|
|
21
|
+
* stays whole. Without the marks, a newline with more behind it is refused: keeping only the first
|
|
22
|
+
* line would store a broken value, and "Set" would still print.
|
|
23
|
+
*/
|
|
24
|
+
export function readHiddenKeys(input: HiddenInput, text: string): HiddenInput {
|
|
25
|
+
let { value } = input
|
|
26
|
+
let rest = input.pending + text
|
|
27
|
+
|
|
28
|
+
while (rest.length > 0) {
|
|
29
|
+
if (rest.startsWith(PASTE_START)) {
|
|
30
|
+
const end = rest.indexOf(PASTE_END)
|
|
31
|
+
if (end === -1) return { value, pending: rest }
|
|
32
|
+
value += rest.slice(PASTE_START.length, end).replace(/\r\n?/g, "\n").replace(/\n$/, "")
|
|
33
|
+
rest = rest.slice(end + PASTE_END.length)
|
|
34
|
+
continue
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const sequence = ESCAPE_SEQUENCE.exec(rest)
|
|
38
|
+
if (sequence) {
|
|
39
|
+
rest = rest.slice(sequence[0].length)
|
|
40
|
+
continue
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const character = String.fromCodePoint(rest.codePointAt(0) ?? 0)
|
|
44
|
+
rest = rest.slice(character.length)
|
|
45
|
+
|
|
46
|
+
if (character === CTRL_C) return { value, pending: "", outcome: "cancelled" }
|
|
47
|
+
if (character === CTRL_D) return { value, pending: "", outcome: "done" }
|
|
48
|
+
if (character === "\r" || character === "\n") {
|
|
49
|
+
const outcome = rest.trim() === "" ? "done" : "refused"
|
|
50
|
+
return { value, pending: "", outcome }
|
|
51
|
+
}
|
|
52
|
+
if (character === BACKSPACE) value = [...value].slice(0, -1).join("")
|
|
53
|
+
else if (character >= " ") value += character
|
|
54
|
+
}
|
|
55
|
+
return { value, pending: "" }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Reads a value from a terminal without showing it. It never joins the scrollback or the shell's
|
|
60
|
+
* history.
|
|
61
|
+
*/
|
|
62
|
+
export function promptHidden(label: string): Promise<string> {
|
|
63
|
+
const { stdin, stderr } = process
|
|
64
|
+
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
let input: HiddenInput = { value: "", pending: "" }
|
|
67
|
+
let dots = 0
|
|
68
|
+
|
|
69
|
+
const onData = (chunk: Buffer) => {
|
|
70
|
+
input = readHiddenKeys(input, chunk.toString("utf8"))
|
|
71
|
+
|
|
72
|
+
const shown = Math.min([...input.value].length, MOST_DOTS)
|
|
73
|
+
stderr.write(shown > dots ? "•".repeat(shown - dots) : "\b \b".repeat(dots - shown))
|
|
74
|
+
dots = shown
|
|
75
|
+
|
|
76
|
+
if (input.outcome === undefined) return
|
|
77
|
+
stdin.off("data", onData)
|
|
78
|
+
stdin.setRawMode(false)
|
|
79
|
+
stdin.pause()
|
|
80
|
+
stderr.write("\u001b[?2004l\n")
|
|
81
|
+
|
|
82
|
+
if (input.outcome === "done") resolve(input.value)
|
|
83
|
+
else if (input.outcome === "refused") {
|
|
84
|
+
reject(new Error("[SixbEnv] A value with several lines has to come from standard input."))
|
|
85
|
+
} else {
|
|
86
|
+
void import("../ui/live/prompt").then(({ PromptCancelledError }) =>
|
|
87
|
+
reject(new PromptCancelledError())
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
stderr.write(`${label} \u001b[?2004h`)
|
|
93
|
+
stdin.setRawMode(true)
|
|
94
|
+
stdin.resume()
|
|
95
|
+
stdin.on("data", onData)
|
|
96
|
+
})
|
|
97
|
+
}
|