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

export async function dev() {
  console.log(`dev`);
  const config = getConfig();
  const targetDir = path.resolve(__dirname, `../../generated`);
  const bin = path.resolve(__dirname, '../../node_modules/.bin');
  await generateProject(targetDir);
  await generatePages(targetDir);
  setTimeout(() => {
    open(`http://localhost:3000`);
  }, 1000);
  execa(`${bin}/next`, ['dev', targetDir], {
    env: { cwd: process.cwd(), config: JSON.stringify(config) },
  }).stdout.pipe(process.stdout);
}
