Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,56 @@ 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) {
this.options.push('-c', cmd.replace(/(\s)/g,'\ '));
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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -169,4 +178,3 @@ gs.prototype.pagecount = function(cb) {
return cb.call(self, null, data);
});
};

22 changes: 22 additions & 0 deletions tests/gs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down