const execa = require('execa');
const open = require('open');
import * as path from 'path';
import { getConfig } from '@2fn/conf';

const ADMIN_PORT = process.env.ADMIN_PORT || 4001;

export async function admin() {
  console.log(`starting admin dashboard`);
  const config = getConfig();
  const cwd = path.dirname(require.resolve('@2fn/dashboard'));

  setTimeout(() => {
    open(`http://localhost:${ADMIN_PORT}`);
  }, 2000);
  await execa('npm', ['start', '--', '-p', ADMIN_PORT.toString()], {
    cwd,
    env: { cwd: process.cwd(), config: JSON.stringify(config) },
  });
}
