import path from "path";
import fs from "fs";

const rootDir = path.resolve(__dirname, "../../../");
const ignorePattern =
    /\/\.git\/|\/\.next\/|\/\.dist\/|node_modules|\/\.local_dist\/|\/\.tmp\/|\/types\/|\.config\.js|\/public\//;

function transformJsToTs(dir: string) {
    const dirContent = fs.readdirSync(dir);

    for (let i = 0; i < dirContent.length; i++) {
        const fileFolder = dirContent[i];
        const fullFileFolderPath = path.join(dir, fileFolder);
        const stat = fs.statSync(fullFileFolderPath);

        if (stat.isDirectory()) {
            transformJsToTs(fullFileFolderPath);
            continue;
        }

        if (ignorePattern.test(fullFileFolderPath)) continue;

        if (fullFileFolderPath.match(/\.jsx?$/)) {
            const extension = fullFileFolderPath.match(/\.jsx?$/)?.[0];
            if (!extension) continue;
            const newExtension = extension.replace("js", "ts");
            const newFilePath = fullFileFolderPath.replace(
                /\.jsx?$/,
                newExtension
            );

            console.log(fullFileFolderPath);
            console.log(extension, "=>", newExtension);
            console.log(newFilePath);
            console.log("\n/////////////////////////////////////////");
            console.log("/////////////////////////////////////////\n");

            fs.copyFileSync(fullFileFolderPath, newFilePath);
            fs.unlinkSync(fullFileFolderPath);
        }
    }
}

console.log("rootDir", rootDir);

transformJsToTs(rootDir);
