@runbaze/cli 0.0.0-stage → 0.1.0

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.
Files changed (3) hide show
  1. package/README.md +34 -2
  2. package/dist/runbaze.js +287 -0
  3. package/package.json +25 -4
package/README.md CHANGED
@@ -1,3 +1,35 @@
1
- # Temporary Holding Version
1
+ # Runbaze CLI
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Run an agent's published snapshot from your local clone on macOS or Linux. You need Node.js 20 or later, Git, Bash, and the tools your project uses to run locally. The CLI uses your clone's existing Git credentials.
4
+
5
+ ## Install from a checkout
6
+
7
+ From the Runbaze monorepo:
8
+
9
+ ```sh
10
+ bun install --frozen-lockfile
11
+ cd packages/cli
12
+ npm pack
13
+ npm install --global ./runbaze-cli-0.1.0.tgz
14
+ runbaze --version
15
+ ```
16
+
17
+ You can share the resulting tarball for installation on another machine. Bun is used to build the CLI.
18
+
19
+ ## Run a preview
20
+
21
+ Copy the command from **Run locally** and paste it inside your clone of the target repository:
22
+
23
+ ```sh
24
+ runbaze preview <run-id> --repo owner/repo
25
+ ```
26
+
27
+ The dialog includes `--command` when the repository has a custom start command. The preview uses a sibling worktree named `<repo>-runbaze-preview`. It copies local environment files and SQLite databases, follows the run's snapshot, and restarts when dependency files change.
28
+
29
+ Press Ctrl+C to stop the preview. To remove the worktree:
30
+
31
+ ```sh
32
+ runbaze preview clean --repo owner/repo
33
+ ```
34
+
35
+ If Git reports local edits or untracked files, inspect the worktree before running the same command with `--force`.
@@ -0,0 +1,287 @@
1
+ #!/usr/bin/env node
2
+
3
+ // index.ts
4
+ import { spawn } from "node:child_process";
5
+ import { constants } from "node:os";
6
+ import { parseArgs } from "node:util";
7
+
8
+ // ../domain/run-snapshot.ts
9
+ var runSnapshotRef = (runId) => `refs/runbaze/runs/${runId}`;
10
+ var PREVIEW_COMMAND_MAX_LENGTH = 300;
11
+ var CONTROL_CHARACTER = /\p{Cc}/u;
12
+ var hasControlCharacter = (text) => CONTROL_CHARACTER.test(text);
13
+
14
+ // ../domain/shell.ts
15
+ function shellQuote(value) {
16
+ return `'${value.replaceAll("'", "'\\''")}'`;
17
+ }
18
+
19
+ // ../domain/local-preview.ts
20
+ var REPOSITORY_NAME = /^[A-Za-z0-9._-]+$/;
21
+ var SKIPPED_FOLDERS = [".git", ".venv", "node_modules", "vendor"];
22
+ var PRUNED_FOLDERS = SKIPPED_FOLDERS.map((folder) => `-name ${folder}`).join(" -o ");
23
+ var copyUntracked = (directory, patterns, copy) => {
24
+ const names = patterns.map((pattern) => `-name '${pattern}'`).join(" -o ");
25
+ return [
26
+ `find . \\( ${PRUNED_FOLDERS} \\) -prune -o \\( ${names} \\) -type f -print0`,
27
+ `xargs -0 -I{} sh -c '
28
+ git -C "$1" ls-files --error-unmatch -- "$2" >/dev/null 2>&1 && exit 0
29
+ path=$2
30
+ while [ "$path" != "." ]; do
31
+ [ ! -L "$1/$path" ] || exit 0
32
+ path=$(dirname "$path")
33
+ done
34
+ mkdir -p "$1/$(dirname "$2")" && ${copy}
35
+ ' _ ${directory} {}`
36
+ ].join(" | ");
37
+ };
38
+ var copyEnvFiles = (directory) => copyUntracked(directory, [".env*"], 'cp "$2" "$1/$2"');
39
+ var copySqliteDatabases = (directory) => copyUntracked(directory, ["*.sqlite", "*.sqlite3"], '{ [ -e "$1/$2" ] || cp "$2" "$1/$2"; }');
40
+ var JS_INSTALL = [
41
+ "{ if [ -f bun.lock ] || [ -f bun.lockb ]; then bun install",
42
+ "elif [ -f pnpm-lock.yaml ]; then pnpm install",
43
+ "elif [ -f yarn.lock ]; then yarn install",
44
+ "else npm install; fi; }"
45
+ ].join("; ");
46
+ var JS_DEV = [
47
+ "{ if [ -f bun.lock ] || [ -f bun.lockb ]; then bun run dev",
48
+ "elif [ -f pnpm-lock.yaml ]; then pnpm dev",
49
+ "elif [ -f yarn.lock ]; then yarn dev",
50
+ "else npm run dev; fi; }"
51
+ ].join("; ");
52
+ var LARAVEL_INSTALL = [
53
+ "composer install",
54
+ `{ [ ! -f package.json ] || ${JS_INSTALL}; }`
55
+ ].join(" && ");
56
+ var LARAVEL_DEV = `if grep -Eq '"dev"[[:space:]]*:' composer.json; then composer run dev; else php artisan serve; fi`;
57
+ var DJANGO_INSTALL = [
58
+ "if [ -f uv.lock ]; then uv sync",
59
+ "elif [ -f poetry.lock ]; then poetry install",
60
+ "else python3 -m venv .venv && .venv/bin/pip install -r requirements.txt; fi"
61
+ ].join("; ");
62
+ var DJANGO_DEV = [
63
+ "if [ -f uv.lock ]; then uv run python manage.py runserver",
64
+ "elif [ -f poetry.lock ]; then poetry run python manage.py runserver",
65
+ "else .venv/bin/python manage.py runserver; fi"
66
+ ].join("; ");
67
+ var UNKNOWN_STACK = `{ echo 'Runbaze: this repository needs a "Run locally with" command in its settings.' >&2; false; }`;
68
+ var SYNC_INTERVAL_SECONDS = 3;
69
+ var DEPENDENCY_FILES = [
70
+ "artisan",
71
+ "manage.py",
72
+ "package.json",
73
+ "*.lock",
74
+ "*.lockb",
75
+ "package-lock.json",
76
+ ".npmrc",
77
+ "pnpm-lock.yaml",
78
+ "composer.json",
79
+ "Pipfile",
80
+ "requirements*.txt",
81
+ "pyproject.toml",
82
+ "setup.py",
83
+ "setup.cfg"
84
+ ].map((file) => shellQuote(`:(glob)**/${file}`)).join(" ");
85
+ var followAgentWork = (ref) => [
86
+ "# Give the preview its own process group so restart also stops its children.",
87
+ "set -m",
88
+ 'start_preview() { bash -c "$start" runbaze "$dependency_stamp" "$sha" & preview_pid=$!; }',
89
+ "stop_preview() {",
90
+ ' [ -n "$preview_pid" ] || return 0',
91
+ ' kill -TERM -- "-$preview_pid" 2>/dev/null || true',
92
+ " for delay in 1 2 3 4 5; do",
93
+ ' kill -0 -- "-$preview_pid" 2>/dev/null || break',
94
+ " sleep 0.2",
95
+ " done",
96
+ ' kill -KILL -- "-$preview_pid" 2>/dev/null || true',
97
+ ' wait "$preview_pid" 2>/dev/null || true',
98
+ "}",
99
+ `trap 'stop_preview; rmdir "$lock"' EXIT`,
100
+ "start_preview",
101
+ "last=$sha",
102
+ "sleep 0.2",
103
+ 'while kill -0 "$preview_pid" 2>/dev/null; do',
104
+ ` if remote_ref=$(GIT_TERMINAL_PROMPT=0 git ls-remote origin ${ref} 2>/dev/null); then`,
105
+ ' if [ -z "$remote_ref" ]; then',
106
+ " echo 'Runbaze: this preview is no longer available' >&2",
107
+ " exit 0",
108
+ " fi",
109
+ ' new=$(printf "%s\\n" "$remote_ref" | cut -f1)',
110
+ ` if [ "$last" != "$new" ] && GIT_TERMINAL_PROMPT=0 git fetch -q origin ${ref}; then`,
111
+ " new=$(git rev-parse FETCH_HEAD)",
112
+ ' if git checkout -q --detach "$new"; then',
113
+ " sha=$new",
114
+ ` echo "Runbaze: updated to the agent's latest work"`,
115
+ ` if ! git diff --quiet "$last" "$new" -- ${DEPENDENCY_FILES}; then`,
116
+ " echo 'Runbaze: dependencies changed, restarting preview' >&2",
117
+ " stop_preview",
118
+ " start_preview",
119
+ " fi",
120
+ " last=$new",
121
+ " fi",
122
+ " fi",
123
+ " fi",
124
+ ` sleep ${SYNC_INTERVAL_SECONDS}`,
125
+ "done",
126
+ 'wait "$preview_pid"'
127
+ ];
128
+ var DETECTED_STACK_START = [
129
+ "install_dependencies() {",
130
+ ` if [ -f artisan ] && [ -f composer.json ]; then ${LARAVEL_INSTALL}`,
131
+ ` elif [ -f manage.py ]; then ${DJANGO_INSTALL}`,
132
+ ` elif [ -f package.json ]; then ${JS_INSTALL}`,
133
+ ` else ${UNKNOWN_STACK}; fi`,
134
+ "}",
135
+ 'installed=$(cat "$1" 2>/dev/null)',
136
+ `if [ -z "$installed" ] || ! git diff --quiet "$installed" "$2" -- ${DEPENDENCY_FILES}; then`,
137
+ ' install_dependencies && printf "%s\\n" "$2" > "$1" || exit 1',
138
+ "fi",
139
+ `if [ -f artisan ] && [ -f composer.json ]; then ${LARAVEL_DEV}`,
140
+ `elif [ -f manage.py ]; then ${DJANGO_DEV}`,
141
+ `elif [ -f package.json ]; then ${JS_DEV}; fi`
142
+ ].join(`
143
+ `);
144
+ var repositoryGuard = (repository) => {
145
+ const slug = shellQuote(repository.toLowerCase());
146
+ return `case "$(git remote get-url origin 2>/dev/null | tr '[:upper:]' '[:lower:]')" in *[:/]${slug} | *[:/]${slug}.git | *[:/]${slug}/) ;; *) echo ${shellQuote(`Runbaze: run this in your clone of ${repository}`)} >&2; exit 1 ;; esac`;
147
+ };
148
+ function previewWorktree(repository) {
149
+ const [owner, name, extra] = repository.split("/");
150
+ if (!(owner && name) || extra !== undefined || !REPOSITORY_NAME.test(owner) || !REPOSITORY_NAME.test(name)) {
151
+ return null;
152
+ }
153
+ const directory = `../${name}-runbaze-preview`;
154
+ const setup = [
155
+ repositoryGuard(repository),
156
+ 'cd "$(git rev-parse --show-toplevel)" || exit 1',
157
+ `dir="$(cd .. && pwd -P)/"${shellQuote(`${name}-runbaze-preview`)}`,
158
+ 'lock="$dir.lock"',
159
+ 'if ! mkdir "$lock" 2>/dev/null; then',
160
+ " echo 'Runbaze: a preview is already running or the worktree is locked' >&2",
161
+ " exit 1",
162
+ "fi",
163
+ `trap 'rmdir "$lock"' EXIT`,
164
+ "trap 'exit 130' INT",
165
+ "trap 'exit 143' TERM"
166
+ ];
167
+ return {
168
+ directory,
169
+ launchScript(runId, previewCommand) {
170
+ const ref = shellQuote(runSnapshotRef(runId));
171
+ const dir = '"$dir"';
172
+ const start = previewCommand ? `rm -f "$1" || exit 1
173
+ shift 2
174
+ ${previewCommand}` : DETECTED_STACK_START;
175
+ return [
176
+ ...setup,
177
+ `git fetch -q origin ${ref} || exit 1`,
178
+ "sha=$(git rev-parse FETCH_HEAD) || exit 1",
179
+ 'if git worktree list --porcelain | grep -Fxq "worktree $dir"; then',
180
+ ' git -C "$dir" checkout -q --detach "$sha" || exit 1',
181
+ "else",
182
+ ' git worktree add -q --detach "$dir" "$sha" || exit 1',
183
+ "fi",
184
+ `${copyEnvFiles(dir)} || exit 1`,
185
+ `${copySqliteDatabases(dir)} || exit 1`,
186
+ 'cd "$dir" || exit 1',
187
+ "git_dir=$(git rev-parse --absolute-git-dir) || exit 1",
188
+ 'dependency_stamp="$git_dir/runbaze-installed"',
189
+ `start=${shellQuote(start)}`,
190
+ ...followAgentWork(ref)
191
+ ].join(`
192
+ `);
193
+ },
194
+ cleanupScript(force = false) {
195
+ const remove = force ? 'git worktree remove --force "$dir"' : 'git worktree remove "$dir"';
196
+ return [...setup, remove].join(`
197
+ `);
198
+ }
199
+ };
200
+ }
201
+ // package.json
202
+ var version = "0.1.0";
203
+
204
+ // index.ts
205
+ var RUN_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
206
+ var HELP = `Usage:
207
+ runbaze preview <run-id> --repo <owner/repo> [--command=<shell-command>]
208
+ runbaze preview clean --repo <owner/repo> [--force]
209
+ runbaze --version
210
+
211
+ Run from your clone of the repository. Requires Git and Bash on macOS or Linux.
212
+ --command overrides framework detection. --force removes local edits in the preview worktree.
213
+ `;
214
+ function previewScript(args) {
215
+ const [action, ...rest] = args;
216
+ if (!action || action === "--help" || action === "-h") {
217
+ process.stdout.write(HELP);
218
+ return null;
219
+ }
220
+ if (action === "--version") {
221
+ process.stdout.write(`${version}
222
+ `);
223
+ return null;
224
+ }
225
+ if (action !== "preview") {
226
+ throw new Error(`Unknown command: ${action}. See runbaze --help.`);
227
+ }
228
+ const clean = rest[0] === "clean";
229
+ const { positionals, values } = parseArgs({
230
+ args: clean ? rest.slice(1) : rest,
231
+ allowPositionals: !clean,
232
+ options: {
233
+ repo: { type: "string" },
234
+ help: { type: "boolean", short: "h" },
235
+ ...clean ? { force: { type: "boolean" } } : { command: { type: "string" } }
236
+ }
237
+ });
238
+ if (values.help) {
239
+ process.stdout.write(HELP);
240
+ return null;
241
+ }
242
+ const worktree = values.repo && previewWorktree(values.repo);
243
+ if (!worktree) {
244
+ throw new Error("Provide the repository as --repo owner/repo.");
245
+ }
246
+ if (clean) {
247
+ return worktree.cleanupScript("force" in values && values.force === true);
248
+ }
249
+ const [runId] = positionals;
250
+ if (positionals.length !== 1 || !runId || !RUN_ID.test(runId)) {
251
+ throw new Error("Provide a valid run ID (UUID).");
252
+ }
253
+ const command = "command" in values && typeof values.command === "string" ? values.command : null;
254
+ if (command !== null && (!command.trim() || command.length > PREVIEW_COMMAND_MAX_LENGTH || hasControlCharacter(command))) {
255
+ throw new Error(`--command must be a single line of 1–${PREVIEW_COMMAND_MAX_LENGTH} characters.`);
256
+ }
257
+ return worktree.launchScript(runId.toLowerCase(), command);
258
+ }
259
+ function main() {
260
+ const script = previewScript(process.argv.slice(2));
261
+ if (script === null) {
262
+ return;
263
+ }
264
+ if (process.platform !== "darwin" && process.platform !== "linux") {
265
+ throw new Error("Local preview requires macOS, Linux, or a Linux shell in WSL.");
266
+ }
267
+ const bash = spawn("bash", ["-c", script], { stdio: "inherit" });
268
+ const forwardInterrupt = () => bash.kill("SIGINT");
269
+ const forwardTermination = () => bash.kill("SIGTERM");
270
+ process.once("SIGINT", forwardInterrupt);
271
+ process.once("SIGTERM", forwardTermination);
272
+ bash.once("error", (error) => {
273
+ process.stderr.write(`Runbaze: could not start Bash: ${error.message}
274
+ `);
275
+ process.exitCode = 1;
276
+ });
277
+ bash.once("exit", (code, signal) => {
278
+ process.exitCode = code ?? (signal ? 128 + constants.signals[signal] : 1);
279
+ });
280
+ }
281
+ try {
282
+ main();
283
+ } catch (error) {
284
+ process.stderr.write(`Runbaze: ${error instanceof Error ? error.message : String(error)}
285
+ `);
286
+ process.exitCode = 1;
287
+ }
package/package.json CHANGED
@@ -1,6 +1,27 @@
1
1
  {
2
2
  "name": "@runbaze/cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "bin": {
6
+ "runbaze": "./dist/runbaze.js"
7
+ },
8
+ "files": [
9
+ "dist/runbaze.js"
10
+ ],
11
+ "engines": {
12
+ "node": ">=20"
13
+ },
14
+ "scripts": {
15
+ "build": "bun build ./index.ts --target=node --outfile ./dist/runbaze.js",
16
+ "prepack": "bun run build",
17
+ "test": "bun run build && vitest run",
18
+ "typecheck": "tsc --noEmit --emitDeclarationOnly false"
19
+ },
20
+ "devDependencies": {
21
+ "@repo/domain": "*",
22
+ "@repo/typescript-config": "*",
23
+ "@types/node": "25.3.5",
24
+ "typescript": "^5.9.3",
25
+ "vitest": "^4.0.18"
26
+ }
27
+ }