OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git-v3 / examples / cli.js

119 lines · javascript ref:
#!/usr/bin/env node
/**
 * Mini CLI for pear-git-v3 (Phase 7a).
 *
 * Usage:
 *   node examples/cli.js daemon          Start the daemon
 *   node examples/cli.js demo            Spin up a daemon, seed sample data, print URL
 *
 * Storage: PEAR_GIT_STORE env var (default: ./.pear-git-v3-cli)
 * Socket:  PEAR_GIT_SOCK  env var (default: <store>/svc.sock)
 * HTTP:    PEAR_GIT_HTTP  env var (default: 4040)
 *
 * NOTE: Phase 7a has no auth — anyone with access to the socket or HTTP port
 * can read/write everything. Don't expose to a network. Local-only.
 */

import { startPearGit } from '../index.js'
import { mkdir } from 'fs/promises'
import { join, dirname } from 'path'

const storage = process.env.PEAR_GIT_STORE || './.pear-git-v3-cli'
const sock = process.env.PEAR_GIT_SOCK || join(storage, 'svc.sock')
const httpPort = parseInt(process.env.PEAR_GIT_HTTP || '4040')

const cmd = process.argv[2]

async function main () {
  if (cmd === 'daemon') {
    await mkdir(dirname(sock), { recursive: true })
    const node = await startPearGit({
      storage,
      uds: sock,
      http: { port: httpPort }
    })
    console.log('pear-git-v3 daemon (Phase 7a)')
    console.log('  storage: %s', storage)
    console.log('  uds:     %s', sock)
    console.log('  http:    http://127.0.0.1:%d', node.svc.http.port)
    console.log('  mounts:  %s', node.svc.listMounts().join(', '))
    console.log('  handlers: %d', node.svc.listHandlers().length)
    console.log('  (Ctrl-C to stop)')

    let shuttingDown = false
    process.on('SIGINT', async () => {
      if (shuttingDown) process.exit(1)
      shuttingDown = true
      console.log('\nshutting down...')
      try { await node.close() } catch (err) { console.error(err.message) }
      process.exit(0)
    })
    process.on('SIGTERM', async () => {
      if (shuttingDown) process.exit(1)
      shuttingDown = true
      try { await node.close() } catch {}
      process.exit(0)
    })
    return
  }

  if (cmd === 'demo') {
    await mkdir(dirname(sock), { recursive: true })
    const node = await startPearGit({
      storage,
      uds: sock,
      http: { port: httpPort }
    })

    console.log('--- pear-git-v3 demo (Phase 7a) ---\n')
    console.log('Daemon up:')
    console.log('  uds:  %s', sock)
    console.log('  http: http://127.0.0.1:%d\n', node.svc.http.port)

    console.log('1. Create an org')
    await node.orgs.create('my-team', 'group', {
      displayName: 'My Team',
      description: 'a demo team'
    })
    await node.orgs.addMember('my-team', 'alice', 'admin')
    await node.orgs.addMember('my-team', 'bob', 'member')
    await node.orgs.addResource('my-team', 'project-a')

    console.log('2. Open three issues')
    await node.issues.create({ title: 'Login broken', body: 'Cannot sign in', author: 'alice' })
    await node.issues.create({ title: 'Add dark mode', author: 'bob' })
    const id3 = await node.issues.create({ title: 'Update README', author: 'alice' })
    await node.issues.comment(1, { body: 'I can repro on Firefox', author: 'bob' })
    await node.issues.update(id3, { status: 'closed' }, 'alice')

    console.log('3. Open the dashboard:')
    console.log('   →  http://127.0.0.1:%d/', node.svc.http.port)
    console.log('   →  http://127.0.0.1:%d/orgs', node.svc.http.port)
    console.log('   →  http://127.0.0.1:%d/issues', node.svc.http.port)
    console.log('   →  http://127.0.0.1:%d/orgs/my-team\n', node.svc.http.port)
    console.log('   (Ctrl-C to stop)')

    let shuttingDown = false
    process.on('SIGINT', async () => {
      if (shuttingDown) process.exit(1)
      shuttingDown = true
      console.log('\nshutting down...')
      try { await node.close() } catch (err) { console.error(err.message) }
      process.exit(0)
    })
    return
  }

  console.log('pear-git-v3 example CLI (Phase 7a)')
  console.log('  daemon       Start the daemon')
  console.log('  demo         Spin up daemon + seed sample data + print URLs')
  console.log('')
  console.log('Storage: PEAR_GIT_STORE env (default: ./.pear-git-v3-cli)')
  console.log('Socket:  PEAR_GIT_SOCK  env (default: <store>/svc.sock)')
  console.log('HTTP:    PEAR_GIT_HTTP  env (default: 4040)')
  console.log('')
  console.log('Phase 7a: no identity, no auth, no swarm. Local use only.')
}

main().catch((err) => { console.error(err.message); process.exit(1) })