- Add classify.ts as the single source of truth for CLI invocation routing (discriminated union, derives known commands from Commander) - Remove all hardcoded command lists and duplicate path detection logic - Simplify shell wrappers to dumb pipes (zero classification) - Fix hot-start: use `open -n -g -a` (VS Code pattern) so second-instance event fires when app is already running - Fix cold-start race: pull-based IPC (getPendingOpenProject) so renderer fetches the pending path after React mounts, instead of push event that arrived before the listener existed - Fix asar read corruption: unpack node-entrypoint-runner.js from asar (Node.js v24 in Electron 41 can't parse package.json inside asar) - Strip ELECTRON_RUN_AS_NODE from env when spawning desktop app from CLI
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { existsSync, statSync } from "node:fs";
|
|
import path from "node:path";
|
|
|
|
const OPEN_PROJECT_FLAG = "--open-project";
|
|
const OPEN_PROJECT_IGNORED_ARG_PREFIXES = ["-psn_", "--no-sandbox"];
|
|
|
|
function isExistingDirectoryAbsolutePath(candidate: string): boolean {
|
|
if (!path.isAbsolute(candidate) || !existsSync(candidate)) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
return statSync(candidate).isDirectory();
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function parseOpenProjectPathFromArgv(input: {
|
|
argv: string[];
|
|
isDefaultApp: boolean;
|
|
}): string | null {
|
|
const effectiveArgs = input.argv
|
|
.slice(input.isDefaultApp ? 2 : 1)
|
|
.filter(
|
|
(arg) => !OPEN_PROJECT_IGNORED_ARG_PREFIXES.some((prefix) => arg.startsWith(prefix)),
|
|
);
|
|
|
|
const positionalProjectPath = effectiveArgs.find(
|
|
(arg) => !arg.startsWith("-") && isExistingDirectoryAbsolutePath(arg),
|
|
);
|
|
if (positionalProjectPath) {
|
|
return positionalProjectPath;
|
|
}
|
|
|
|
const openProjectIndex = effectiveArgs.indexOf(OPEN_PROJECT_FLAG);
|
|
if (openProjectIndex === -1) {
|
|
return null;
|
|
}
|
|
|
|
const flaggedProjectPath = effectiveArgs[openProjectIndex + 1];
|
|
return flaggedProjectPath && isExistingDirectoryAbsolutePath(flaggedProjectPath)
|
|
? flaggedProjectPath
|
|
: null;
|
|
}
|