|
| 1 | +/** |
| 2 | + * CLI command support for 1code |
| 3 | + * Allows users to open 1code from terminal with: 1code . or 1code /path/to/project |
| 4 | + * |
| 5 | + * Based on PR #16 by @caffeinum (Aleksey Bykhun) |
| 6 | + * https://github.com/21st-dev/1code/pull/16 |
| 7 | + */ |
| 8 | + |
| 9 | +import { app } from "electron" |
| 10 | +import { join } from "path" |
| 11 | +import { existsSync, lstatSync, readlinkSync } from "fs" |
| 12 | + |
| 13 | +// Launch directory from CLI (e.g., `1code /path/to/project`) |
| 14 | +let launchDirectory: string | null = null |
| 15 | + |
| 16 | +/** |
| 17 | + * Get the launch directory passed via CLI args (consumed once) |
| 18 | + */ |
| 19 | +export function getLaunchDirectory(): string | null { |
| 20 | + const dir = launchDirectory |
| 21 | + launchDirectory = null // consume once |
| 22 | + return dir |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Parse CLI arguments to find a directory argument |
| 27 | + * Called on app startup to handle `1code .` or `1code /path/to/project` |
| 28 | + */ |
| 29 | +export function parseLaunchDirectory(): void { |
| 30 | + // Look for a directory argument in argv |
| 31 | + // Skip electron executable and script path |
| 32 | + const args = process.argv.slice(process.defaultApp ? 2 : 1) |
| 33 | + |
| 34 | + for (const arg of args) { |
| 35 | + // Skip flags and protocol URLs |
| 36 | + if (arg.startsWith("-") || arg.includes("://")) continue |
| 37 | + |
| 38 | + // Check if it's a valid directory |
| 39 | + if (existsSync(arg)) { |
| 40 | + try { |
| 41 | + const stat = lstatSync(arg) |
| 42 | + if (stat.isDirectory()) { |
| 43 | + console.log("[CLI] Launch directory:", arg) |
| 44 | + launchDirectory = arg |
| 45 | + return |
| 46 | + } |
| 47 | + } catch { |
| 48 | + // ignore |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// CLI command installation paths |
| 55 | +const CLI_INSTALL_PATH = "/usr/local/bin/1code" |
| 56 | + |
| 57 | +function getCliSourcePath(): string { |
| 58 | + if (app.isPackaged) { |
| 59 | + return join(process.resourcesPath, "cli", "1code") |
| 60 | + } |
| 61 | + return join(__dirname, "..", "..", "resources", "cli", "1code") |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Check if the CLI command is installed |
| 66 | + */ |
| 67 | +export function isCliInstalled(): boolean { |
| 68 | + try { |
| 69 | + if (!existsSync(CLI_INSTALL_PATH)) return false |
| 70 | + const stat = lstatSync(CLI_INSTALL_PATH) |
| 71 | + if (!stat.isSymbolicLink()) return false |
| 72 | + const target = readlinkSync(CLI_INSTALL_PATH) |
| 73 | + return target === getCliSourcePath() |
| 74 | + } catch { |
| 75 | + return false |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Install the CLI command to /usr/local/bin/1code |
| 81 | + * Requires admin privileges on macOS |
| 82 | + */ |
| 83 | +export async function installCli(): Promise<{ success: boolean; error?: string }> { |
| 84 | + const { exec } = await import("child_process") |
| 85 | + const { promisify } = await import("util") |
| 86 | + const execAsync = promisify(exec) |
| 87 | + |
| 88 | + const sourcePath = getCliSourcePath() |
| 89 | + |
| 90 | + if (!existsSync(sourcePath)) { |
| 91 | + return { success: false, error: "CLI script not found in app bundle" } |
| 92 | + } |
| 93 | + |
| 94 | + try { |
| 95 | + // Remove existing if present |
| 96 | + if (existsSync(CLI_INSTALL_PATH)) { |
| 97 | + await execAsync( |
| 98 | + `osascript -e 'do shell script "rm -f ${CLI_INSTALL_PATH}" with administrator privileges'`, |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + // Create symlink with admin privileges |
| 103 | + await execAsync( |
| 104 | + `osascript -e 'do shell script "ln -s \\"${sourcePath}\\" ${CLI_INSTALL_PATH}" with administrator privileges'`, |
| 105 | + ) |
| 106 | + |
| 107 | + console.log("[CLI] Installed 1code command to", CLI_INSTALL_PATH) |
| 108 | + return { success: true } |
| 109 | + } catch (error: unknown) { |
| 110 | + const errorMessage = error instanceof Error ? error.message : "Installation failed" |
| 111 | + console.error("[CLI] Failed to install:", error) |
| 112 | + return { success: false, error: errorMessage } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Uninstall the CLI command from /usr/local/bin/1code |
| 118 | + * Requires admin privileges on macOS |
| 119 | + */ |
| 120 | +export async function uninstallCli(): Promise<{ success: boolean; error?: string }> { |
| 121 | + const { exec } = await import("child_process") |
| 122 | + const { promisify } = await import("util") |
| 123 | + const execAsync = promisify(exec) |
| 124 | + |
| 125 | + try { |
| 126 | + if (existsSync(CLI_INSTALL_PATH)) { |
| 127 | + await execAsync( |
| 128 | + `osascript -e 'do shell script "rm -f ${CLI_INSTALL_PATH}" with administrator privileges'`, |
| 129 | + ) |
| 130 | + } |
| 131 | + console.log("[CLI] Uninstalled 1code command") |
| 132 | + return { success: true } |
| 133 | + } catch (error: unknown) { |
| 134 | + const errorMessage = error instanceof Error ? error.message : "Uninstallation failed" |
| 135 | + console.error("[CLI] Failed to uninstall:", error) |
| 136 | + return { success: false, error: errorMessage } |
| 137 | + } |
| 138 | +} |
0 commit comments