#!/usr/bin/env node
import { existsSync, mkdirSync, writeFileSync } from "fs";
import path from "path";
import { scanAssets } from "../src/loader/scanner";


const [customAssetDir, customOutputPath] = process.argv.slice(2);
const DEFAULT_ASSETS_DIR = path.join(process.cwd(), "assets");
const DEFAULT_OUTPUT_PATH = path.join(process.cwd(), "manifest.json");

const ASSETS_DIR = customAssetDir || DEFAULT_ASSETS_DIR;
const OUTPUT_PATH = customOutputPath || DEFAULT_OUTPUT_PATH;

async function generateManifest() {
    try {
        //exist is now depreciated so usinf existsSync for checking directory existance
        if (!existsSync(ASSETS_DIR)) {
            throw new Error("Assets directory is missing!");
        }
        const manifest = await scanAssets(ASSETS_DIR);
        const outputDir = path.dirname(OUTPUT_PATH);
        if (!existsSync(outputDir)) {
            mkdirSync(outputDir, { recursive: true });
        }
        //@tushar-output manifest.json
        writeFileSync(OUTPUT_PATH, JSON.stringify(manifest, null, 2));

    } catch (error) {
        if (error instanceof Error) {
            console.error(error.message);
        }
        process.exit(1);
    }
}


generateManifest();