-
-
Save briancavalier/5031757 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var when = require('when'); | |
var fs = require('fs'); | |
var concat = {}; | |
concat.each = function(src, dest, options) { | |
if (!options) { options = {}; } | |
if (!options.separator) { options.separator = ''; } | |
return when.reduce(src, function(output, srcpath) { | |
var dfd = when.defer(); | |
fs.readFile(srcpath, function(err, data) { | |
if (err) { dfd.reject(err); } | |
dfd.resolve(output.concat(data)); | |
}); | |
return dfd; | |
}, []).then(function(combined) { | |
var dfd = when.defer(); | |
fs.writeFile(dest, combined.join(options.separator), function(err) { | |
if (err) { dfd.reject(err); } | |
dfd.resolve('File written.'); | |
}); | |
return dfd; | |
}); | |
}; | |
concat.each( | |
['test/fixtures/concat/a.txt', 'test/fixtures/concat/b.txt', 'test/fixtures/concat/c.txt'], | |
'tmp/abc.txt', | |
{separator: '\n'} | |
).then(function(status) { | |
console.log(status); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var when = require('when'); | |
var nodefn = require('when/node/function'); | |
var fs = require('fs'); | |
var readFile = nodefn.bind(require('fs').readFile); | |
var writeFile = nodefn.bind(require('fs').writeFile); | |
var concat = {}; | |
concat.each = function(src, dest, options) { | |
if (!options) { options = {}; } | |
if (!options.separator) { options.separator = ''; } | |
// Yeah, this is everything | |
// Map each filename to its contents | |
// Join the contents synchronously | |
// write the result | |
return when.map(src, readFile).then(function(array) { | |
return array.join(options.separator); | |
}).then(writeFile.bind(fs, dest)); | |
}; | |
concat.each( | |
['test/fixtures/concat/a.txt', 'test/fixtures/concat/b.txt', 'test/fixtures/concat/c.txt'], | |
'tmp/abc.txt', | |
{separator: '\n'} | |
).then(function(status) { | |
console.log(status); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var when = require('when'); | |
var nodefn = require('when/node/function'); | |
var fs = require('fs'); | |
var readFile = nodefn.bind(require('fs').readFile); | |
var writeFile = nodefn.bind(require('fs').writeFile); | |
var concat = {}; | |
concat.each = function(src, dest, options) { | |
if (!options) { options = {}; } | |
if (!options.separator) { options.separator = ''; } | |
// Map each filename to its contents | |
// Reduce the resulting promised array of contents | |
// (Note: when.map and when.reduce are composable) | |
// write the result | |
return when.reduce(src, function(result, file) { | |
return readFile(file).then(function(contents) { | |
return result + contents; | |
}); | |
}, '').then(writeFile.bind(fs, dest)); | |
}; | |
concat.each( | |
['test/fixtures/concat/a.txt', 'test/fixtures/concat/b.txt', 'test/fixtures/concat/c.txt'], | |
'tmp/abc.txt', | |
{separator: '\n'} | |
).then(function(status) { | |
console.log(status); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment