|
| 1 | +import { spawn } from "node:child_process"; |
| 2 | +import * as console from "node:console"; |
| 3 | +import process from "node:process"; |
| 4 | +import type { Command } from "@commander-js/extra-typings"; |
| 5 | +import boxen from "boxen"; |
| 6 | +import chalk from "chalk"; |
| 7 | +import ora from "ora"; |
| 8 | + |
| 9 | +const NEW_CLI_INSTALL_URL = "https://cli.sfcompute.com"; |
| 10 | +const MIGRATION_GUIDE_URL = "https://sfcompute.com/migrate"; |
| 11 | + |
| 12 | +export function showMigrateBanner() { |
| 13 | + const message = `We've rewritten the sf CLI in Rust. |
| 14 | +
|
| 15 | +List idle capacity on the orderbook to |
| 16 | +recoup up to 20% of your spend. |
| 17 | +
|
| 18 | +Run 'sf migrate' to switch. Your current |
| 19 | +CLI stays as 'sf-old'. |
| 20 | +
|
| 21 | +Docs: ${MIGRATION_GUIDE_URL} |
| 22 | +Hide: SF_CLI_DISABLE_MIGRATE_BANNER=1`; |
| 23 | + |
| 24 | + console.log( |
| 25 | + boxen(chalk.yellow(message), { |
| 26 | + padding: 1, |
| 27 | + borderColor: "yellow", |
| 28 | + borderStyle: "round", |
| 29 | + }), |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +export function registerMigrate(program: Command) { |
| 34 | + return program |
| 35 | + .command("migrate") |
| 36 | + .description("Install the new Rust-based sf CLI") |
| 37 | + .action(async () => { |
| 38 | + const spinner = ora("Downloading install script").start(); |
| 39 | + let script: string; |
| 40 | + try { |
| 41 | + const response = await fetch(NEW_CLI_INSTALL_URL); |
| 42 | + if (!response.ok) { |
| 43 | + spinner.fail("Failed to download install script."); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | + script = await response.text(); |
| 47 | + spinner.succeed(); |
| 48 | + } catch (err) { |
| 49 | + spinner.fail("Failed to download install script."); |
| 50 | + console.error(err); |
| 51 | + process.exit(1); |
| 52 | + } |
| 53 | + |
| 54 | + console.log(chalk.cyan("\nInstalling the new Rust sf CLI...\n")); |
| 55 | + |
| 56 | + if (process.env.IS_DEVELOPMENT_CLI_ENV) { |
| 57 | + console.log( |
| 58 | + chalk.yellow( |
| 59 | + "[dev] Skipping install script execution (IS_DEVELOPMENT_CLI_ENV).\n", |
| 60 | + ), |
| 61 | + ); |
| 62 | + } else { |
| 63 | + const bashProcess = spawn("bash", [], { |
| 64 | + stdio: ["pipe", "inherit", "inherit"], |
| 65 | + env: process.env, |
| 66 | + }); |
| 67 | + |
| 68 | + // Without an error listener, spawn failures (ENOENT/EACCES on bash) emit |
| 69 | + // an unhandled 'error' event and crash the CLI instead of exiting cleanly. |
| 70 | + const spawnError = new Promise<Error>((resolve) => { |
| 71 | + bashProcess.once("error", resolve); |
| 72 | + }); |
| 73 | + |
| 74 | + try { |
| 75 | + bashProcess.stdin.write(script); |
| 76 | + bashProcess.stdin.end(); |
| 77 | + } catch { |
| 78 | + // If stdin is already torn down (e.g. spawn failed synchronously), the |
| 79 | + // 'error' event handler below will surface the real reason. |
| 80 | + } |
| 81 | + |
| 82 | + const result = await Promise.race([ |
| 83 | + new Promise<{ kind: "close"; code: number | null }>((resolve) => { |
| 84 | + bashProcess.once("close", (code) => |
| 85 | + resolve({ kind: "close", code }), |
| 86 | + ); |
| 87 | + }), |
| 88 | + spawnError.then((err) => ({ kind: "error" as const, err })), |
| 89 | + ]); |
| 90 | + |
| 91 | + if (result.kind === "error") { |
| 92 | + console.error(chalk.red(`Failed to run bash: ${result.err.message}`)); |
| 93 | + process.exit(1); |
| 94 | + } |
| 95 | + |
| 96 | + if (result.code !== 0) { |
| 97 | + console.error(chalk.red("\nMigration failed.")); |
| 98 | + process.exit(1); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + console.log( |
| 103 | + boxen( |
| 104 | + chalk.cyan( |
| 105 | + `You're on the new sf. |
| 106 | +
|
| 107 | +Your previous CLI is still available as 'sf-old'. |
| 108 | +
|
| 109 | +Next steps: |
| 110 | + sf login |
| 111 | + sf availability |
| 112 | +
|
| 113 | +Docs: ${MIGRATION_GUIDE_URL}`, |
| 114 | + ), |
| 115 | + { |
| 116 | + padding: 1, |
| 117 | + borderColor: "cyan", |
| 118 | + borderStyle: "round", |
| 119 | + }, |
| 120 | + ), |
| 121 | + ); |
| 122 | + process.exit(0); |
| 123 | + }); |
| 124 | +} |
0 commit comments