Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

10.8 KB
Raw Blame
/**
 * CLI launcher generation and PATH plumbing — pure helpers, no Electron imports, so
 * they unit-test under plain vitest AND load in plain node: scripts/build-assets.mjs
 * imports the built dist/launcher.js to write the launcher scripts into this package's
 * bin/, which electron-builder then ships inside the app directory.
 *
 * The launchers run the bundled @adelie/cli on the app's own Electron
 * runtime as Node (ELECTRON_RUN_AS_NODE), so an installed desktop app provides a
 * working `penguin` command without any system Node installation. How bin/ reaches
 * PATH is per platform (see cli-install.ts and build/linux/*.tpl for the deb variant).
 */
import path from "node:path";

/** CLI bundle inside the app directory, emitted by tsup alongside the shell (asar off). */
export const CLI_ENTRY_RELPATH = "dist/penguin.js";

/**
 * The CLI entry the embedded server should offer the Agents it runs — handed to it as
 * `PENGUIN_CLI_ENTRY`, which is what the server's `<root>/bin/penguin` shim execs (see the
 * server's services/cli-shim.ts). It is the same bundled entry the PATH launchers above
 * run, and it is passed explicitly because nothing else would carry it: the fork inherits
 * `process.env`, where the variable is deliberately absent — the login-shell import
 * excludes it precisely so a profile line written for a checkout cannot retarget an
 * installed app.
 *
 * An explicit `PENGUIN_CLI_ENTRY` in the launch environment still wins, as it does for
 * `PENGUIN_WEB_DIST`. A source run pins nothing: the app directory holds no bundled CLI
 * there, and the server's own fallback finds the checkout's `packages/cli/dist/penguin.js`.
 */
export function embeddedCliEntry(opts: {
  isPackaged: boolean;
  appPath: string;
  env: NodeJS.ProcessEnv;
}): string | null {
  const explicit = opts.env.PENGUIN_CLI_ENTRY?.trim();
  if (explicit) return explicit;
  return opts.isPackaged ? path.join(opts.appPath, ...CLI_ENTRY_RELPATH.split("/")) : null;
}

/**
 * Present in every launcher script below, and shipped unchanged in every desktop release
 * since 0.2.2 — which is what lets cli-link.ts recognise a `penguin` this app wrote,
 * including one written by an older version, and leave anyone else's alone.
 */
export const LAUNCHER_MARKER = "penguin CLI launcher (generated by";

/**
 * Platform executable names, fixed by electron-builder.yml: productName Adelie
 * (macOS bundle executable and Windows .exe) and linux.executableName adelie.
 */
export const MAC_EXECUTABLE = "Adelie";
export const WIN_EXECUTABLE = "Adelie.exe";
export const LINUX_EXECUTABLE = "adelie";

/**
 * POSIX launcher, written to `<app>/bin/penguin` (chmod 755). Resolves its own real
 * location first (PATH exposure is a symlink on macOS/deb), then finds the Electron
 * executable at the platform's fixed position relative to the app directory:
 * - macOS: app dir is `<bundle>/Contents/Resources/app` → `../../MacOS/Adelie`
 * - Linux: app dir is `<install>/resources/app` → `../../adelie`
 */
