const esbuild = require("esbuild")
const fs = require("fs")
const path = require("path")

const config = require(process.cwd() + (process.argv?.length > 3 ? "/" + process.argv[3] : "/esbuild.config.js"))
const { watch } = require("chokidar")

function log(str, clear) {
    if (clear && process.stdout.cursorTo && process.stdout.clearScreenDown) {
        process.stdout.cursorTo(0, 0)
        process.stdout.clearScreenDown()
    }
    console.log("\x1b[36m%s\x1b[0m", str)
}

let buildResults
let ctx

async function build(catchError) {
    if (!ctx) ctx = await esbuild.context(config.options)
    log((buildResults ? "re" : "") + "building...")
    const timerStart = Date.now()
    try {
        buildResults = await ctx.rebuild()
        if (config.options.metafile) {
            fs.writeFileSync(
                (config.options.outfile ? path.dirname(config.options.outfile) : config.options.outdir) + "/meta.json",
                JSON.stringify(buildResults.metafile, null, 4)
            )
        }
    } catch (e) {
        console.log(e)
        if (!catchError) throw e
    }

    const timerEnd = Date.now()
    log(`built in ${timerEnd - timerStart}ms.`)
}

let bs
switch (process.argv?.length > 2 ? process.argv[2] : "build") {
    case "serve":
        console.log("\x1b[36m%s\x1b[0mserving...")
        esbuild.context(config.options).then(function (_ctx) {
            _ctx.serve(config.serve).catch((err) => {
                console.error(err)
                process.exit(1)
            })
        })
        break
    case "start":
        bs = require("browser-sync")
        bs.init(config.browserSync)
    case "watch":
        // config.options.incremental = true
        build(true)
        const watcher = watch(config.watch.path)
        log("watching files...")
        watcher.on("change", function (path) {
            log(`${path} changed`, true)
            build(true).then(() => {
                if (bs) {
                    bs.reload()
                }
            })
        })
        break
    default:
        esbuild.build(config.options).then(function (buildResults) {
            if (config.options.metafile) {
                fs.writeFileSync(
                    (config.options.outfile ? path.dirname(config.options.outfile) : config.options.outdir) +
                        "/meta.json",
                    JSON.stringify(buildResults.metafile, null, 4)
                )
            }
        })
}