/**
 * Created by chendong on 2017/8/3.
 */

var fs = require('fs');
var path = require('path');


function TeldMoveFileWebpackPlugin() {
}

function yellow(str) {
    console.log("\u001b[1m\u001b[33m"+str+"\u001b[39m\u001b[22m");
}
function green(str) {
    console.log("\u001b[1m\u001b[32m" +str + "\u001b[39m\u001b[22m");
}

TeldMoveFileWebpackPlugin.prototype.apply=function(compiler) {

    var ROOT_PATH = path.resolve("");

    compiler.plugin("done", function(compilation, callback) {
        green("------copy files to dir [plugin]");
        if(!fs.existsSync(path.join(ROOT_PATH, '/plugin'))){
            fs.mkdirSync(path.join(ROOT_PATH, '/plugin'))
        }
        fs.writeFileSync(path.join(ROOT_PATH, '/plugin/plugin.config.js'), fs.readFileSync(path.join(ROOT_PATH, '/plugin.config.js')));
        fs.writeFileSync(path.join(ROOT_PATH, '/plugin/teld-helper-list-17.js'), fs.readFileSync(path.join(ROOT_PATH, '/teld-helper-list-17.js')));

        var fileUrl = path.join(ROOT_PATH, 'dist/js')
        var files = fs.readdirSync(fileUrl);//读取该文件夹

        files.forEach(function(file){
            if(file !="frame.js" && file != "shared.js")
            //fs.linkSync(fileUrl+'/'+file,path.join(ROOT_PATH, '/plugin/'+file));
            fs.writeFileSync(path.join(ROOT_PATH, '/plugin/'+file),fs.readFileSync(fileUrl+'/'+file));
        });


        green("------end copy!!!")
        //callback();
    });



}



module.exports = TeldMoveFileWebpackPlugin;