import { run } from './auxilary.mjs'

import { select, input } from '@inquirer/prompts'
import { existsSync } from 'node:fs'

const type = await select({
  message: 'What do you want to bootstrap?',
  choices: [
    {
      name: 'A layer',
      value: 'layer',
      description: 'Bootstrap a new layer to extend tooling',
    },
    {
      name: 'An app',
      value: 'app',
      description: 'Bootstrap a new webapp',
    },
  ],
})

const name = (await input({ message: `Name your new ${type}:` })).trim()

if (type === 'layer') {
  const template = 'packages/layers/template'
  const layer = `packages/layers/${name}`

  if (existsSync(layer)) {
    console.error(`Path ${layer} already exists`)
    process.exit(2)
  }

  run(`cp -r ${template} ${layer}`)
  run(`cd ${layer} && rm -rf node_modules`)
  run(`cd ${layer}/.playground && rm -rf .nuxt && rm -rf node_modules`)
  run(`find ${layer} -type f -name '*' -print0 | xargs -0 sed -i 's/__LAYER_NAME__/${name}/g'`)
}
