From 7cd0983b95de2e6204e5432db750d698cfee16f2 Mon Sep 17 00:00:00 2001 From: Leonid Suprun Date: Wed, 27 Jul 2016 16:55:33 +0300 Subject: [PATCH] Specify initialization files path --- index.js | 58 ++++++++++++++++++++++++++++++----------------------- tests/gs.js | 22 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index 27db417..b76f069 100644 --- a/index.js +++ b/index.js @@ -3,46 +3,40 @@ var spawn = require('child_process').spawn, module.exports = exports = create; -function create() { - var _gs = new gs(); +function create(inputFile) { + var _gs = new gs(inputFile); return _gs; } -function gs() { +function gs(inputFile) { this.options = []; - this._input = null; + this._input = inputFile; } gs.prototype.__proto__ = EventEmitter.prototype; gs.prototype.batch = function() { - this.options.push('-dBATCH'); - return this; + return this.define('BATCH'); }; gs.prototype.diskfonts = function() { - this.options.push('-dDISKFONTS'); - return this; + return this.define('DISKFONTS'); }; gs.prototype.nobind = function() { - this.options.push('-dNOBIND'); - return this; + return this.define('NOBIND'); }; gs.prototype.nocache = function() { - this.options.push('-dNOCACHE'); - return this; + return this.define('NOCACHE'); }; gs.prototype.nodisplay = function() { - this.options.push('-dNODISPLAY'); - return this; + return this.define('NODISPLAY'); }; gs.prototype.nopause = function() { - this.options.push('-dNOPAUSE'); - return this; + return this.define('NOPAUSE'); }; gs.prototype.command = function(cmd) { @@ -50,15 +44,15 @@ gs.prototype.command = function(cmd) { return this; }; -gs.prototype.define = function(key, val) { - this.options.push('-d' + key + (val ? '=' + val : '')); +gs.prototype.define = function(key, val, mod) { + mod = mod || 'd'; + this.options.push('-' + mod + key + (val ? '=' + val : '')); return this; }; gs.prototype.device = function(dev) { dev = dev || 'txtwrite'; - this.options.push('-sDEVICE=' + dev); - return this; + return this.define('DEVICE', dev, 's'); }; gs.prototype.input = function(file) { @@ -73,6 +67,19 @@ gs.prototype.output = function(file) { return this; }; +gs.prototype.include = function(path) { + if (path == undefined) { + throw new Error('Include path is not specified'); + } + + if (Array.isArray(path)) { + path = path.join(':'); + } + + this.options = this.options.concat(['-I', path]); + return this; +} + gs.prototype.quiet = function() { this.options.push('-q'); return this; @@ -88,8 +95,7 @@ gs.prototype.currentDirectory = function() { gs.prototype.p = gs.prototype.currentDirectory; gs.prototype.papersize = function(size) { - this.options.push('-sPAPERSIZE=' + size); - return this; + return this.define('PAPERSIZE', size, 's'); }; gs.prototype.resolution = function(xres, yres) { @@ -106,8 +112,7 @@ gs.prototype.reset = function() { }; gs.prototype.safer = function() { - this.options.push('-dSAFER'); - return this; + return this.define('SAFER'); }; gs.prototype.exec = function(cb) { @@ -121,6 +126,10 @@ gs.prototype.exec = function(cb) { var _data = []; var totalBytes = 0; + proc.stderr.on('data', function(data) { + cb(data.toString()); + }); + proc.stdout.on('data', function(data) { totalBytes += data.length; _data.push(data); @@ -169,4 +178,3 @@ gs.prototype.pagecount = function(cb) { return cb.call(self, null, data); }); }; - diff --git a/tests/gs.js b/tests/gs.js index 23b314e..63a08d4 100644 --- a/tests/gs.js +++ b/tests/gs.js @@ -17,6 +17,28 @@ describe('gs', function() { }); }); + describe('#include', function() { + it('should raise an error for null include path', function(done) { + assert.throws(function() { gs().include(); }, Error); + done(); + }); + + it('should set include option', function(done) { + const input = '/opt/my app/Init'; + const good = ['-I', '/opt/my app/Init']; + + assert.deepEqual(gs().include(input).options, good); + done(); + }); + it('should set array of include paths', function(done) { + const input = ['/opt/my app/Init', '/tmp', '.']; + const good = ['-I', '/opt/my app/Init:/tmp:.']; + + assert.deepEqual(gs().include(input).options, good); + done(); + }); + }); + describe('#device', function() { it('should set device option with default', function(done) { assert.deepEqual(gs().device().options, ['-sDEVICE=txtwrite']);