@lobehub/cli 0.0.152 → 0.0.154

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/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { createRequire } from "node:module";
3
3
  import crypto$1, { X509Certificate, createHash, randomBytes, randomUUID, sign } from "node:crypto";
4
4
  import * as fs$5 from "node:fs";
5
- import fs, { appendFileSync, closeSync, constants, createReadStream, createWriteStream, existsSync, fstatSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, readdirSync, readlinkSync, realpathSync, rmSync, statSync, symlinkSync, unlink, writeFileSync } from "node:fs";
5
+ import fs, { appendFileSync, chmodSync, closeSync, constants, createReadStream, createWriteStream, existsSync, fstatSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, readdirSync, readlinkSync, realpathSync, rmSync, statSync, symlinkSync, unlink, writeFileSync } from "node:fs";
6
6
  import * as os$4 from "node:os";
7
7
  import os, { EOL, constants as constants$1, homedir, platform, tmpdir } from "node:os";
8
8
  import * as path$4 from "node:path";
@@ -51039,6 +51039,300 @@ function registerAgentSignalCommand(program) {
51039
51039
  });
51040
51040
  }
51041
51041
 
51042
+ //#endregion
51043
+ //#region src/utils/desktopApp.ts
51044
+ const CHANNELS$1 = [
51045
+ "stable",
51046
+ "beta",
51047
+ "nightly"
51048
+ ];
51049
+ /** Matches `productName` in scripts/electronWorkflow/setDesktopVersion.ts. */
51050
+ const BUNDLE_NAMES = {
51051
+ beta: "LobeHub-Beta",
51052
+ nightly: "LobeHub-Nightly",
51053
+ stable: "LobeHub"
51054
+ };
51055
+ /** The shell command `lh app --install-cli` writes. */
51056
+ const CLI_SHIM_NAME = "lobehub";
51057
+ const SHIM_MARKER = "# installed by `lh app --install-cli`";
51058
+ const isDesktopChannel = (value) => CHANNELS$1.includes(value);
51059
+ const macBundleCandidates = (channel) => ["/Applications", path.join(os.homedir(), "Applications")].map((dir) => path.join(dir, `${BUNDLE_NAMES[channel]}.app`));
51060
+ /**
51061
+ * The executable inside a `.app`. `CFBundleExecutable` is not always the
51062
+ * product name (a downstream build may rename it), so fall back to reading the
51063
+ * single binary out of `Contents/MacOS` instead of guessing again.
51064
+ */
51065
+ const macExecutablePath = (bundlePath) => {
51066
+ const macosDir = path.join(bundlePath, "Contents", "MacOS");
51067
+ const named = path.join(macosDir, BUNDLE_NAMES.stable);
51068
+ if (existsSync(named)) return named;
51069
+ try {
51070
+ const binary = readdirSync(macosDir).find((entry) => {
51071
+ try {
51072
+ return statSync(path.join(macosDir, entry)).isFile();
51073
+ } catch {
51074
+ return false;
51075
+ }
51076
+ });
51077
+ return binary ? path.join(macosDir, binary) : null;
51078
+ } catch {
51079
+ return null;
51080
+ }
51081
+ };
51082
+ /**
51083
+ * Windows and Linux binary names are NOT derived from the channel's product
51084
+ * name. `electron-builder.mjs` pins `win.executableName` to a single constant
51085
+ * for every channel, while only the product (and therefore the install
51086
+ * directory) carries the suffix — so beta and nightly both ship
51087
+ * `LobeHub.exe` inside their own folder. Looking for `LobeHub-Beta.exe` finds
51088
+ * nothing and reports a missing installation.
51089
+ *
51090
+ * Linux leaves `executableName` unset, so the binary follows the product name;
51091
+ * the lowercase form covers a build that lowercased it.
51092
+ */
51093
+ const platformExecutableNames = (channel, platform = process.platform) => {
51094
+ if (platform === "win32") return ["LobeHub.exe"];
51095
+ if (platform === "linux") return [BUNDLE_NAMES[channel], BUNDLE_NAMES[channel].toLowerCase()];
51096
+ return [];
51097
+ };
51098
+ const toLocation = (channel, installPath, platform = process.platform) => {
51099
+ if (platform === "darwin") {
51100
+ const executablePath = macExecutablePath(installPath);
51101
+ return executablePath ? {
51102
+ channel,
51103
+ executablePath,
51104
+ installPath
51105
+ } : null;
51106
+ }
51107
+ for (const name of platformExecutableNames(channel, platform)) {
51108
+ const executablePath = path.join(installPath, name);
51109
+ if (existsSync(executablePath)) return {
51110
+ channel,
51111
+ executablePath,
51112
+ installPath
51113
+ };
51114
+ }
51115
+ return null;
51116
+ };
51117
+ const candidateInstallPaths = (channel) => {
51118
+ if (process.platform === "darwin") return macBundleCandidates(channel);
51119
+ const name = BUNDLE_NAMES[channel];
51120
+ if (process.platform === "win32") {
51121
+ const localAppData = process.env.LOCALAPPDATA ?? path.join(os.homedir(), "AppData", "Local");
51122
+ return [path.join(localAppData, "Programs", name), path.join(localAppData, name)];
51123
+ }
51124
+ return [
51125
+ `/opt/${name}`,
51126
+ path.join(os.homedir(), ".local", "opt", name),
51127
+ `/usr/lib/${name}`
51128
+ ];
51129
+ };
51130
+ /**
51131
+ * Find an installed desktop build.
51132
+ *
51133
+ * `LOBEHUB_DESKTOP_PATH` short-circuits the search so a dev build, a
51134
+ * white-label bundle, or a copy in `~/Downloads` can be used without teaching
51135
+ * the CLI every layout. It accepts either the bundle and the executable.
51136
+ */
51137
+ const resolveDesktopApp = (channel, env = process.env) => {
51138
+ const override = env.LOBEHUB_DESKTOP_PATH;
51139
+ if (override) {
51140
+ const located = locateFromOverride(override, channel ?? "stable");
51141
+ if (located) return located;
51142
+ }
51143
+ for (const candidateChannel of channel ? [channel] : CHANNELS$1) for (const installPath of candidateInstallPaths(candidateChannel)) {
51144
+ if (!existsSync(installPath)) continue;
51145
+ const located = toLocation(candidateChannel, installPath);
51146
+ if (located) return located;
51147
+ }
51148
+ return null;
51149
+ };
51150
+ const locateFromOverride = (override, channel) => {
51151
+ const absolute = path.resolve(override);
51152
+ if (!existsSync(absolute)) return null;
51153
+ if (statSync(absolute).isDirectory() && absolute.endsWith(".app")) return toLocation(channel, absolute);
51154
+ return {
51155
+ channel,
51156
+ executablePath: absolute,
51157
+ installPath: path.dirname(absolute)
51158
+ };
51159
+ };
51160
+ /**
51161
+ * Scheme the installed build answers to — a nightly and a stable install
51162
+ * coexist on one machine, and each handoff has to address its own.
51163
+ */
51164
+ const schemeFor = (location) => location.channel === "stable" ? "lobehub" : `lobehub-${location.channel}`;
51165
+ /**
51166
+ * Start the app detached and return as soon as it is running.
51167
+ *
51168
+ * NOT `--wait`: unlike an editor, the app is the session — blocking the shell
51169
+ * until the user quits would leave every terminal that ran `lh app` hanging.
51170
+ */
51171
+ const launchDesktopApp = (location, args) => new Promise((resolve, reject) => {
51172
+ const child = spawn(location.executablePath, args, {
51173
+ detached: true,
51174
+ stdio: "ignore"
51175
+ });
51176
+ child.once("error", reject);
51177
+ child.once("spawn", () => {
51178
+ child.unref();
51179
+ resolve();
51180
+ });
51181
+ });
51182
+ /** Where the shim goes: next to the running `lh`, else `~/.local/bin`. */
51183
+ const resolveShimDirectory = (env = process.env) => {
51184
+ const cliDir = env.LOBE_CLI_SHIM_DIR;
51185
+ if (cliDir) return path.resolve(cliDir);
51186
+ return path.join(os.homedir(), ".local", "bin");
51187
+ };
51188
+ const resolveShimPath = (env = process.env) => path.join(resolveShimDirectory(env), CLI_SHIM_NAME);
51189
+ /**
51190
+ * Write a one-line `lobehub` launcher.
51191
+ *
51192
+ * It execs the bundle's executable instead of symlinking to it: a symlink
51193
+ * pins whatever channel happened to be installed at the time, so switching
51194
+ * from nightly to stable would silently keep launching the old build.
51195
+ */
51196
+ const installShim = (location, env = process.env) => {
51197
+ const shimPath = resolveShimPath(env);
51198
+ mkdirSync(path.dirname(shimPath), { recursive: true });
51199
+ if (existsSync(shimPath)) {
51200
+ const existing = readFileSafe(shimPath);
51201
+ if (existing !== null && !existing.includes(SHIM_MARKER)) return {
51202
+ path: shimPath,
51203
+ written: false
51204
+ };
51205
+ }
51206
+ writeFileSync(shimPath, [
51207
+ "#!/bin/sh",
51208
+ SHIM_MARKER,
51209
+ `exec "${location.executablePath}" "$@"`,
51210
+ ""
51211
+ ].join("\n"), "utf8");
51212
+ chmodSync(shimPath, 493);
51213
+ return {
51214
+ path: shimPath,
51215
+ written: true
51216
+ };
51217
+ };
51218
+ const uninstallShim = (env = process.env) => {
51219
+ const shimPath = resolveShimPath(env);
51220
+ if (!existsSync(shimPath)) return false;
51221
+ const existing = readFileSafe(shimPath);
51222
+ if (existing === null || !existing.includes(SHIM_MARKER)) return false;
51223
+ rmSync(shimPath);
51224
+ return true;
51225
+ };
51226
+ const readFileSafe = (filePath) => {
51227
+ try {
51228
+ return readFileSync(filePath, "utf8");
51229
+ } catch {
51230
+ return null;
51231
+ }
51232
+ };
51233
+
51234
+ //#endregion
51235
+ //#region src/commands/app.ts
51236
+ /**
51237
+ * Canonicalize the directory a launch should open.
51238
+ *
51239
+ * Resolving here, in the invoking shell's process, is what makes the feature
51240
+ * work at all: the app itself may be started by launchd with `/` as its
51241
+ * working directory, so a relative `.` would mean nothing by the time it is
51242
+ * read on the other side.
51243
+ */
51244
+ const resolveLaunchDirectory = (input = ".") => {
51245
+ const absolute = path.resolve(input);
51246
+ if (!existsSync(absolute)) return null;
51247
+ if (!statSync(absolute).isDirectory()) return null;
51248
+ try {
51249
+ return realpathSync(absolute);
51250
+ } catch {
51251
+ return null;
51252
+ }
51253
+ };
51254
+ const describeMissingApp = (channel) => {
51255
+ return [
51256
+ `No ${channel ? `${channel} channel of ` : ""}LobeHub Desktop installation was found.`,
51257
+ "",
51258
+ " Install the desktop app, or point at a build explicitly:",
51259
+ ` ${import_picocolors.default.bold("LOBEHUB_DESKTOP_PATH=/path/to/LobeHub.app")} lh app .`,
51260
+ "",
51261
+ ` Check what is installed: ${import_picocolors.default.bold("lh app --scheme")}`
51262
+ ].join("\n");
51263
+ };
51264
+ const runInstallCli = (location, options) => {
51265
+ const result = installShim(location);
51266
+ if (options.json !== void 0) return outputJson({
51267
+ ...result,
51268
+ executablePath: location.executablePath
51269
+ }, options.json);
51270
+ if (!result.written) {
51271
+ log$15.error(`Refusing to overwrite ${result.path} — it was not written by LobeHub.`);
51272
+ process.exitCode = 1;
51273
+ return;
51274
+ }
51275
+ console.log(`Installed ${import_picocolors.default.bold(result.path)} → ${import_picocolors.default.dim(location.executablePath)}`);
51276
+ console.log(`\nNow run ${import_picocolors.default.bold("lobehub .")} to open a directory.`);
51277
+ if (!(process.env.PATH ?? "").split(path.delimiter).includes(path.dirname(result.path))) console.log(import_picocolors.default.yellow(`\n Note: ${path.dirname(result.path)} is not on your PATH.`), `\n Add it in your shell profile to use the command from anywhere.`);
51278
+ };
51279
+ function registerAppCommand(program) {
51280
+ program.command("app [path]").description("Open a directory in LobeHub Desktop (like `code .`)").option("-n, --new-window", "Start a new conversation instead of revealing the existing one").option("--channel <channel>", "Which installed build to use (stable, beta, nightly)").option("--scheme", "Print the deep-link scheme of the installed build").option("--install-cli", "Install the `lobehub` shell command").option("--uninstall-cli", "Remove the `lobehub` shell command").option("--dry-run", "Print what would be launched without starting it").option("--json [fields]", "Output JSON").action(async (pathArg, options) => {
51281
+ const channel = options.channel && isDesktopChannel(options.channel) ? options.channel : void 0;
51282
+ if (options.channel && !channel) {
51283
+ log$15.error(`Unknown channel "${options.channel}". Use stable, beta, or nightly.`);
51284
+ process.exitCode = 1;
51285
+ return;
51286
+ }
51287
+ if (options.uninstallCli) {
51288
+ const removed = uninstallShim();
51289
+ if (options.json !== void 0) return outputJson({ removed }, options.json);
51290
+ console.log(removed ? "Removed the `lobehub` command." : "No `lobehub` command to remove.");
51291
+ return;
51292
+ }
51293
+ const location = resolveDesktopApp(channel);
51294
+ if (!location) {
51295
+ log$15.error(describeMissingApp(channel));
51296
+ process.exitCode = 1;
51297
+ return;
51298
+ }
51299
+ if (options.installCli) return runInstallCli(location, options);
51300
+ if (options.scheme) {
51301
+ if (options.json !== void 0) return outputJson({
51302
+ channel: location.channel,
51303
+ executablePath: location.executablePath,
51304
+ scheme: schemeFor(location)
51305
+ }, options.json);
51306
+ console.log(schemeFor(location));
51307
+ return;
51308
+ }
51309
+ const target = pathArg ?? ".";
51310
+ const directory = resolveLaunchDirectory(target);
51311
+ if (!directory) {
51312
+ log$15.error(`Not a directory: ${target}`);
51313
+ process.exitCode = 1;
51314
+ return;
51315
+ }
51316
+ const args = [directory];
51317
+ if (options.newWindow) args.push("--new-window");
51318
+ if (options.dryRun) {
51319
+ if (options.json !== void 0) return outputJson({
51320
+ args,
51321
+ channel: location.channel,
51322
+ executablePath: location.executablePath
51323
+ }, options.json);
51324
+ console.log(`${location.executablePath} ${args.join(" ")}`);
51325
+ return;
51326
+ }
51327
+ await launchDesktopApp(location, args);
51328
+ if (options.json !== void 0) return outputJson({
51329
+ channel: location.channel,
51330
+ opened: directory
51331
+ }, options.json);
51332
+ log$15.info(`Opened ${import_picocolors.default.bold(directory)} in LobeHub Desktop`);
51333
+ });
51334
+ }
51335
+
51042
51336
  //#endregion
