// Create a new project.
//
// We should probably rename this...

import { mkdirSync, writeFileSync } from "fs"
import { join } from "path"
import { apps } from "@/webapp"
import { NOSE_WWW } from "@/config"
import { isDir } from "@/utils"

import load from "./load"

export default function (project: string) {
    if (!project) throw "usage: new <project name>"

    if (apps().includes(project)) throw `${project} already exists`

    if (!isDir(NOSE_WWW)) throw `no www dir! make one in a real shell:\n$ mkdir -p ${NOSE_WWW}`

    mkdirSync(join(NOSE_WWW, project))
    writeFileSync(join(NOSE_WWW, project, `index.ts`), `export default (c: Context) =>\n  "Hello, world!"`)

    load(project)

    return `created ${project}`
}