title: NodeJS concatenate all files in a directory
body: <p>Is there a faster or more succinct way to concatenate all of the files located in a directory using NodeJS?</p>
  
  <p>In bash I could do something like this:</p>
  
  <pre><code>for file in $1
  do
    cat "$file"
    echo
  done &gt; $2;
  </code></pre>
  
  <p>Here is what I'm doing now:</p>
  
  <pre><code>var fs = require('fs');
  var Promise = require('bluebird');
  
  module.exports = function(directory, destination) {
      return new Promise((resolve, reject) =&gt; {
          fs.readdir(directory, (err, files) =&gt; {
              if (err) {
                  return reject(err);
              }
  
              (function next() {
                  var file = files.shift();
                  if (!file) {
                      return resolve();
                  }
  
                  fs.readFile(directory + '/' + file, (err, content) =&gt; {
                      if (err) {
                          return reject(err);
                      }
  
                      fs.appendFile(destination, '\n' + content, (err) =&gt; {
                          if (err) {
                              return reject(err);
                          }
  
                          return next();
                      });
                  });
              })();
          });
      });
  };
  </code></pre>