用nodejs实现在ionic的hooks文件夹下对文件夹的粘贴和复制

在Ionic2中的hooks中放入自定义脚本来执行cordova命令。hooks目录之前是在.cordova/hooks该目录下,但现在已经搬到了ionic项目下,执行ionic hooks add 可以添加hooks目录。添加完目录了之后,其中的文件夹名称和命令执行顺序是一致的。以下是可以添加的文件夹目录:
after_build/
after_compile/
after_docs/
after_emulate/
after_platform_add/
after_platform_rm/
after_platform_ls/
after_plugin_add/
after_plugin_ls/
after_plugin_rm/
after_plugin_search/
after_prepare/
after_run/
after_serve/
before_build/
before_compile/
before_docs/
before_emulate/
before_platform_add/
before_platform_rm/
before_platform_ls/
before_plugin_add/
before_plugin_ls/
before_plugin_rm/
before_plugin_search/
before_prepare/
before_run/
before_serve/
pre_package/ <-- Windows 8 and Windows Phone only.

官方文档建议使用nodejs来写hooks脚本,以下是使用nodejs来实现文件夹拷贝:

var async = require("async");  
var fs = require("fs");  
var path = require("path");  
// cursively make dir   创建文件夹
function mkdirs(p, mode, f, made) {  
    if (typeof mode === 'function' || mode === undefined) {  
        f = mode;  
        mode = 0777 & (~process.umask());  
    }  
    if (!made)  
        made = null;  
  
    var cb = f || function () {};  
    if (typeof mode === 'string')  
        mode = parseInt(mode, 8);  
        p = path.resolve(p);  
  
    fs.mkdir(p, mode, function (er) {  
        if (!er) {  
            made = made || p;  
            return cb(null, made);  
        }  
        switch (er.code) {  
        case 'ENOENT':  
            mkdirs(path.dirname(p), mode, function (er, made) {  
                if (er) {  
                    cb(er, made);  
                } else {  
                    mkdirs(p, mode, cb, made);  
                }  
            });  
            break;  
  
            // In the case of any other error, just see if there's a dir  
            // there already.  If so, then hooray!  If not, then something  
            // is borked.  
        default:  
            fs.stat(p, function (er2, stat) {  
                // if the stat fails, then that's super weird.  
                // let the original error be the failure reason.  
                if (er2 || !stat.isDirectory()) {  
                    cb(er, made);  
                } else {  
                    cb(null, made)  
                };  
            });  
            break;  
        }  
    });  
}  
// 文件复制
function copyFile(file, toDir, cb) {  
    async.waterfall([  
            function (callback) {  
                fs.exists(toDir, function (exists) {  
                    if (exists) {  
                        callback(null, false);  
                    } else {  
                        callback(null, true);  
                    }  
                });  
            }, function (need, callback) {  
                if (need) {  
                    mkdirs(path.dirname(toDir), callback);  
                } else {  
                    callback(null,true);  
                }  
            }, function (p, callback) {  
                var reads = fs.createReadStream(file);  
                var writes = fs.createWriteStream(path.join(path.dirname(toDir), path.basename(file)));  
                reads.pipe(writes);  
                //don't forget close the  when  all the data are read  
                reads.on("end", function () {  
                    writes.end();  
                    callback(null);  
                });  
                reads.on("error", function (err) {  
                    console.log("error occur in reads");  
                    callback(true, err);  
                });  
  
            }  
        ], cb);  
  
}  
  
// 对需要copy的文件计数  
  
function _ccoutTask(from, to, cbw) {  
    async.waterfall([  
            function (callback) {  
                fs.stat(from, callback);  
            },  
            function (stats, callback) {  
                if (stats.isFile()) {  
                    cbw.addFile(from, to);  
                    callback(null, []);  
                } else if (stats.isDirectory()) {  
                    fs.readdir(from, callback);  
                }  
            },  
            function (files, callback) {  
                if (files.length) {  
                    for (var i = 0; i < files.length; i++) {  
                        _ccoutTask(path.join(from, files[i]), path.join(to, files[i]), cbw.increase());  
                    }  
                }  
                callback(null);  
            }  
        ], cbw);  
  
}  
// 计数回调之前
function ccoutTask(from, to, cb) {  
    var files = [];  
    var count = 1;  
  
    function wrapper(err) {  
        count--;  
        if (err || count <= 0) {  
            cb(err, files)  
        }  
    }  
    wrapper.increase = function () {  
        count++;  
        return wrapper;  
    }  
    wrapper.addFile = function (file, dir) {  
        files.push({  
            file : file,  
            dir : dir  
        });  
    }  
  
    _ccoutTask(from, to, wrapper);  
}  
  
  
function copyDir(from, to, cb) {  
    if(!cb){  
      cb=function(){};  
    }  
    //多个函数依次执行,且前一个的输出为后一个的输入
    async.waterfall([  
            function (callback) {  
                //判断路径是否存在
                fs.exists(from, function (exists) {  
                    if (exists) {  
                          //路径存在传给下一个函数
                        callback(null, true);  
                    } else {  
                           //路径不存在结束函数直接执行回调函数
                        console.log(from + " not exists");  
                        callback(true);  
                    }  
                });  
            },  
            function (exists, callback) {
                //获取文件信息 
                fs.stat(from, callback);  
            },  
            function (stats, callback) { 
                 //是否是文件
                if (stats.isFile()) {  
                    // 文件夹拷贝 
                    copyFile(from, to, function (err) {  
                        if (err) {  
                            // waterfall  中断
                            callback(true);  
                        } else {  
                            callback(null, []);  
                        }  
                    });  
                 //是否是目录
                } else if (stats.isDirectory()) {  
                    
                    ccoutTask(from, to, callback);  
                }  
            },  
            function (files,callback) {      
                // 文件总数限制            
                async.mapLimit(files, 10, function (f, cb) {  
                    copyFile(f.file, f.dir, cb);  
                }, callback);  
            }  
        ], cb);  
}  
  
var start = new Date().getTime();  
  
copyDir("F:\\gear", "F:\\gear2", function (err) {  
    if (err) {  
        console.log("error ocur");  
        console.dir(err);  
    } else {  
  
        console.log("copy ok");  
        console.log("consume time:" + (new Date().getTime() - start))  
    }  
});  

将此脚本放入before_prepare目录中: 此目录在ionic prepare 之前执行,也就是生成 www 目录之前。这个时候适合做代码压缩(gulp)、删除不必要文件(比如sass)。gulp 的部分就再也不用去手动执行 gulp build —release 了。

你可能感兴趣的:(用nodejs实现在ionic的hooks文件夹下对文件夹的粘贴和复制)