Repository navigation
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·333 lines (312 loc) · 13.9 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·333 lines (312 loc) · 13.9 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env bash
# Pull the latest ViewCode, build the desktop app and open it.
#
# ./build.sh pull, install, build, open the desktop app
# ./build.sh --no-pull build what is checked out
# ./build.sh --web run server + web UI in dev mode instead (open the printed URL)
# ./build.sh --install build ViewCode for the browser and install a `viewcode`
# command (~/.local/bin): run `viewcode` any time to start
# the server and open ViewCode in your browser, no desktop
# app needed. Run it again after pulling to update.
# ./build.sh --managed for locked-down machines: enables only Claude and
# Cursor, so ViewCode never launches Codex, OpenCode, Grok,
# Antigravity or Command Code
# (docs/operations/managed-mode-plan.md)
# ./build.sh --relay set up "Quick connect" first: deploys a small relay Worker
# to YOUR Cloudflare account (signs you in with wrangler if
# needed) and stores its address and a secret for ViewCode.
# Undo with: node scripts/viewcode-relay.ts remove
# (docs/operations/viewcode-relay.md)
# ./build.sh --fresh start as a brand-new install: moves your ViewCode data
# (~/.viewcode) and the desktop app's profile aside, with a
# timestamp, so onboarding and the provider picker show again.
# Nothing is deleted; the script prints how to restore.
# Refuses to run while ViewCode is open.
#
# Run from anywhere; it works in the folder this script lives in.
set -euo pipefail
cd "$(dirname "$0")"
pull=1
mode=desktop
managed=0
relay=0
fresh=0
install=0
for arg in "$@"; do
case "$arg" in
--no-pull) pull=0 ;;
--web) mode=web ;;
--managed) managed=1 ;;
--relay) relay=1 ;;
--fresh) fresh=1 ;;
--install) install=1 ;;
-h | --help)
sed -n '2,27p' "$0"
exit 0
;;
*)
echo "Unknown option: $arg (try --help)" >&2
exit 2
;;
esac
done
step() { printf '\n\033[1m==> %s\033[0m\n' "$*"; }
node_ok() {
command -v node >/dev/null &&
node -e 'const [a,b]=process.versions.node.split(".").map(Number);process.exit(a>24||(a===24&&b>=13)?0:1)'
}
# --fresh must inspect the actual target directories before any setup can move
# legacy tools out of them. Prefer an already-installed private Node when the
# system Node is too old. An unavailable resolver is not evidence of idle data.
refuse_fresh_while_running() {
if ! "$state_node" scripts/build-state.ts check "$mode"; then
echo "Fresh reset refused; no data was moved. This check needs Node 24.13+ and installed repo dependencies." >&2
exit 1
fi
}
if [ "$fresh" = 1 ]; then
state_node=$(command -v node || true)
if ! node_ok; then
state_node=$(ls -d "$HOME"/.viewcode-tools/node/node-v24.*/bin/node 2>/dev/null | tail -1 || true)
fi
[ -n "$state_node" ] || {
echo "Cannot check fresh-reset targets without Node; install Node 24.13+ and repo dependencies first." >&2
exit 1
}
refuse_fresh_while_running
fi
# Downloads the Node release in nodejs.org/dist/<$1> into ~/.viewcode-tools/node,
# for ViewCode only; the system Node is untouched. Sets fetched_node_dir.
fetch_node() {
local os arch ext dir base file sums
case "$(uname -s)" in
Darwin) os=darwin ext=tar.gz ;;
Linux) os=linux ext=tar.xz ;;
*) echo "Install Node.js 24.13+ from https://nodejs.org and run again." >&2; exit 1 ;;
esac
case "$(uname -m)" in
arm64 | aarch64) arch=arm64 ;;
x86_64 | amd64) arch=x64 ;;
*) echo "Unsupported CPU $(uname -m); install Node.js 24.13+ yourself." >&2; exit 1 ;;
esac
base="https://nodejs.org/dist/$1"
sums=$(curl -fsSL "$base/SHASUMS256.txt")
file=$(printf '%s\n' "$sums" | awk -v s="-$os-$arch.$ext" 'index($2, s) { print $2; exit }')
[ -n "$file" ] || { echo "Could not find a Node $1 download for $os-$arch." >&2; exit 1; }
dir="$tools_dir/node/${file%.$ext}"
if [ ! -x "$dir/bin/node" ]; then
echo "Downloading $file (used only by ViewCode, into $tools_dir/node)"
mkdir -p "$tools_dir/node"
local tmp
tmp=$(mktemp -d)
curl -fL --progress-bar "$base/$file" -o "$tmp/$file"
local want got
want=$(printf '%s\n' "$sums" | awk -v f="$file" '$2 == f { print $1 }')
if command -v shasum >/dev/null; then got=$(shasum -a 256 "$tmp/$file" | awk '{print $1}');
else got=$(sha256sum "$tmp/$file" | awk '{print $1}'); fi
[ "$want" = "$got" ] || { echo "Checksum mismatch for $file; aborting." >&2; rm -rf "$tmp"; exit 1; }
tar -xf "$tmp/$file" -C "$tools_dir/node"
rm -rf "$tmp"
fi
fetched_node_dir="$dir"
}
install_private_node() {
fetch_node latest-v24.x
export PATH="$fetched_node_dir/bin:$PATH"
}
# The server as ViewCode's own program (apps/server/dist-exe/t3), so the app
# never runs its own binary with ELECTRON_RUN_AS_NODE, which endpoint security
# on company Macs flags. Building it needs Node 25.7+: the version the release
# embeds is fetched next to build.sh's own Node, used for this step only.
build_standalone_server() {
local version
version=$(sed -n 's/^const SEA_NODE_VERSION = "\(.*\)";$/\1/p' apps/server/vite.config.ts)
[ -n "$version" ] || return 1
(fetch_node "v$version" && PATH="$PWD/node_modules/.bin:$fetched_node_dir/bin:$PATH" node apps/server/scripts/cli.ts build-exe) || return 1
# Apple Silicon only runs signed code; ad-hoc, like the source-launched app.
codesign --force --sign - apps/server/dist-exe/t3
}
step "Checking tools"
# build.sh's own Node and pnpm live apart from ViewCode's data, so --fresh can
# move the data aside without pulling the tools out from under this script.
tools_dir="$HOME/.viewcode-tools"
if [ ! -d "$tools_dir" ]; then
mkdir -p "$tools_dir"
# Earlier versions kept them inside ~/.viewcode, or in a --fresh backup of it.
for old in "$HOME/.viewcode" $(ls -d "$HOME"/.viewcode.bak-* 2>/dev/null | sort -r); do
if [ -d "$old/node" ]; then
mv "$old/node" "$tools_dir/node"
[ -d "$old/bin" ] && mv "$old/bin" "$tools_dir/bin"
break
fi
done
fi
command -v git >/dev/null || { echo "git is not installed." >&2; exit 1; }
if ! node_ok; then
latest=$(ls -d "$tools_dir"/node/node-v24.*/bin 2>/dev/null | tail -1 || true)
[ -n "$latest" ] && export PATH="$latest:$PATH"
if ! node_ok; then
echo "Node.js $(node -v 2>/dev/null || echo 'not found') is too old; ViewCode needs 24.13 or newer."
install_private_node
fi
fi
echo "Using Node.js $(node -v)"
# Trust the OS certificate store too, so installs work behind corporate TLS
# inspection (otherwise Node fails with SELF_SIGNED_CERT_IN_CHAIN).
export NODE_OPTIONS="--use-system-ca${NODE_OPTIONS:+ $NODE_OPTIONS}"
# pnpm comes from corepack in a private folder, so nothing global is changed.
export COREPACK_ENABLE_DOWNLOAD_PROMPT=0
shims="$tools_dir/bin"
mkdir -p "$shims"
corepack enable --install-directory "$shims" pnpm
export PATH="$shims:$PATH"
echo "Using pnpm $(pnpm -v)"
if [ "$pull" = 1 ]; then
branch=$(git rev-parse --abbrev-ref HEAD)
step "Pulling the latest $branch"
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "You have local changes. Commit or stash them, or run with --no-pull." >&2
git status --short >&2
exit 1
fi
git pull --ff-only origin "$branch"
fi
echo "At $(git log -1 --format='%h %s')"
step "Installing dependencies"
pnpm install --frozen-lockfile --config.confirmModulesPurge=false
# Use the launcher's env-file and worktree rules after pulling/installing too.
# Only these paths cross into the shell; credentials from .env stay private.
state_node=$(command -v node)
state_paths=$("$state_node" scripts/build-state.ts resolve "$mode")
base_dir=$(printf '%s\n' "$state_paths" | sed -n '1p')
state_dir=$(printf '%s\n' "$state_paths" | sed -n '2p')
profile_dir=$(printf '%s\n' "$state_paths" | sed -n '3p')
if [ "$fresh" = 1 ]; then
step "Fresh start: moving existing ViewCode data aside"
# Checked again: the install above takes a while.
refuse_fresh_while_running
stamp=$(date +%Y%m%d-%H%M%S)
data_dir="$base_dir"
for dir in "$data_dir" "$profile_dir"; do
if [ -e "$dir" ]; then
mv "$dir" "$dir.bak-$stamp"
echo "Moved $dir -> $dir.bak-$stamp"
echo " restore: rm -rf \"$dir\" && mv \"$dir.bak-$stamp\" \"$dir\""
fi
done
fi
if [ "$managed" = 1 ]; then
step "Managed mode: enabling only Claude and Cursor"
# Merges into the settings the launched app reads; other settings are kept.
settings_file="$state_dir/settings.json"
SETTINGS_FILE="$settings_file" node -e '
const fs = require("node:fs"), path = require("node:path");
const file = process.env.SETTINGS_FILE;
// Parsed as leniently as the server does (fromLenientJson in
// packages/shared/src/schemaJson.ts): comments and trailing commas outside
// strings are dropped. Anything still unparsable is left untouched.
const lenient = (text) =>
text
.replace(/("(?:[^"\\]|\\.)*")|\/\/[^\n]*/g, (m, str) => (str ? m : ""))
.replace(/("(?:[^"\\]|\\.)*")|\/\*[\s\S]*?\*\//g, (m, str) => (str ? m : ""))
.replace(/("(?:[^"\\]|\\.)*")|,(\s*[}\]])/g, (m, str, close) => (str ? m : (close ?? "")));
let settings = {};
if (fs.existsSync(file)) {
const text = fs.readFileSync(file, "utf8");
try { settings = JSON.parse(lenient(text)); }
catch (error) {
console.error("Cannot read " + file + " (" + error.message + "); fix or remove it first.");
process.exit(1);
}
if (!settings || typeof settings !== "object" || Array.isArray(settings)) {
console.error("Cannot read " + file + " (not a JSON object); fix or remove it first.");
process.exit(1);
}
if (lenient(text) !== text) console.log("Note: comments in " + file + " are not kept.");
}
const chosen = new Set(["claudeAgent", "cursor"]);
const drivers = ["codex", "claudeAgent", "cursor", "grok", "opencode", "antigravity", "commandCode", "droid", "devin", "pi"];
settings.providers = settings.providers ?? {};
for (const driver of drivers)
settings.providers[driver] = { ...settings.providers[driver], enabled: chosen.has(driver) };
// Explicit provider instances (extra accounts) override providers.*, so
// they follow their driver too; the envelope flag carries the choice.
for (const instance of Object.values(settings.providerInstances ?? {})) {
if (!instance || typeof instance !== "object") continue;
instance.enabled = chosen.has(instance.driver);
if (instance.config && typeof instance.config === "object") delete instance.config.enabled;
}
settings.defaultAutoPull = false;
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, JSON.stringify(settings, null, 2) + "\n");
console.log("Wrote " + file);
'
fi
if [ "$relay" = 1 ]; then
step "Quick connect: setting up your relay Worker"
# Uses the same data folder as above, so the app reads what it stores. A failed
# setup (closed sign-in, no network) does not stop the build; run it again with
# node scripts/viewcode-relay.ts deploy
"$state_node" scripts/viewcode-relay.ts deploy --mode "$mode" ||
echo "Quick connect setup did not finish; continuing without it. Re-run: node scripts/viewcode-relay.ts deploy" >&2
fi
if [ "$mode" = web ]; then
step "Starting server + web UI (Ctrl+C to stop)"
exec pnpm dev
fi
# T3 Connect (Anywhere in Connect phone) is compiled in from three values in the
# gitignored repo .env (docs/operations/connect-setup.md). This only reports
# whether they are present; it never prints them.
step "Checking T3 Connect configuration"
"$state_node" --input-type=module -e '
import { resolvePublicConfig, loadRepoEnv } from "./scripts/lib/public-config.ts";
const config = resolvePublicConfig(loadRepoEnv());
const missing = [
["T3CODE_CLERK_PUBLISHABLE_KEY", config.clerkPublishableKey],
["T3CODE_CLERK_JWT_TEMPLATE", config.clerkJwtTemplate],
["T3CODE_RELAY_URL", config.relayUrl],
].filter(([, value]) => !value).map(([name]) => name);
console.log(missing.length === 0
? "T3 Connect: configured. It will show under Connect phone > Anywhere, next to Quick connect."
: "T3 Connect: not configured (optional; missing " + missing.join(", ") + "). Connect phone > Anywhere still offers Quick connect. See docs/operations/connect-setup.md.");
'
if [ "$install" = 1 ]; then
step "Building ViewCode for the browser"
pnpm exec vp run --filter t3 build
# The launcher pins this checkout and this Node, so `viewcode` works from any
# shell. It runs the server with plain Node, as `npx t3` does: no Electron.
bin_dir="$HOME/.local/bin"
mkdir -p "$bin_dir"
launcher="$bin_dir/viewcode"
{
printf '#!/bin/sh\n'
printf '# Installed by %s/build.sh --install; run that again to update.\n' "$PWD"
printf 'exec "%s" --use-system-ca "%s/apps/server/dist/bin.mjs" "$@"\n' "$(command -v node)" "$PWD"
} >"$launcher"
chmod +x "$launcher"
echo "Installed $launcher"
case ":$PATH:" in
*":$bin_dir:"*) ;;
*)
echo "Add it to your PATH once, then open a new terminal:"
echo " echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> ~/.zshrc"
;;
esac
step "Starting ViewCode in your browser (Ctrl+C to stop; next time just run: viewcode)"
exec "$launcher"
fi
step "Building the desktop app"
pnpm build:desktop
# Single-executables are arm64-only on macOS; elsewhere the app runs its
# server as before.
if [ "$(uname -s)" = Darwin ] && [ "$(uname -m)" = arm64 ]; then
step "Building ViewCode's server program"
if build_standalone_server; then
export T3CODE_DESKTOP_SERVER_EXECUTABLE="$PWD/apps/server/dist-exe/t3"
echo "The app will run its server as $T3CODE_DESKTOP_SERVER_EXECUTABLE"
else
echo "Could not build the server program; the app starts its server the old way (itself as Node)." >&2
fi
fi
step "Opening ViewCode (close the window or press Ctrl+C to quit)"
exec pnpm start:desktop