Code coverage report for src/extract.js

Statements: 10.34% (3 / 29)      Branches: 0% (0 / 16)      Functions: 0% (0 / 9)      Lines: 12% (3 / 25)      Ignored: none     

All files » src/ » extract.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63      1                 1                                                                       1                            
// vim: set shiftwidth=2 expandtab:
'use strict';
 
var spawn      = require('child_process').spawn
  , fs         = require('fs')
  , async      = require('async')
  , path       = require('path')
 
  , styledocco = require('./styledocco')
  , helper     = require('./helper')
  ;
 
var preprocess = function(file, pp, options, cb) {
  var stdout = '';
  // stdin would have been nice here, but not all preprocessors (less)
  // accepts that, so we need to read the file both here and for the parser.
  // Don't process SASS partials.
  if (file.match(/(^|\/)_.*\.s(c|a)ss$/) != null) {
    process.nextTick(function() { cb(null, ''); });
  } else if (pp != null) {
    pp += ' ' + file;
    pp = pp.split(' ');
 
    pp = spawn(pp.shift(), pp);
 
    pp.stderr.setEncoding('utf8');
    pp.stdout.setEncoding('utf8');
 
    pp.on('error', function(err) {
      if (err != null && options.verbose) console.error(err.message);
    });
 
    pp.on('close', function() {
      cb(null, stdout);
    });
 
    pp.stderr.on('data', function(data) {
      if (data.length && options.verbose) console.error(data);
    });
 
    pp.stdout.on('data', function(data) {
      stdout += data;
    });
  } else {
    fs.readFile(file, 'utf8', cb);
  }
};
 
module.exports = function (options, file) {
  return {
    css: async.apply(preprocess
                   , file
                   , options.preprocessor || helper.fileTypes[path.extname(file)]
                   , options),
    docs: function(cb) {
      fs.readFile(file, 'utf8', function(err, code) {
        if (err != null) return cb(err);
        cb(null, styledocco(code));
      });
    }
  }
}