51043
51337
  //#region src/commands/artboardComments.ts
51044
51338
  const runAgentId = () => {
@@ -593774,6 +594068,7 @@ function createProgram() {
593774
594068
  registerAgentCommand(program);
593775
594069
  registerAgentGroupCommand(program);
593776
594070
  registerAgentSignalCommand(program);
594071
+ registerAppCommand(program);
593777
594072
  registerBotCommand(program);
593778
594073
  registerArtboardCommand(program);
593779
594074
  registerChannelCommand(program);
package/man/man1/lh.1 CHANGED
@@ -1,6 +1,6 @@
1
1
  .\" Code generated by `npm run man:generate`; DO NOT EDIT.
2
2
  .\" Manual command details come from the Commander command tree.
3
- .TH LH 1 "" "@lobehub/cli 0.0.152" "User Commands"
3
+ .TH LH 1 "" "@lobehub/cli 0.0.154" "User Commands"
4
4
  .SH NAME
5
5
  lh \- LobeHub CLI \- manage and connect to LobeHub services
6
6
  .SH SYNOPSIS
@@ -89,6 +89,9 @@ Manage agent groups
89
89
  .B agent\-signal
90
90
  Inspect and trigger Agent Signal source events
91
91
  .TP
92
+ .B app
93
+ Open a directory in LobeHub Desktop (like `code .`)
94
+ .TP
92
95
  .B bot
93
96
  Manage bot integrations
94
97
  .TP
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/cli",
3
- "version": "0.0.152",
3
+ "version": "0.0.154",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/lobehub/lobehub",
@@ -83,5 +83,5 @@
83
83
  "access": "public",
84
84
  "registry": "https://registry.npmjs.org"
85
85
  },
86
- "gitHead": "9463328d58872729c60c4f759995da3ddde8b968"
86
+ "gitHead": "861773018cf365ba16d0e179b2d944eb01d0f430"
87
87
  }