diff --git a/cli/index.ts b/cli/index.ts index fed2ff2d..5644e5f7 100755 --- a/cli/index.ts +++ b/cli/index.ts @@ -2,6 +2,7 @@ // storebound: ship a mobile app or game to Google Play and the App Store from the terminal. // Run `storebound help` for the command list. Config: storebound.config.ts; credentials: env or .storebound/. import { parseArgs } from './lib/args'; +import { toolPath } from './lib/tools'; import { loadConfig } from './lib/config'; import type { Ctx } from './lib/ctx'; @@ -78,6 +79,8 @@ docs: README.md and the skills/ folder of the toolchain`); } async function main(): Promise { + // tools (adb, emulator, ffmpeg…) are also looked up where they usually live, for apps started without a shell PATH + process.env.PATH = toolPath(process.env); const argv = process.argv.slice(2); const [a, b] = argv; if (!a || a === 'help' || a === '--help' || a === '-h') return help(); diff --git a/cli/lib/runner.ts b/cli/lib/runner.ts index 28232e44..bcb0d435 100644 --- a/cli/lib/runner.ts +++ b/cli/lib/runner.ts @@ -42,7 +42,7 @@ export function childEnv(extra: Record = {}, machine = false): R // never inherited: the session secret, and the owner's confirmation (serve passes it per call, after a verified token) for (const [k, v] of Object.entries(process.env)) if (v !== undefined && !k.startsWith('STOREBOUND_SESSION') && k !== 'STOREBOUND_OWNER_CONFIRMED') base[k] = v; // app-managed tools (desktop data folder) come first on PATH - if (base.STOREBOUND_TOOL_PATH) base.PATH = toolPath(base); + base.PATH = toolPath(base); return { ...base, ...extra, NO_COLOR: '1', ...(machine ? { STOREBOUND_MACHINE: '1', STOREBOUND_SERVE: '1' } : {}) }; } diff --git a/cli/lib/tools.ts b/cli/lib/tools.ts index 2e145c23..d20126a8 100644 --- a/cli/lib/tools.ts +++ b/cli/lib/tools.ts @@ -23,10 +23,25 @@ export interface ToolStatus { install: { kind: 'download' | 'link'; url: string; note: string }; } -/** PATH with the app-managed tool folders first. */ -export function toolPath(env: Record = process.env): string { +/** Folders where tools usually live but that are often missing from PATH (apps started from the Dock or Finder get + * a minimal PATH): the Android SDK that Android Studio installs, Homebrew, and the usual local bin folders. */ +export function wellKnownToolDirs(env: Record = process.env, platform: string = process.platform): string[] { + const home = env.HOME ?? env.USERPROFILE ?? ''; + if (!home) return []; // a bare environment (tests, sandboxes) keeps exactly its own PATH + const sdks = [env.ANDROID_HOME, env.ANDROID_SDK_ROOT, + platform === 'darwin' ? join(home, 'Library/Android/sdk') : platform === 'win32' ? join(env.LOCALAPPDATA ?? join(home, 'AppData/Local'), 'Android/Sdk') : join(home, 'Android/Sdk')]; + const dirs = sdks.filter((d): d is string => !!d).flatMap(d => [join(d, 'platform-tools'), join(d, 'emulator')]); + if (platform === 'darwin') dirs.push('/opt/homebrew/bin', '/usr/local/bin'); + else if (platform !== 'win32') dirs.push('/usr/local/bin', join(home, '.local/bin')); + return [...new Set(dirs)].filter(d => existsSync(d)); +} + +/** PATH with the app-managed tool folders first and the well-known tool folders after the user's own PATH. */ +export function toolPath(env: Record = process.env, platform: string = process.platform): string { const extra = (env.STOREBOUND_TOOL_PATH ?? '').split(delimiter).filter(Boolean); - return [...extra, env.PATH ?? ''].filter(Boolean).join(delimiter); + const own = (env.PATH ?? '').split(delimiter).filter(Boolean); + const known = wellKnownToolDirs(env, platform).filter(d => !own.includes(d)); + return [...extra, ...own, ...known].join(delimiter); } /** First line of ` ` (version banners), or null. */ diff --git a/tests/tools-path.test.ts b/tests/tools-path.test.ts new file mode 100644 index 00000000..a26763da --- /dev/null +++ b/tests/tools-path.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, test } from 'bun:test'; +import { mkdirSync, mkdtempSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { toolPath, wellKnownToolDirs } from '../cli/lib/tools'; + +describe('tool folders outside PATH', () => { + test('finds the Android SDK Android Studio installs, after the own PATH', () => { + const home = mkdtempSync(join(tmpdir(), 'sb-home-')); + mkdirSync(join(home, 'Library/Android/sdk/platform-tools'), { recursive: true }); + mkdirSync(join(home, 'Library/Android/sdk/emulator'), { recursive: true }); + const env = { HOME: home, PATH: '/usr/bin:/bin' }; + expect(wellKnownToolDirs(env, 'darwin')).toContain(join(home, 'Library/Android/sdk/platform-tools')); + const p = toolPath(env, 'darwin').split(':'); + expect(p.slice(0, 2)).toEqual(['/usr/bin', '/bin']); + expect(p).toContain(join(home, 'Library/Android/sdk/emulator')); + }); + + test('ANDROID_HOME wins, missing folders are skipped, app folders come first', () => { + const sdk = mkdtempSync(join(tmpdir(), 'sb-sdk-')); + mkdirSync(join(sdk, 'platform-tools')); + const env = { HOME: '/nonexistent', ANDROID_HOME: sdk, PATH: '/usr/bin', STOREBOUND_TOOL_PATH: '/app/tools' }; + const p = toolPath(env, 'linux').split(':'); + expect(p[0]).toBe('/app/tools'); + expect(p).toContain(join(sdk, 'platform-tools')); + expect(p).not.toContain(join(sdk, 'emulator')); + }); +});