OverviewFilesCommitsIssuesDocsReleasesPeers

pgh / lib / commands / server.js

41 lines · javascript ref:
/** pgh server start|stop|restart|status */

import { execSync } from '../_compat-exec.js'
import { join, dirname } from '#node:path'
import { fileURLToPath } from '#node:url'

const __dirname = dirname(fileURLToPath(import.meta.url))
const SCRIPTS_DIR = join(__dirname, '..', '..', '..', 'scripts')

export const usage = `pgh server — local daemon process control

  pgh server status                        Daemon status
  pgh server start | stop | restart        Run the pgv3 start/stop scripts`

export async function run (client, args) {
  const sub = args[0]
  if (!sub) throw new Error(usage)

  if (sub === 'status') {
    return client.call('status', {})
  }

  if (sub === 'start') {
    execSync('bash ' + join(SCRIPTS_DIR, 'pgv3-start.sh'), { stdio: 'inherit' })
    return null
  }

  if (sub === 'stop') {
    execSync('bash ' + join(SCRIPTS_DIR, 'pgv3-stop.sh'), { stdio: 'inherit' })
    return null
  }

  if (sub === 'restart') {
    try { execSync('bash ' + join(SCRIPTS_DIR, 'pgv3-stop.sh'), { stdio: 'inherit' }) } catch {}
    execSync('bash ' + join(SCRIPTS_DIR, 'pgv3-start.sh'), { stdio: 'inherit' })
    return null
  }

  throw new Error('unknown server subcommand: ' + sub + '\n' + usage)
}