@kvbox/cli 0.5.34 → 0.5.36

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/kvbox.js CHANGED
@@ -164,7 +164,7 @@ function versionOf(srcFileUrl, injected) {
164
164
  }
165
165
 
166
166
  // src/version.ts
167
- var VERSION = versionOf(import.meta.url, true ? "0.5.34" : void 0);
167
+ var VERSION = versionOf(import.meta.url, true ? "0.5.36" : void 0);
168
168
 
169
169
  // src/commands/vm/index.ts
170
170
  import { Command as Command33 } from "commander";
@@ -645,7 +645,12 @@ var RescaleMachineRequest = z2.object({
645
645
  size: SizeSchema.optional(),
646
646
  vcpu: z2.number().int().min(1).max(64).optional(),
647
647
  memMib: z2.number().int().min(256).optional(),
648
- diskGib: z2.number().int().min(1).optional()
648
+ diskGib: z2.number().int().min(1).optional(),
649
+ /** Skip the capacity guard when a running box grows (#37). */
650
+ force: z2.boolean().optional()
651
+ });
652
+ var StartMachineRequest = z2.object({
653
+ force: z2.boolean().optional()
649
654
  });
650
655
  var SetMachineRequest = z2.object({
651
656
  /** Seconds without network traffic before the box is hibernated (#47); 0: never. */
@@ -1179,8 +1184,8 @@ var ServerClient = class {
1179
1184
  stopMachine(name) {
1180
1185
  return this.call("POST", `/machines/${encodeURIComponent(name)}/stop`, MachineSchema);
1181
1186
  }
1182
- startMachine(name) {
1183
- return this.call("POST", `/machines/${encodeURIComponent(name)}/start`, MachineSchema);
1187
+ startMachine(name, force = false) {
1188
+ return this.call("POST", `/machines/${encodeURIComponent(name)}/start`, MachineSchema, force ? { force } : void 0);
1184
1189
  }
1185
1190
  pauseMachine(name) {
1186
1191
  return this.call("POST", `/machines/${encodeURIComponent(name)}/pause`, MachineSchema);
@@ -1191,8 +1196,8 @@ var ServerClient = class {
1191
1196
  hibernateMachine(name) {
1192
1197
  return this.call("POST", `/machines/${encodeURIComponent(name)}/hibernate`, MachineSchema);
1193
1198
  }
1194
- wakeMachine(name) {
1195
- return this.call("POST", `/machines/${encodeURIComponent(name)}/wake`, MachineSchema);
1199
+ wakeMachine(name, force = false) {
1200
+ return this.call("POST", `/machines/${encodeURIComponent(name)}/wake`, MachineSchema, force ? { force } : void 0);
1196
1201
  }
1197
1202
  updateMachine(name) {
1198
1203
  return this.call("POST", `/machines/${encodeURIComponent(name)}/update`, MachineSchema);
@@ -1797,6 +1802,10 @@ var newCommand = new Command("new").description("boot a new box from an image (t
1797
1802
  info(` from another box once linked: kvbox vm link <box> ${displayName(box, user)} --port <port>`);
1798
1803
  info(` its variables: kvbox env ls --box ${displayName(box, user)}`);
1799
1804
  }
1805
+ if (box.image === "gh-runner") {
1806
+ info(` wake it on queued jobs: kvbox vm hook create ${displayName(box, user)}`);
1807
+ info(` then add it on GitHub: Settings \u2192 Webhooks \u2192 Add webhook (the hook's URL and secret, content type application/json, event Workflow jobs)`);
1808
+ }
1800
1809
  } finally {
1801
1810
  await client.close();
1802
1811
  }
@@ -2568,7 +2577,7 @@ for (const cmd of hookCommand.commands) cmd.option("--org <slug>", "act in this
2568
2577
 
2569
2578
  // src/commands/vm/rescale.ts
2570
2579
  import { Command as Command22, Option as Option3 } from "commander";
2571
- var rescaleCommand = new Command22("rescale").description("change a box's vCPUs, memory or disk (e.g. --vcpu 4 --mem 16GB); a running box restarts").argument("<name>", "box name").addOption(new Option3("-s, --size <size>", "a preset to start from").choices(SIZE_NAMES)).option("--vcpu <n>", "number of vCPUs").option("--mem <size>", "memory, e.g. 16GB or 4096MB").option("--disk <size>", "disk size, e.g. 32GB (grow only)").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
2580
+ var rescaleCommand = new Command22("rescale").description("change a box's vCPUs, memory or disk (e.g. --vcpu 4 --mem 16GB); a running box restarts").argument("<name>", "box name").addOption(new Option3("-s, --size <size>", "a preset to start from").choices(SIZE_NAMES)).option("--vcpu <n>", "number of vCPUs").option("--mem <size>", "memory, e.g. 16GB or 4096MB").option("--disk <size>", "disk size, e.g. 32GB (grow only)").option("--force", "skip the capacity guard when a running box grows").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
2572
2581
  const req = {
2573
2582
  ...opts.size !== void 0 ? { size: opts.size } : {},
2574
2583
  ...opts.vcpu !== void 0 ? { vcpu: parseCount(opts.vcpu) } : {},
@@ -2576,11 +2585,12 @@ var rescaleCommand = new Command22("rescale").description("change a box's vCPUs,
2576
2585
  ...opts.disk !== void 0 ? { diskGib: parseGib("--disk", opts.disk) } : {}
2577
2586
  };
2578
2587
  if (Object.keys(req).length === 0) throw new KvboxError("nothing to change: give --size, --vcpu, --mem or --disk");
2588
+ const body = opts.force ? { ...req, force: true } : req;
2579
2589
  const client = await clientFor(cmd.optsWithGlobals());
2580
2590
  try {
2581
2591
  const before = pickRef(await boxesFor(client, name), name, me(client));
2582
2592
  if (!before) throw new KvboxError(`box ${name}: not found (kvbox ls)`);
2583
- const box = await spin(`rescaling ${name}\u2026`, client.rescaleMachine(displayName(before, me(client)), req));
2593
+ const box = await spin(`rescaling ${name}\u2026`, client.rescaleMachine(displayName(before, me(client)), body));
2584
2594
  if (opts.json) {
2585
2595
  json(box);
2586
2596
  return;
@@ -2648,28 +2658,139 @@ var rmCommand3 = new Command24("rm").description("destroy a box (its disk is del
2648
2658
 
2649
2659
  // src/commands/vm/set.ts
2650
2660
  import { Command as Command25 } from "commander";
2651
- var setCommand = new Command25("set").description("change a box's settings, e.g. --auto-hibernate 30m (off: never), --idle-check /home/kvbox/idle.sh").argument("<name>", "box name").option("--auto-hibernate <duration>", "hibernate the box after this long without network traffic (30m, 4h, 1d; off: never)").option("--idle-check <script>", "a script in the box that exits 0 when it is idle, asked instead of network traffic (off: none)").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
2652
- if (opts.autoHibernate === void 0 && opts.idleCheck === void 0) throw new KvboxError("nothing to change: give --auto-hibernate or --idle-check");
2661
+
2662
+ // src/swap.ts
2663
+ import { spawn as spawn3 } from "child_process";
2664
+ var SWAP_FILE = "/rom/overlay/swapfile";
2665
+ var SWAP_KEEP_FREE_MIB = 1024;
2666
+ var SWAP_MIN_MIB = 256;
2667
+ function parseSwap(text2) {
2668
+ const t = text2.trim().toLowerCase();
2669
+ if (t === "off" || t === "none" || t === "0") return 0;
2670
+ const mib = parseMib(t);
2671
+ if (mib === void 0 || mib < SWAP_MIN_MIB) throw new KvboxError(`--swap ${text2}: give a size of at least ${SWAP_MIN_MIB}M (2G, 512M), or off`);
2672
+ return mib;
2673
+ }
2674
+ function swapScript() {
2675
+ return [
2676
+ "set -eu",
2677
+ 'mib="$1"',
2678
+ "disk=/rom/overlay",
2679
+ `f=${SWAP_FILE}`,
2680
+ "zs=/sys/module/zswap/parameters/enabled",
2681
+ 'mountpoint -q "$disk" || { echo "this box has no separate box disk at $disk (an image older than the overlay one)" >&2; exit 3; }',
2682
+ 'active() { grep -q "^$f " /proc/swaps; }',
2683
+ 'have=0; [ -f "$f" ] && have=$(( $(stat -c %s "$f") / 1048576 ))',
2684
+ 'if [ "$mib" -gt 0 ] && ! { [ "$have" -eq "$mib" ] && active; }; then',
2685
+ ' free=$(( $(stat -f -c %a "$disk") * $(stat -f -c %S "$disk") / 1048576 + have ))',
2686
+ ` if [ $(( free - mib )) -lt ${SWAP_KEEP_FREE_MIB} ]; then echo "the box disk has $free MiB free; $mib MiB of swap would leave less than ${SWAP_KEEP_FREE_MIB} MiB" >&2; exit 5; fi`,
2687
+ "fi",
2688
+ 'if [ "$mib" -eq 0 ] || ! { [ "$have" -eq "$mib" ] && active; }; then',
2689
+ ' if active; then swapoff "$f" || { echo "swapoff failed: what is in swap does not fit back in RAM; stop something first" >&2; exit 4; }; fi',
2690
+ ' rm -f "$f"',
2691
+ ' if [ "$mib" -gt 0 ]; then',
2692
+ ' fallocate -l "${mib}M" "$f"',
2693
+ ' chmod 600 "$f"',
2694
+ ' mkswap "$f" >/dev/null',
2695
+ ' swapon "$f"',
2696
+ " fi",
2697
+ "fi",
2698
+ "touch /etc/fstab",
2699
+ 'sed -i "\\#^$f #d" /etc/fstab',
2700
+ 'if [ "$mib" -gt 0 ]; then',
2701
+ ' echo "$f none swap sw 0 0" >> /etc/fstab',
2702
+ " printf 'vm.swappiness = 10\\n' > /etc/sysctl.d/90-kvbox-swap.conf",
2703
+ " sysctl -q vm.swappiness=10",
2704
+ " printf 'w /sys/module/zswap/parameters/enabled - - - - 1\\n' > /etc/tmpfiles.d/kvbox-swap.conf",
2705
+ ' echo 1 > "$zs" 2>/dev/null || true',
2706
+ "else",
2707
+ " rm -f /etc/sysctl.d/90-kvbox-swap.conf /etc/tmpfiles.d/kvbox-swap.conf",
2708
+ " sysctl -q vm.swappiness=60",
2709
+ ' echo 0 > "$zs" 2>/dev/null || true',
2710
+ "fi",
2711
+ 'line=$(grep "^$f " /proc/swaps || true)',
2712
+ "set -- $line",
2713
+ 'echo "kvbox-swap ${3:-0} ${4:-0} $(cat "$zs" 2>/dev/null || echo N)"'
2714
+ ].join("\n");
2715
+ }
2716
+ function swapCommand(mib) {
2717
+ return `sudo -n sh -c ${shellQuote(swapScript())} kvbox-swap ${mib}`;
2718
+ }
2719
+ function parseSwapReport(output) {
2720
+ const m = /^kvbox-swap (\d+) (\d+) (\S+)\s*$/m.exec(output);
2721
+ if (!m) return void 0;
2722
+ return { sizeMib: Math.round(Number(m[1]) / 1024), usedMib: Math.round(Number(m[2]) / 1024), zswap: m[3] === "Y" || m[3] === "1" };
2723
+ }
2724
+ function runSwap(ep, mib) {
2725
+ const child = spawn3("ssh", ["-n", ...sshArgs(ep), swapCommand(mib)], { stdio: ["ignore", "pipe", "pipe"] });
2726
+ const chunks = [];
2727
+ child.stdout.on("data", (c) => chunks.push(c));
2728
+ child.stderr.on("data", (c) => chunks.push(c));
2729
+ return new Promise((resolve3, reject) => {
2730
+ child.once("error", (err) => reject(new KvboxError(`could not run ssh: ${err.message}`)));
2731
+ child.once("close", (code) => resolve3({ status: code ?? 1, output: Buffer.concat(chunks).toString("utf8") }));
2732
+ });
2733
+ }
2734
+
2735
+ // src/commands/vm/set.ts
2736
+ var setCommand = new Command25("set").description("change a box's settings, e.g. --auto-hibernate 30m (off: never), --idle-check /home/kvbox/idle.sh, --swap 2G").argument("<name>", "box name").option("--auto-hibernate <duration>", "hibernate the box after this long without network traffic (30m, 4h, 1d; off: never)").option("--idle-check <script>", "a script in the box that exits 0 when it is idle, asked instead of network traffic (off: none)").option("--swap <size>", "a swap file on the box's disk for memory spikes such as a build (2G, 512M; off: none)").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
2737
+ if (opts.autoHibernate === void 0 && opts.idleCheck === void 0 && opts.swap === void 0) {
2738
+ throw new KvboxError("nothing to change: give --auto-hibernate, --idle-check or --swap");
2739
+ }
2653
2740
  const autoHibernateSec = opts.autoHibernate === void 0 ? void 0 : parseIdleTimeout(opts.autoHibernate);
2654
2741
  const idleCheck = opts.idleCheck === void 0 ? void 0 : parseIdleCheck(opts.idleCheck);
2742
+ const swapMib = opts.swap === void 0 ? void 0 : parseSwap(opts.swap);
2743
+ const hostSettings = autoHibernateSec !== void 0 || idleCheck !== void 0;
2655
2744
  const client = await clientFor(cmd.optsWithGlobals());
2656
2745
  try {
2657
2746
  const found = pickRef(await boxesFor(client, name), name, me(client));
2658
2747
  if (!found) throw new KvboxError(`box ${name}: not found (kvbox ls)`);
2659
- const box = await spin(`updating ${name}\u2026`, client.setMachine(displayName(found, me(client)), { ...autoHibernateSec !== void 0 ? { autoHibernateSec } : {}, ...idleCheck !== void 0 ? { idleCheck } : {} }));
2660
- if (!idleCheckKept(idleCheck, box)) {
2661
- throw new KvboxError(`box ${name}: its host runs a kvbox-server too old for --idle-check; it needs updating first${autoHibernateSec !== void 0 ? " (--auto-hibernate was applied)" : ""}`);
2748
+ if (swapMib !== void 0 && found.status === "stopped") throw new KvboxError(`box ${name} is stopped; --swap is set up inside the box (kvbox start ${name})`);
2749
+ let box = found;
2750
+ if (hostSettings) {
2751
+ box = await spin(`updating ${name}\u2026`, client.setMachine(displayName(found, me(client)), { ...autoHibernateSec !== void 0 ? { autoHibernateSec } : {}, ...idleCheck !== void 0 ? { idleCheck } : {} }));
2752
+ if (!idleCheckKept(idleCheck, box)) {
2753
+ throw new KvboxError(`box ${name}: its host runs a kvbox-server too old for --idle-check; it needs updating first${autoHibernateSec !== void 0 ? " (--auto-hibernate was applied)" : ""}`);
2754
+ }
2662
2755
  }
2756
+ const swap = swapMib === void 0 ? void 0 : await setSwap(client, box, name, swapMib, cmd);
2663
2757
  if (opts.json) {
2664
- json(box);
2758
+ json(swap === void 0 ? box : { ...box, swap });
2665
2759
  return;
2666
2760
  }
2667
- info(`${color.green("\u2713")} box ${color.bold(name)}: ${autoHibernateText(box.autoHibernateSec, box.idleCheck)}`);
2761
+ if (hostSettings) info(`${color.green("\u2713")} box ${color.bold(name)}: ${autoHibernateText(box.autoHibernateSec, box.idleCheck)}`);
2668
2762
  if (idleCheck && !box.autoHibernateSec) info(color.dim(` the idle check is used once auto-hibernate is on: kvbox vm set ${name} --auto-hibernate 10m`));
2763
+ if (swap) info(`${color.green("\u2713")} box ${color.bold(name)}: ${swapText(swap)}`);
2669
2764
  } finally {
2670
2765
  await client.close();
2671
2766
  }
2672
2767
  });
2768
+ async function setSwap(client, found, name, mib, cmd) {
2769
+ const cert = await ensureCert(client, void 0, void 0, { owner: found.owner });
2770
+ const wasRunning = found.status === "running";
2771
+ const box = wasRunning ? found : await wakeUp(client, found, displayName(found, me(client)));
2772
+ const ep = sshEndpoint(box, client.target);
2773
+ if (!wasRunning) await spin(`waiting for ${name} to accept SSH\u2026`, waitForSsh(ep));
2774
+ const what = mib === 0 ? `removing ${name}'s swap\u2026` : `setting up ${mib >= 1024 ? `${mib / 1024}G` : `${mib}M`} of swap in ${name}\u2026`;
2775
+ let result = await spin(what, runSwap(ep, mib));
2776
+ if (result.status === 255) {
2777
+ const again = await clientFor(cmd.optsWithGlobals());
2778
+ if (await renewAfterRefusal(again, cert, found.owner).finally(() => again.close())) result = await spin(what, runSwap(ep, mib));
2779
+ }
2780
+ const report = parseSwapReport(result.output);
2781
+ if (result.status !== 0 || !report) {
2782
+ const said = result.output.split("\n").filter((l) => l.trim() !== "" && !l.startsWith("kvbox-swap ")).join("\n ");
2783
+ if (result.status === 255) throw new KvboxError(`box ${name}: could not reach it over SSH${said ? `
2784
+ ${said}` : ""}`, 255);
2785
+ throw new KvboxError(`box ${name}: --swap failed${said ? `: ${said}` : ""}`);
2786
+ }
2787
+ return report;
2788
+ }
2789
+ function swapText(s) {
2790
+ if (s.sizeMib === 0) return "no swap";
2791
+ const size = s.sizeMib % 1024 === 0 ? `${s.sizeMib / 1024} GB` : `${s.sizeMib} MB`;
2792
+ return `${size} swap (${s.usedMib} MB used, zswap ${s.zswap ? "on" : "off"})`;
2793
+ }
2673
2794
  function idleCheckKept(asked, box) {
2674
2795
  return asked === void 0 || (box.idleCheck ?? "") === asked;
2675
2796
  }
@@ -2682,10 +2803,10 @@ function parseIdleCheck(text2) {
2682
2803
 
2683
2804
  // src/commands/vm/start.ts
2684
2805
  import { Command as Command26 } from "commander";
2685
- var startCommand = new Command26("start").description("boot a stopped box again (same name, IP and disk)").argument("<name>", "box name").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
2806
+ var startCommand = new Command26("start").description("boot a stopped box again (same name, IP and disk)").argument("<name>", "box name").option("--force", "skip the capacity guard").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
2686
2807
  const client = await clientFor(cmd.optsWithGlobals());
2687
2808
  try {
2688
- const box = await spin(`booting ${name}\u2026`, client.startMachine(name));
2809
+ const box = await spin(`booting ${name}\u2026`, client.startMachine(name, opts.force ?? false));
2689
2810
  if (opts.json) json(box);
2690
2811
  else info(`${color.green("\u2713")} ${describe(box)}`);
2691
2812
  } finally {
@@ -2862,10 +2983,10 @@ function block(u) {
2862
2983
 
2863
2984
  // src/commands/vm/wake.ts
2864
2985
  import { Command as Command32 } from "commander";
2865
- var wakeCommand = new Command32("wake").description("continue a hibernated box where it left off (same name, IP and disk)").argument("<name>", "box name").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
2986
+ var wakeCommand = new Command32("wake").description("continue a hibernated box where it left off (same name, IP and disk)").argument("<name>", "box name").option("--force", "skip the capacity guard").option("--json", "print the box as JSON").action(async (name, opts, cmd) => {
2866
2987
  const client = await clientFor(cmd.optsWithGlobals());
2867
2988
  try {
2868
- const box = await spin(`waking ${name} (loading its memory from disk)\u2026`, client.wakeMachine(name));
2989
+ const box = await spin(`waking ${name} (loading its memory from disk)\u2026`, client.wakeMachine(name, opts.force ?? false));
2869
2990
  if (opts.json) json(box);
2870
2991
  else info(`${color.green("\u2713")} box ${name} is awake, running where it left off`);
2871
2992
  } finally {
@@ -2885,7 +3006,7 @@ import { Command as Command41 } from "commander";
2885
3006
  import { Command as Command34 } from "commander";
2886
3007
 
2887
3008
  // src/commands/server/daemon.ts
2888
- import { spawn as spawn3 } from "child_process";
3009
+ import { spawn as spawn4 } from "child_process";
2889
3010
  import { readFile as readFile4 } from "fs/promises";
2890
3011
  import { setTimeout as sleep2 } from "timers/promises";
2891
3012
  async function runningPid(p = paths()) {
@@ -2920,7 +3041,7 @@ async function restartLocalServer(p = paths()) {
2920
3041
  }
2921
3042
  function runKvboxServer(args) {
2922
3043
  return new Promise((resolve3, reject) => {
2923
- const child = spawn3("kvbox-server", args, { stdio: ["ignore", "pipe", "pipe"] });
3044
+ const child = spawn4("kvbox-server", args, { stdio: ["ignore", "pipe", "pipe"] });
2924
3045
  let out = "";
2925
3046
  let err = "";
2926
3047
  child.stdout.on("data", (chunk) => out += chunk.toString());
@@ -3725,9 +3846,10 @@ function serverRemote(opts) {
3725
3846
  var nextjsTemplate = {
3726
3847
  id: "nextjs",
3727
3848
  label: "Next.js",
3728
- hint: "source shipped; installed, built and run in the box on port 3000",
3849
+ hint: "source shipped; installed, built and run in the box on $PORT (3000)",
3729
3850
  size: "s",
3730
3851
  port: 3e3,
3852
+ customPort: true,
3731
3853
  detect(pkg) {
3732
3854
  if (!pkg) return false;
3733
3855
  return "next" in { ...pkg.dependencies, ...pkg.devDependencies };
@@ -3773,7 +3895,7 @@ var nodeTemplate = {
3773
3895
  };
3774
3896
 
3775
3897
  // src/commands/deploy/templates/react.ts
3776
- import { spawn as spawn4 } from "child_process";
3898
+ import { spawn as spawn5 } from "child_process";
3777
3899
  import { access as access2 } from "fs/promises";
3778
3900
  import { join as join3 } from "path";
3779
3901
  var OUT_DIRS = ["dist", "build"];
@@ -3781,7 +3903,7 @@ async function runBuild(dir, pm) {
3781
3903
  const args = runScript(pm, "build");
3782
3904
  info(`${color.dim("$")} ${pm} ${args.join(" ")}`);
3783
3905
  const status2 = await new Promise((resolve3, reject) => {
3784
- const child = spawn4(pm, args, { cwd: dir, stdio: ["ignore", process.stderr, process.stderr], shell: process.platform === "win32" });
3906
+ const child = spawn5(pm, args, { cwd: dir, stdio: ["ignore", process.stderr, process.stderr], shell: process.platform === "win32" });
3785
3907
  child.once("error", (err) => reject(new KvboxError(`could not run ${pm}: ${err.message}`)));
3786
3908
  child.once("close", (code) => resolve3(code ?? 1));
3787
3909
  });
@@ -3866,7 +3988,7 @@ var DeployConfigSchema = z5.object({
3866
3988
  size: z5.enum(SIZE_NAMES).optional(),
3867
3989
  /** The build output directory, relative to the app; the template's default when absent. */
3868
3990
  dist: z5.string().optional(),
3869
- /** The port the app serves, for a Node server that hardcodes one; the template's (3000) when absent. */
3991
+ /** The port a Next.js app or Node server serves; the template's (3000) when absent. */
3870
3992
  port: z5.number().int().min(1).max(65535).optional()
3871
3993
  });
3872
3994
  async function readConfig(dir) {
@@ -3903,7 +4025,7 @@ function subdomainFor(name) {
3903
4025
  }
3904
4026
 
3905
4027
  // src/commands/deploy/ship.ts
3906
- import { spawn as spawn5 } from "child_process";
4028
+ import { spawn as spawn6 } from "child_process";
3907
4029
  import { readdir, stat as stat2 } from "fs/promises";
3908
4030
  import { join as join5 } from "path";
3909
4031
  var IMAGE_TOO_OLD = 3;
@@ -3925,8 +4047,8 @@ async function measure(dir, exclude = []) {
3925
4047
  return total;
3926
4048
  }
3927
4049
  async function ship(ep, dir, remote, exclude = []) {
3928
- const tar = spawn5("tar", ["-cf", "-", ...exclude.map((e) => `--exclude=${e}`), "-C", dir, "."], { stdio: ["ignore", "pipe", "pipe"] });
3929
- const ssh = spawn5("ssh", [...sshArgs(ep), remote], { stdio: ["pipe", "pipe", "pipe"] });
4050
+ const tar = spawn6("tar", ["-cf", "-", ...exclude.map((e) => `--exclude=${e}`), "-C", dir, "."], { stdio: ["ignore", "pipe", "pipe"] });
4051
+ const ssh = spawn6("ssh", [...sshArgs(ep), remote], { stdio: ["pipe", "pipe", "pipe"] });
3930
4052
  tar.stdout.pipe(ssh.stdin);
3931
4053
  const chunks = [];
3932
4054
  ssh.stdout.on("data", (c) => chunks.push(c));
@@ -3956,7 +4078,7 @@ async function ship(ep, dir, remote, exclude = []) {
3956
4078
  var shipping = { ship, measure, waitForSsh };
3957
4079
 
3958
4080
  // src/commands/deploy/index.ts
3959
- var deployCommand = new Command57("deploy").description("put the app in this directory (React, Next.js or a Node server) on a box with its own HTTPS address; redeploy by running it again").argument("[dir]", "the app's directory (default: the current one)").addOption(new Option5("-t, --template <template>", "what kind of app it is (detected from package.json when not given)").choices(TEMPLATE_IDS)).option("-n, --name <name>", "the box's name (default: the directory's name)").option("--sub <subdomain>", "its HTTPS name, https://<subdomain>.kvbox.app (default: the box's name)").addOption(new Option5("-s, --size <size>", "size of the box when it has to be made").choices(SIZE_NAMES)).option("--port <port>", "the port a Node server listens on, when it does not use $PORT (default 3000; saved to kvbox.json)", parsePort).option("-y, --yes", "ask nothing: take the defaults for whatever the flags and kvbox.json leave open").option("--json", "print the result as JSON").option("--org <slug>", "deploy into this org's boxes (saved to kvbox.json)").action(async (dirArg, opts, cmd) => {
4081
+ var deployCommand = new Command57("deploy").description("put the app in this directory (React, Next.js or a Node server) on a box with its own HTTPS address; redeploy by running it again").argument("[dir]", "the app's directory (default: the current one)").addOption(new Option5("-t, --template <template>", "what kind of app it is (detected from package.json when not given)").choices(TEMPLATE_IDS)).option("-n, --name <name>", "the box's name (default: the directory's name)").option("--sub <subdomain>", "its HTTPS name, https://<subdomain>.kvbox.app (default: the box's name)").addOption(new Option5("-s, --size <size>", "size of the box when it has to be made").choices(SIZE_NAMES)).option("--port <port>", "the port a Next.js app or Node server listens on (default 3000; saved to kvbox.json)", parsePort).option("-y, --yes", "ask nothing: take the defaults for whatever the flags and kvbox.json leave open").option("--json", "print the result as JSON").option("--org <slug>", "deploy into this org's boxes (saved to kvbox.json)").action(async (dirArg, opts, cmd) => {
3960
4082
  const dir = resolve2(dirArg ?? ".");
3961
4083
  const pkg = await readPackageJson(dir);
3962
4084
  const saved = await readConfig(dir);
@@ -4028,10 +4150,13 @@ ${result.output.trim()}`);
4028
4150
  await client.close();
4029
4151
  }
4030
4152
  });
4031
- function parsePort(v) {
4153
+ function portProblem(v) {
4032
4154
  const n = Number(v);
4033
- if (!Number.isInteger(n) || n < 1 || n > 65535) throw new InvalidArgumentError2("expected a port, 1-65535");
4034
- return n;
4155
+ return !/^\d+$/.test(v) || n < 1 || n > 65535;
4156
+ }
4157
+ function parsePort(v) {
4158
+ if (portProblem(v)) throw new InvalidArgumentError2("expected a port, 1-65535");
4159
+ return Number(v);
4035
4160
  }
4036
4161
  async function readPackageJson(dir) {
4037
4162
  try {
@@ -4095,8 +4220,12 @@ async function settle(dir, pkg, saved, opts, orgFlag, cloud) {
4095
4220
  if (open.size && ask) {
4096
4221
  size = await select("Box size", SIZE_NAMES.map((s) => ({ value: s, label: s, hint: `${SIZES[s].vcpu} vCPU, ${SIZES[s].memMib / 1024} GB RAM, ${SIZES[s].diskGib} GB disk` })), SIZE_NAMES.indexOf(template.size));
4097
4222
  }
4098
- if (opts.port !== void 0 && !template.customPort) throw new KvboxError(`--port is for a Node server; the ${template.label} template serves port ${template.port}`);
4099
- const port = opts.port ?? (template.customPort ? saved?.port : void 0);
4223
+ if (opts.port !== void 0 && !template.customPort) throw new KvboxError(`--port is for a Next.js app or a Node server; the ${template.label} template serves port ${template.port}`);
4224
+ let port = opts.port ?? (template.customPort ? saved?.port : void 0);
4225
+ if (template.customPort && port === void 0 && fresh && ask) {
4226
+ const answer = await text("Port", { default: String(template.port), validate: (a) => portProblem(a) ? "expected a port, 1-65535" : void 0 });
4227
+ port = Number(answer);
4228
+ }
4100
4229
  const org = orgFlag ?? saved?.org;
4101
4230
  const config = {
4102
4231
  template: template.id,