/* BARE_OS_BIN_API 1.0.0 — bump when staged /bin script semantics change (see developer guide). */ /** Shared helpers for drive-resident /bin scripts (prepended before each command). */ function bareStdin(ctx) { return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : '' } /** @param {number} mode @param {'file' | 'directory' | 'symlink'} type */ function bareFormatModeString(mode, type) { const typeChar = type === 'directory' ? 'd' : type === 'symlink' ? 'l' : '-' const perm = mode & 0o777 const r = (bit) => (perm & bit ? 'r' : '-') const w = (bit) => (perm & bit ? 'w' : '-') const x = (bit) => (perm & bit ? 'x' : '-') return ( typeChar + r(0o400) + w(0o200) + x(0o100) + r(0o040) + w(0o020) + x(0o010) + r(0o004) + w(0o002) + x(0o001) ) } /** @param {number} mtimeMs @param {number} [nowMs] */ function bareFormatLsMtime(mtimeMs, nowMs) { const now = nowMs != null ? nowMs : Date.now() const d = new Date(mtimeMs) const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] const mon = months[d.getMonth()] const day = String(d.getDate()).padStart(2, ' ') const sixMo = 180 * 24 * 3600 * 1000 if (Math.abs(now - mtimeMs) > sixMo) { const yr = String(d.getFullYear()).padStart(4, ' ') return mon + ' ' + day + ' ' + yr } const hh = String(d.getHours()).padStart(2, '0') const mm = String(d.getMinutes()).padStart(2, '0') return mon + ' ' + day + ' ' + hh + ':' + mm } /** @param {number} size */ function barePosixBlocks(size) { return Math.ceil(Number(size) / 512) || 0 } /** * Raw stdout for NUL/binary when **`process.stdout.write`** is missing. * If **`ctx.bareOsBinWrite(Uint8Array|string)`** is set (tests / host), use it. * @param {Record} ctx * @param {string | Uint8Array} chunk * @returns {boolean} */ function bareOsEmitRaw(ctx, chunk) { if (typeof ctx.bareOsBinWrite === 'function') { const b4 = ctx.b4a const u8 = typeof chunk === 'string' ? b4 && typeof b4.from === 'function' ? b4.from(chunk) : new TextEncoder().encode(chunk) : chunk ctx.bareOsBinWrite(u8 instanceof Uint8Array ? u8 : new Uint8Array(u8)) return true } const w = globalThis.process?.stdout?.write if (typeof w === 'function') { w.call(globalThis.process.stdout, chunk) return true } return false } /** * pear — Pear development tools inside Bare OS. * * Provides access to the Pear runtime stack (build, bundle, stage, release, seed) * from within a booted Bare OS guest via the new ctx.pear surface. * * See docs/design/ctx-pear-surface-and-bare-audit-plan.md for the full plan and design. */ function printHelp(argv0 = 'pear') { console.log(`${argv0} — Pear development surface for Bare OS Usage: ${argv0} help ${argv0} info ${argv0} list ${argv0} stage [dir] (stub — will use ctx.pear.pearBuild) ${argv0} build [dir] (alias for stage in v1) The ctx.pear surface exposes selected Pear and Bare build/bundling packages (pear-build, pear-bundle, bare-bundle-compile, etc.) when BARE_OS_BARE_MODULES is enabled. Many operations (full release, seeding, live sidecar IPC) currently delegate to a host Pear sidecar (when available) following the same pattern as peerctl and the P2P App Store. Pear apps you create here are natural citizens of the P2P App Store: - Stage your app with Pear tooling - Publish it via `appstore` (or directly as a pear:// package) - Other users can discover and run it Use the `pear-dev` agent skill together with the `appstore` skill for autonomous "build Pear app → publish to my store" workflows. `) } async function cmdInfo(ctx) { console.log('pear — Pear development tools (ctx.pear surface)') console.log('Bare OS version of selected Pear runtime packages.') console.log('') if (ctx.pear && typeof ctx.pear === 'object') { const keys = Object.keys(ctx.pear).sort() console.log(`Exposed on ctx.pear (${keys.length} packages):`) for (const k of keys) { const val = ctx.pear[k] const type = typeof val console.log(` ${k.padEnd(20)} ${type}`) } } else { console.log('ctx.pear is not currently populated (BARE_OS_BARE_MODULES may be disabled).') } console.log('') console.log('Run "pear list" for the same view.') } async function cmdList(ctx) { if (!ctx.pear || typeof ctx.pear !== 'object') { console.log('ctx.pear is not available in this environment.') return } const keys = Object.keys(ctx.pear).sort() if (keys.length === 0) { console.log('No packages currently exposed on ctx.pear.') return } console.log(`ctx.pear — ${keys.length} package(s) available:\n`) for (const k of keys) { console.log(` ${k}`) } } async function cmdInit(ctx, targetDir = '.') { const name = 'my-pear-app' const dir = targetDir === '.' ? `${(ctx.env?.HOME || '/home/guest')}/pear-projects/${name}` : targetDir console.log(`pear init: creating minimal Pear app skeleton at ${dir} (stub implementation)`) try { await ctx.vfs.mkdir(dir, { recursive: true }) const packageJson = { name, version: '0.1.0', main: 'index.js', type: 'module', pear: { name: 'my-pear-app', type: 'desktop' } } await ctx.vfs.writeFile(`${dir}/package.json`, ctx.b4a.from(JSON.stringify(packageJson, null, 2))) await ctx.vfs.writeFile(`${dir}/index.js`, ctx.b4a.from('console.log("Hello from my Pear app!");\n')) console.log('Created basic package.json + index.js') console.log('Next (when fully wired): run "pear stage ' + dir + '" using ctx.pear.pearBuild') } catch (err) { console.error('Failed to init:', err?.message || err) } } // The coreutils runner executes this file body with `ctx` in scope. // We run the command logic directly at the bottom (consistent with other commands like appstore). const sub = (typeof argv !== 'undefined' && argv[1] ? argv[1] : 'help').toLowerCase() switch (sub) { case 'help': case '--help': case '-h': printHelp(typeof argv !== 'undefined' ? argv[0] : 'pear') break case 'info': await cmdInfo(ctx) break case 'list': case 'ls': await cmdList(ctx) break case 'stage': case 'build': const target = (typeof argv !== 'undefined' ? argv[2] : null) || '.' if (ctx.pear && ctx.pear.pearBuild) { console.log(`Attempting stage of ${target} using ctx.pear.pearBuild (early integration)...`) try { console.log('ctx.pear.pearBuild is available. Full stage pipeline landing in upcoming plan items.') console.log('Exposed Pear build surface:', Object.keys(ctx.pear).filter(k => k.toLowerCase().includes('build') || k.toLowerCase().includes('bundle'))) } catch (e) { console.error('Stage attempt error:', e?.message || e) } } else { console.log('pear stage / build: advanced integration in progress (ctx.pear.pearBuild available).') console.log('Current exposed packages on ctx.pear:') await cmdList(ctx) } break case 'init': await cmdInit(ctx, (typeof argv !== 'undefined' ? argv[2] : null)) break default: console.error(`Unknown subcommand: ${sub}`) printHelp(typeof argv !== 'undefined' ? argv[0] : 'pear') break }