export function posixLauncherScript(): string {
  return `#!/bin/sh
# penguin CLI launcher (generated by packages/desktop/scripts/build-assets.mjs).
# Runs the bundled penguin CLI on this app's Electron runtime as Node
# (ELECTRON_RUN_AS_NODE=1) - no system Node installation required.

# Resolve this script's real location; PATH exposure installs a symlink to it
# (readlink -f is not portable to macOS, hence the loop).
SOURCE=$0
while [ -h "$SOURCE" ]; do
  DIR=$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)
  SOURCE=$(readlink "$SOURCE")
  case $SOURCE in
    /*) ;;
    *) SOURCE="$DIR/$SOURCE" ;;
  esac
done
BIN_DIR=$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)
APP_DIR=$(dirname "$BIN_DIR")

CLI_ENTRY="$APP_DIR/${CLI_ENTRY_RELPATH}"
# The Electron executable sits at a fixed location relative to the app directory:
# macOS <bundle>/Contents/Resources/app -> ../../MacOS/${MAC_EXECUTABLE};
# Linux <install>/resources/app -> ../../${LINUX_EXECUTABLE}.
for CANDIDATE in "$APP_DIR/../../MacOS/${MAC_EXECUTABLE}" "$APP_DIR/../../${LINUX_EXECUTABLE}"; do
  if [ -x "$CANDIDATE" ]; then
    export ELECTRON_RUN_AS_NODE=1
    exec "$CANDIDATE" "$CLI_ENTRY" "$@"
  fi
done
echo "penguin: could not find the Adelie runtime next to $APP_DIR" >&2
exit 1
`;
}

/**
 * Windows launcher, written to `<app>\\bin\\penguin.cmd`. `%~dp0` is this script's
 * directory (with a trailing backslash): the exe sits three levels up
 * (bin -> app -> resources -> install root), the CLI entry one level up in the app dir.
 * CRLF line endings on purpose - cmd.exe is unreliable with bare LF scripts.
 */
export function windowsLauncherScript(): string {
  return [
    "@echo off",
    "rem penguin CLI launcher (generated by packages/desktop/scripts/build-assets.mjs).",
    "rem Runs the bundled penguin CLI on this app's Electron runtime as Node.",
    "setlocal",
    'set "ELECTRON_RUN_AS_NODE=1"',
    `"%~dp0..\\..\\..\\${WIN_EXECUTABLE}" "%~dp0..\\${CLI_ENTRY_RELPATH.replaceAll("/", "\\")}" %*`,
    "exit /b %errorlevel%",
    "",
  ].join("\r\n");
}

/**
 * Single-line CJS bootstrap for the AppImage wrapper (`electron -e <this>`): resolves
 * the CLI entry relative to the mounted application (process.execPath is the Electron
 * binary inside the AppImage mount, resources/app sits next to it), splices it into
 * argv[1] so `node -e` argv ([execPath, ...args]) becomes the [execPath, entry, ...args]
 * shape the CLI's argv parsing expects, then imports the (ESM) entry. Must not contain
 * single quotes: the wrapper embeds it in a single-quoted shell string, so the path
 * segments are emitted with JSON.stringify rather than spelled out.
 */
export function appImageBootstrapJs(): string {
  const segments = CLI_ENTRY_RELPATH.split("/")
    .map((segment) => JSON.stringify(segment))
    .join(",");
  return (
    'const path=require("path");' +
    `const cli=path.join(path.dirname(process.execPath),"resources","app",${segments});` +
    "process.argv.splice(1,0,cli);" +
    "import(cli).catch((err)=>{console.error(err);process.exit(1);});"
  );
}

/**
 * Wrapper script installed to ~/.local/bin/penguin for the AppImage form, which has no
 * stable install directory to symlink into: it invokes the AppImage itself as Node.
 * The AppImage path is baked in at install time; the guard turns a moved/deleted
 * AppImage into a clear message instead of a shell "not found".
 */
export function appImageWrapperScript(appImagePath: string): string {
  if (appImagePath.includes("'")) {
    // A quote would break out of the single-quoted assignment below; refuse rather
    // than generate a broken (or injectable) script.
    throw new Error(`AppImage path must not contain single quotes: ${appImagePath}`);
  }
  return `#!/bin/sh
# penguin CLI launcher (generated by the Adelie desktop app).
# Runs the CLI bundled inside the AppImage by starting the AppImage's Electron
# runtime as Node (ELECTRON_RUN_AS_NODE=1). If the AppImage moves, re-run
# "Install 'penguin' Command" from the Adelie application menu.
APPIMAGE_PATH='${appImagePath}'
if [ ! -x "$APPIMAGE_PATH" ]; then
  echo "penguin: AppImage not found at $APPIMAGE_PATH (moved or deleted?)." >&2
  echo "penguin: re-run \\"Install 'penguin' Command\\" from the Adelie menu." >&2
  exit 1
fi
export ELECTRON_RUN_AS_NODE=1
# The -- ends Node option parsing: without it a leading-dash argument (penguin --help)
# would be swallowed as a Node flag instead of reaching the CLI.
exec "$APPIMAGE_PATH" -e '${appImageBootstrapJs()}' -- "$@"
`;
}

