
/**
 * 文件处理辅助类
 * */

var fs = require('fs');
var array = require('./array');

/**
 * 返回一条可用的文件路径
 * @param path          路径或路径数组
 * @param callback   回掉，如果不指定则为同步版本
 */
exports.validPath = function(path,callback){
    if(callback && typeof callback=='function'){
        valid_path(path,callback);
        return false;
    }else{
        return  valid_path_sync(path);
    }
};
function valid_path_sync(path){ //同步版本
    if(typeof path=='string'){
        var is = fs.existsSync(path);
        return is?path:null;
    }
    for(var k in path){
        if(fs.existsSync(path[k])){
            return path[k];
        }
    }
    return null;
}
function valid_path(path,callback){ //异步
    var str = typeof path=='string'
        , i = 0
        , leg = path.length;
    //查找函数
    function seek(path){
        fs.exists(path, function (exists) {
            if(exists){
                callback(path);
            }else{
                if(str){ //单独路径
                    callback(null);
                }else{
                    var other =  next();
                    if(other) seek(other);
                    else{
                        callback(null);
                    }
                }
            }
        });
    }
    //下一个路径
    function next(){
        var pat = null;
        if(i<leg) pat = path[i];
        i++;
        return pat;
    }
    //开始查找
    if(str){
        seek(path);
    }else{
        seek(next());
    }
}


/* *
 * 按文件名数组读取文件并按顺序合并
 * @nameAry 文件名数组
 * @callback 回调函数，参数(err,data)
 * @option.merger = true 是否合并文件
 * */
exports.readFileList = function(nameAry,merger,callback){
    if(!array.isArray(nameAry) || 0==nameAry.length){
        if(true==merger){
            callback(true,'');
        }else{
            callback(true,[]); //返回数组
        }
        return false;
    }
    if(typeof(merger)=='function'){
        callback = merger;
        merger = true; //是否合并
    }
    var leg = nameAry.length
        ,fileCon = []  //读取的单文件内容数组
        ,hasNum = 0; //已经读取的文件数量统计
    fileCon.length = leg; //保证与item的顺序相同
    for(var i=0;i<leg;i++){
        read(i);
    }
    //读取文件
    function read(k){
        fs.readFile(nameAry[k], 'utf8',function(err, data){
            if( err ) throw err;
            hasNum++;
            fileCon[k] = data;
            if(hasNum==leg){ //已经读完
                if(true==merger){
                    var content = '';
                    for(var j=0;j<leg;j++){ //合并文件
                        content += fileCon[j]
                    }
                    callback(err,content);
                }else{
                    //console.log(fileCon);
                    callback(err,fileCon); //返回数组
                }
            }
        });
    }
};

@@ -40,16 +40,45 @@
 ('fs');%0A
+var path = require('path');%0A%0A
 var arra
@@ -2745,20 +2745,739 @@
        %7D);%0A    %7D%0A%7D;%0A
+%0A%0A%0A%0A%0A/**%0A * %E5%88%9B%E5%BB%BA%E5%A4%9A%E5%B1%82%E6%96%87%E4%BB%B6%E5%A4%B9%EF%BC%88%E5%90%8C%E6%AD%A5%EF%BC%89%0A */%0Avar mkdirsSync = exports.mkdirsSync = function(dirpath, mode) %7B %0A    var exists = fs.existsSync(dirpath);%0A    if(exists)%7B%0A        return true%0A    %7D else %7B%0A        //%E5%B0%9D%E8%AF%95%E5%88%9B%E5%BB%BA%E7%88%B6%E7%9B%AE%E5%BD%95%EF%BC%8C%E7%84%B6%E5%90%8E%E5%86%8D%E5%88%9B%E5%BB%BA%E5%BD%93%E5%89%8D%E7%9B%AE%E5%BD%95%0A        mkdirsSync(path.dirname(dirpath), mode);%0A        return fs.mkdirSync(dirpath, mode);%0A    %7D%0A%7D%0A%0A%0A%0A/**%0A * %E5%88%9B%E5%BB%BA%E5%A4%9A%E5%B1%82%E6%96%87%E4%BB%B6%E5%A4%B9%EF%BC%88%E5%BC%82%E6%AD%A5%EF%BC%89%0A */%0Avar mkdirs = exports.mkdirs = function(dirpath, mode, callback) %7B%0A    fs.exists(dirpath, function(exists) %7B%0A        if(exists) %7B%0A                callback(dirpath);%0A        %7D else %7B%0A                //%E5%B0%9D%E8%AF%95%E5%88%9B%E5%BB%BA%E7%88%B6%E7%9B%AE%E5%BD%95%EF%BC%8C%E7%84%B6%E5%90%8E%E5%86%8D%E5%88%9B%E5%BB%BA%E5%BD%93%E5%89%8D%E7%9B%AE%E5%BD%95%0A                mkdirs(path.dirname(dirpath), mode, function()%7B%0A                        fs.mkdir(dirpath, mode, callback);%0A                %7D);%0A        %7D%0A    %7D);%0A%7D;%0A

Update file.js