lib/sf.js
function transfer(xmlFiles, output, dir, needAppend, _callback){
  /**
   * If needAppend is true, output must be a file but not a directory
   */
  Step(
    function(){
      if(typeof output === 'undefined'){
        output = path.dirname(xmlFiles[0]);
      }
      output = output.indexOf(path.sep) === 0 ? output : path.join(process.cwd(), output);
      fs.stat(output, this); 
    },

    function(err, stat){
      if(err){
        throw err;
      }

      if(needAppend){
        if(!stat.isFile()){
          throw new Error('Output must be a file but not a directory when --append is enabled');
        }
      }else{
        if(!stat.isDirectory()){
          throw new Error('Output must be a directory');
        }
        output = path.join(output, 'nproxy-rule.js');
      }
      
      return output;
    },

    function transferBatch(err, outputFile){
      var group;

      if(err){
        throw err;
      }

      group = this.group();

      xmlFiles.forEach(function(xmlFile){
        transferSingleFile(xmlFile, dir, group());
      });
    },

    function append(err, rules){
      var wrapperTpl;
      var result;
      var isTargetEmpty = true;

      if(err){
        throw err;
      }

      if(needAppend){
        wrapperTpl = fs.readFileSync(output, 'utf-8');
        isTargetEmpty = false;
      }else{
        wrapperTpl = [
          'module.exports = [/*{{more}}*/',
          '];'
        ].join(EOL);
      }

      return _append(wrapperTpl, rules, isTargetEmpty);
    },

    function writeOutput(err, content){
      if(err){
        throw err;
      }

      fs.writeFile(output, 
        content,
        'utf-8', function(err){
          if(err){ throw err};
          log.info('Write output file successfully!');
          if(typeof _callback === 'function'){
            _callback(null);
          }
      });
    }
  );

}
function transfer(xmlFiles, output, dir, needAppend, _callback){
  /**
   * If needAppend is true, output must be a file but not a directory
   */
  Step(
    function(){
      if(typeof output === 'undefined'){
        output = path.dirname(xmlFiles[0]);
      }
      output = output.indexOf(path.sep) === 0 ? output : path.join(process.cwd(), output);
      fs.stat(output, this); 
    },

    function(err, stat){
      if(err){
        throw err;
      }

      if(needAppend){
        if(!stat.isFile()){
          throw new Error('Output must be a file but not a directory when --append is enabled');
        }
      }else{
        if(!stat.isDirectory()){
          throw new Error('Output must be a directory');
        }
        output = path.join(output, 'nproxy-rule.js');
      }
      
      return output;
    },

    function transferBatch(err, outputFile){
      var group;

      if(err){
        throw err;
      }

      group = this.group();

      xmlFiles.forEach(function(xmlFile){
        transferSingleFile(xmlFile, dir, group());
      });
    },

    function append(err, rules){
      var wrapperTpl;
      var result;
      var isTargetEmpty = true;

      if(err){
        throw err;
      }

      if(needAppend){
        wrapperTpl = fs.readFileSync(output, 'utf-8');
        isTargetEmpty = false;
      }else{
        wrapperTpl = [
          'module.exports = [/*{{more}}*/',
          '];'
        ].join(EOL);
      }

      return _append(wrapperTpl, rules, isTargetEmpty);
    },

    function writeOutput(err, content){
      if(err){
        throw err;
      }

      fs.writeFile(output, 
        content,
        'utf-8', function(err){
          if(err){ throw err};
          log.info('Write output file successfully!');
          if(typeof _callback === 'function'){
            _callback(null);
          }
      });
    }
  );

}
function transfer ( xmlFiles , output , dir , needAppend , _callback ) { /**
   * If needAppend is true, output must be a file but not a directory
   */ Step ( function ( ) { if ( typeof output === 'undefined' ) { output = path . dirname ( xmlFiles [ 0 ] ) ; } output = output . indexOf ( path . sep ) === 0 ? output : path . join ( process . cwd ( ) , output ) ; fs . stat ( output , this ) ; } , function ( err , stat ) { if ( err ) { throw err ; } if ( needAppend ) { if ( ! stat . isFile ( ) ) { throw new Error ( 'Output must be a file but not a directory when --append is enabled' ) ; } } else { if ( ! stat . isDirectory ( ) ) { throw new Error ( 'Output must be a directory' ) ; } output = path . join ( output , 'nproxy-rule.js' ) ; } return output ; } , function transferBatch ( err , outputFile ) { var group ; if ( err ) { throw err ; } group = this . group ( ) ; xmlFiles . forEach ( function ( xmlFile ) { transferSingleFile ( xmlFile , dir , group ( ) ) ; } ) ; } , function append ( err , rules ) { var wrapperTpl ; var result ; var isTargetEmpty = true ; if ( err ) { throw err ; } if ( needAppend ) { wrapperTpl = fs . readFileSync ( output , 'utf-8' ) ; isTargetEmpty = false ; } else { wrapperTpl = [ 'module.exports = [/*{{more}}*/' , '];' ] . join ( EOL ) ; } return _append ( wrapperTpl , rules , isTargetEmpty ) ; } , function writeOutput ( err , content ) { if ( err ) { throw err ; } fs . writeFile ( output , content , 'utf-8' , function ( err ) { if ( err ) { throw err } ; log . info ( 'Write output file successfully!' ) ; if ( typeof _callback === 'function' ) { _callback ( null ) ; } } ) ; } ) ; }
Transfer sf rules in inputs to nproxy rule file

@param {Array} xmlFiles xml files
@param {String} output output dir or file when append is true
@param {String} dir dir in the rule files
@param {Boolean} needAppend if true, the transfered rule will be appended
Transfer sf rules in inputs to nproxy rule file