/**
 * A string as one POSIX shell word: single-quoted, with every embedded single quote
 * spliced as `'\''` (close, escaped quote, reopen). Nothing inside single quotes is
 * special to the shell, so a quoted word is safe whatever the value holds.
 */
export function shellQuote(value: string): string {
  return `'${value.replaceAll("'", `'\\''`)}'`;
}

/**
 * A string as an AppleScript literal for `osascript -e`: backslash and double quote are
 * the two characters AppleScript escapes inside a quoted string. Applied AFTER
 * {@link shellQuote}, whose `'\''` splice contributes a backslash of its own.
 */
export function appleScriptString(value: string): string {
  return `"${value.replace(/[\\"]/g, (c) => `\\${c}`)}"`;
}

/**
 * The privileged half of the macOS `penguin` install (see cli-install.ts): the
 * `osascript` source that creates the link directory and points it at the app's bundled
 * launcher, run when the unprivileged symlink was refused.
 *
 * `target` is derived from the app bundle's own location, which the user controls — a
 * bundle kept under `~/Anne's Apps/` puts an apostrophe in the middle of the command.
 * Both quoting layers are applied here so the value can never leave its shell word,
 * which under `with administrator privileges` is the difference between a failed install
 * and a root shell.
 */
export function adminSymlinkAppleScript(target: string, link: string): string {
  const dir = link.slice(0, link.lastIndexOf("/")) || "/";
  const command = [
    `mkdir -p ${shellQuote(dir)}`,
    `ln -sf ${shellQuote(target)} ${shellQuote(link)}`,
  ].join(" && ");
  return `do shell script ${appleScriptString(command)} with administrator privileges`;
}

/**
 * Idempotent HKCU\\Environment PATH append: returns the new value to write, or null
 * when `binDir` is already present (comparison ignores case, surrounding quotes and
 * trailing slashes). The existing value is preserved verbatim — only `;binDir` is
 * appended — so other software's entries are never rewritten.
 */
export function mergeWindowsUserPath(
  current: string | null | undefined,
  binDir: string,
): string | null {
  const normalize = (entry: string): string =>
    entry
      .trim()
      .replace(/^"|"$/g, "")
      .replace(/[\\/]+$/, "")
      .toLowerCase();
  const target = binDir.trim().replace(/[\\/]+$/, "");
  const base = (current ?? "").trim();
  const present = base
    .split(";")
    .map(normalize)
    .some((entry) => entry !== "" && entry === normalize(target));
  if (present) return null;
  if (base === "") return target;
  return base.endsWith(";") ? `${base}${target}` : `${base};${target}`;
}

export type CliInstallKind = "darwin" | "windows" | "appimage";

/**
 * Whether (and how) this run can install the `penguin` command onto PATH. A source run has
 * no installed app to point at; deb installs already ship a /usr/bin symlink from their postinst
 * script (build/linux/after-install.tpl), so only the AppImage form qualifies on Linux
 * (recognized by the APPIMAGE env var its runtime sets).
 */
export function cliInstallKind(opts: {
  packaged: boolean;
  platform: NodeJS.Platform;
  appImagePath: string | null;
}): CliInstallKind | null {
  if (!opts.packaged) return null;
  if (opts.platform === "darwin") return "darwin";
  if (opts.platform === "win32") return "windows";
  if (opts.platform === "linux" && opts.appImagePath !== null && opts.appImagePath !== "") {
    return "appimage";
  }
  return null;
}