import path from 'path'
import glob from 'fast-glob'
import chalk from 'chalk'
import consola from 'consola'
import { ensureDir, projRoot, writeJson } from '@kankan-components/build-utils'

const pathOutput = path.resolve(__dirname, '..', 'dist')

async function main() {
    await ensureDir(pathOutput)

    const basicComponents = await glob('*', {
        cwd: path.resolve(projRoot, 'packages/components/basic'),
        onlyDirectories: true,
    })

    const advanceComponents = await glob('*', {
        cwd: path.resolve(projRoot, 'packages/components/advance'),
        onlyDirectories: true,
    })

    const editorComponents = await glob('*', {
        cwd: path.resolve(projRoot, 'packages/components/editor'),
        onlyDirectories: true,
    })
    const components = basicComponents.concat(advanceComponents).concat(editorComponents)
    consola.log('components', components)

    await writeJson(path.resolve(pathOutput, 'components.json'), components)
    consola.success(chalk.green('Component list generated'))
}

main()
