UNPKG

docparse-dumpinvoices

Version:

takes a list of couchdb invoice ids and exports the data contained within those invoices to an Excel spreadsheet

87 lines (79 loc) 2.33 kB
/** * Takes a stream of invoice objects and writes them to the excel spreadsheet */ var moment = require('moment'); var mime = require('mime'); var Stream = require('stream').Stream; var spawn = require('child_process').spawn; var inspect = require('eyespect').inspector(); var path = require('path'); var getTemplatePath = require('./getTemplatePath'); module.exports = function(feed, res, cb) { var filename = 'output-'+moment().format('YYYY MM DD hh-mm-ss')+'.xls'; var mimetype = mime.lookup('.xls'); if (res.setHeader) { res.setHeader('Content-disposition', 'attachment; filename=' + filename); res.setHeader('Content-type', mimetype); } var message; var template_path = getTemplatePath(); var script_path = path.join(__dirname,'process.py'); // feed.on('change', function (doc) { // inspect(doc, 'got doc') // }) var child = spawn('python', [script_path, template_path]); var format = new ArrayFormatter; var formatted = feed.pipe(format) formatted.pipe(child.stdin); child.stdout.pipe(res); // nothing should write to stderr child.stderr.setEncoding('utf8') var stdErr = '' child.stderr.on('data', function(data) { console.log(data) stdErr += data if (/^execvp\(\)/.test(data)) { inspect('Failed to start child process.', data); } }) // prompt the user to download the spreadsheet var now = moment(); child.on('close', function() { if (stdErr) { return cb({ message: 'error dumping invoices to spreadsheet', error: stdErr, stack: new Error().stack }) } now = null child = null template_path = null script_path = null mimetype = null res.end(); res = null formatted = null format = null return cb(); }); }; function ArrayFormatter () { Stream.call(this); this.writable = true; this._done = false; } ArrayFormatter.prototype.__proto__ = Stream.prototype; ArrayFormatter.prototype.write = function (doc) { var text = JSON.stringify(doc).replace(/\\n/g,''); var output = text + '\n' this.emit('data', output) return true; }; ArrayFormatter.prototype.end = ArrayFormatter.prototype.destroy = function () { if (this._done) return; this._done = true; // emit the end event to close the python process this.emit('end'); };