lib/fsExt.js
function existsOrCreate(filePath, opts, cb){
    if(arguments.length===2){
        cb = arguments[1];
        opts = {};
    }
    opts.encoding = opts.encoding || 'utf8';
    opts.data = opts.data || opts.content || '';
    opts.mode = opts.mode || '0777';
    opts.replace = opts.replace ? true : false;
    
    var fp = resolve(filePath);
    var isFile = opts.hasOwnProperty('isFile') ? opts.isFile : !!fp.extName;
    
    fs.exists(fp.fullPath, function(exists){
        if(exists && !opts.replace) cb(null, exists);
        else {
            
            // file doesn't exists, create folder first
            mkdirp((isFile ? fp.dirName : fp.fullPath), opts.mode, function(err) {
                if (err) cb(new Error('fsExt.existsOrCreate: creating folders failed').cause(err));
                else if(isFile || opts.replace){
                    
                    // folders are created, so create file
                    fs.writeFile(fp.fullPath, opts.data, opts.encoding, function(err){
                        if (err) cb(new Error('fsExt.existsOrCreate: creating file failed').cause(err));
                        else cb(null, exists);
                    });
                }
                else cb(null, exists);
            });
        }
    });
}
function existsOrCreate(filePath, opts, cb){
    if(arguments.length===2){
        cb = arguments[1];
        opts = {};
    }
    opts.encoding = opts.encoding || 'utf8';
    opts.data = opts.data || opts.content || '';
    opts.mode = opts.mode || '0777';
    opts.replace = opts.replace ? true : false;
    
    var fp = resolve(filePath);
    var isFile = opts.hasOwnProperty('isFile') ? opts.isFile : !!fp.extName;
    
    fs.exists(fp.fullPath, function(exists){
        if(exists && !opts.replace) cb(null, exists);
        else {
            
            // file doesn't exists, create folder first
            mkdirp((isFile ? fp.dirName : fp.fullPath), opts.mode, function(err) {
                if (err) cb(new Error('fsExt.existsOrCreate: creating folders failed').cause(err));
                else if(isFile || opts.replace){
                    
                    // folders are created, so create file
                    fs.writeFile(fp.fullPath, opts.data, opts.encoding, function(err){
                        if (err) cb(new Error('fsExt.existsOrCreate: creating file failed').cause(err));
                        else cb(null, exists);
                    });
                }
                else cb(null, exists);
            });
        }
    });
}
function existsOrCreate ( filePath , opts , cb ) { if ( arguments . length === 2 ) { cb = arguments [ 1 ] ; opts = { } ; } opts . encoding = opts . encoding || 'utf8' ; opts . data = opts . data || opts . content || '' ; opts . mode = opts . mode || '0777' ; opts . replace = opts . replace ? true : false ; var fp = resolve ( filePath ) ; var isFile = opts . hasOwnProperty ( 'isFile' ) ? opts . isFile : ! ! fp . extName ; fs . exists ( fp . fullPath , function ( exists ) { if ( exists && ! opts . replace ) cb ( null , exists ) ; else { // file doesn't exists, create folder first mkdirp ( ( isFile ? fp . dirName : fp . fullPath ) , opts . mode , function ( err ) { if ( err ) cb ( new Error ( 'fsExt.existsOrCreate: creating folders failed' ) . cause ( err ) ) ; else if ( isFile || opts . replace ) { // folders are created, so create file fs . writeFile ( fp . fullPath , opts . data , opts . encoding , function ( err ) { if ( err ) cb ( new Error ( 'fsExt.existsOrCreate: creating file failed' ) . cause ( err ) ) ; else cb ( null , exists ) ; } ) ; } else cb ( null , exists ) ; } ) ; } } ) ; }
check if folder or file exists, if not create it
@param {String} filePath file path or array of paths to resolve
@param {Object} opts aditional options when creating file or directory (encoding, data, mode, replace, isFile) - use isFile when creating file with no extension, or folder with dot inside
@param {Function} cb callback(err,exists)
check if folder or file exists if not create it