Last active
          December 14, 2015 04:59 
        
      - 
      
- 
        Save cowboy/5032214 to your computer and use it in GitHub Desktop. 
    node-task idea: concat
  
        
  
    
      This file contains hidden or 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 _ = require('lodash'); | |
| var nodefn = require('when/node/function'); | |
| var readFile = nodefn.bind(fs.readFile); | |
| var writeFile = nodefn.bind(fs.writeFile); | |
| // Listen for events here. | |
| var concat = new (require('events').EventEmitter); | |
| // Internal helper for getting options. | |
| var normalizeOptions = function(o) { | |
| concat.emit('debug', 'options', o); | |
| return _.defaults({}, o, { | |
| separator: '', | |
| banner: '', | |
| footer: '', | |
| }); | |
| }; | |
| // Concat one file. | |
| concat.each = function(src, dest, o) { | |
| o = normalizeOptions(o); | |
| concat.emit('debug', 'src', src); | |
| concat.emit('debug', 'dest', dest); | |
| return when.map(src, readFile).then(function(contents) { | |
| return o.banner + contents.join(o.separator) + o.footer; | |
| }).then(writeFile.bind(fs, dest)); | |
| }; | |
| // Concat many files. | |
| concat.all = function(files, o) { | |
| o = normalizeOptions(o); | |
| return when.map(files, function(f) { | |
| return concat.each(f.src, f.dest, o); | |
| }); | |
| }; | |
| // Standard "node task" interface. | |
| concat.run = function(o) { | |
| if (!o) { o = {}; } | |
| return concat.all(o.files || [], o); | |
| }; | |
| module.exports = concat; | 
  
    
      This file contains hidden or 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
    
  
  
    
  | concat.on('status', console.log.bind(console, 'status')); | |
| concat.on('warn', console.log.bind(console, 'warn')); | |
| concat.on('debug', console.log.bind(console, 'debug')); | |
| concat.each( | |
| ['test/fixtures/concat/a.txt', 'test/fixtures/concat/b.txt', 'test/fixtures/concat/c.txt'], | |
| 'tmp/each-abc.txt', | |
| {separator: ';each;', banner: '/* EACH */\n', footer: '\n\n'} | |
| ).then(console.log, console.error) | |
| concat.all( | |
| [ | |
| {src: ['test/fixtures/concat/a.txt', 'test/fixtures/concat/b.txt', 'test/fixtures/concat/c.txt'], dest: 'tmp/all-abc.txt'}, | |
| {src: ['test/fixtures/concat/d.txt', 'test/fixtures/concat/e.txt', 'test/fixtures/concat/f.txt'], dest: 'tmp/all-def.txt'}, | |
| {src: ['test/fixtures/concat/g.txt', 'test/fixtures/concat/h.txt', 'test/fixtures/concat/i.txt'], dest: 'tmp/all-ghi.txt'}, | |
| ], | |
| {separator: ';all;', banner: '/* ALL */\n', footer: '\n\n'} | |
| ).then(console.log, console.error); | |
| concat.run({ | |
| files: [ | |
| {src: ['test/fixtures/concat/a.txt', 'test/fixtures/concat/b.txt', 'test/fixtures/concat/c.txt'], dest: 'tmp/run-abc.txt'}, | |
| {src: ['test/fixtures/concat/d.txt', 'test/fixtures/concat/e.txt', 'test/fixtures/concat/f.txt'], dest: 'tmp/run-def.txt'}, | |
| {src: ['test/fixtures/concat/g.txt', 'test/fixtures/concat/h.txt', 'test/fixtures/concat/i.txt'], dest: 'tmp/run-ghi.txt'}, | |
| ], | |
| separator: ';run;', | |
| banner: '/* RUN */\n', | |
| footer: '\n\n', | |
| }).then(console.log, console.error); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment