import {echo, exit, cat, rm} from "shelljs";
import nomnom from "nomnom";
import fs from "fs";
import path from "path";
import {exec} from "child_process";
import _ from "underscore";


const transformBasePath = path.join(__dirname, "..", "transforms");
const runFirst = [
    "resolve-relative-imports.js"
];
const runLast = [
    "remove-stilr.js",
    "convert-to-radium.js"
];

const {src, all, single, clean} = nomnom.options({
    src: {
        position: 0,
        help: "Source directory to run the transforms against"
    },
    all: {
        flag: true,
        abbr: "A",
        help: "Run all transforms in transforms folder"
    },
    single: {
        help: "Run single transform",
        abbr: "S"
    },
    clean: {
        flag: true,
        help: "Remove any files that have been marked for deletion",
        abbr: "C"
    }
}).parse();


if (!src) {
    echo("src option is required");
    exit(1);
}

const buildCMD = (filePath, file) => {
    return `jscodeshift -t ${filePath} ${file} --extensions "jsx,js"`;
};

const markForDeletion = () => {
    const emptyIndexFile = path.join(__dirname, "..", "empty_indexes.txt");
    const files = cat(emptyIndexFile)
                    .split("\n")
                    .filter(f => f !== "");
    const uniqueFiles = _.uniq(files);
    const filesList = uniqueFiles.join("\n");
    filesList.to(emptyIndexFile);

    echo("The following index files have been marked for deletion.");
    echo("run `wildcat-codemod -C` to delete them:\n");
    echo(filesList);
};

const applyTransform = (transforms) => {
    if (!transforms.length) {
        markForDeletion();
        return;
    }

    const transform = transforms.shift();
    const transformFilePath = path.join(transformBasePath, transform);
    const cmd = buildCMD(transformFilePath, src);

    echo("Applying transform", transform);

    exec(cmd, (err, stout) => {
        echo(stout);
        applyTransform(transforms);
    });
};

const deleteEmptyIndexes = () => {
    echo("Removing unused index files");
    const emptyIndexFile = path.join(__dirname, "..", "empty_indexes.txt");
    const emptyIndexFiles = cat(emptyIndexFile);

    if (emptyIndexFiles === null) {
        return;
    }

    emptyIndexFiles
        .split("\n")
        .filter(f => f !== "")
        .map(f => rm(f));
    rm(emptyIndexFile);
};

if (clean) {
    deleteEmptyIndexes();
} else if (all) {
    const transforms = fs.readdirSync(transformBasePath)
                        .filter(fileName => fileName.match(".js$"))
                        .filter(filename => runFirst.indexOf(filename) === -1)
                        .filter(filename => runLast.indexOf(filename) === -1);
    const orderedTransforms = [...runFirst, ...transforms, ...runLast];
    applyTransform(orderedTransforms);
}

if (single) {
    const transformFilePath = path.join(transformBasePath, single);
    const cmd = buildCMD(transformFilePath, src);
    exec(cmd, (err, stout) => {
        echo(err);
        echo(stout);
    });
}

@@ -1,16 +1,21 @@
 import %7B
+cat, 
 echo, ex
@@ -18,19 +18,24 @@
 , exit, 
-cat
+find, mv
 , rm%7D fr
@@ -1536,24 +1536,269 @@
 sList);%0A%7D;%0A%0A
+const renameFiles = () =%3E %7B%0A    echo(%22Renaming files from .jsx to .js%22);%0A%0A    find(src)%0A        .filter(file =%3E %7B%0A            return file.match(/%5C.jsx$/);%0A        %7D).map(file =%3E %7B%0A            mv(file, file.replace(%22jsx%22, %22js%22));%0A        %7D);%0A%7D;%0A%0A
 const applyT
@@ -3013,16 +3013,36 @@
 unLast%5D;
+%0A%0A    renameFiles();
 %0A    app

Rename files from js to jsx