Skip to content

Commit

Permalink
#4 introducing options for each command
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Jun 18, 2013
1 parent 307d527 commit 1fe6951
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
var im = require('imagemagick'),
path = require("path"),
fs = require("fs"),
options = require("./options");
defaults = require("./options");

var Main = function(){

}

Main.prototype = {

queue: [],

icon: function( source ){

icon: function( source, options ){
// merge with defaults
options = extend( defaults, ( options || {} ) );
// load sizes from options
var sizes = options.icons;
// get (or create) the destination dir
Expand All @@ -26,7 +28,9 @@ Main.prototype = {
this.process();
},

splash: function( source ){
splash: function( source, options ){
// merge with defaults
options = extend( defaults, ( options || {} ) );
// load sizes from options
var sizes = options.splash;
// get (or create) the destination dir
Expand All @@ -41,9 +45,9 @@ Main.prototype = {
this.process();
},

process: function(){
process: function(){
var self = this;
// get the next item from the queue
// get the next item from the queue
var img = this.queue.pop();
im.crop({
srcPath: img.src,
Expand All @@ -54,18 +58,18 @@ Main.prototype = {
if (err) throw err
console.log("- created image: "+ img.dest)
// process the next image
if( self.queue.length ){
if( self.queue.length ){
self.process();
} else {
console.log("!!! All images created");
}
});

}
}

// Helpers
function dir( path ){
function dir( path ){
return path.substring(0,path.lastIndexOf("/")+1);
}

Expand All @@ -78,7 +82,21 @@ function check( dir ){
var exists = fs.existsSync( path );
if(!exists) fs.mkdirSync( path );
}


}

// Deep extend:
// http://andrewdupont.net/2009/08/28/deep-extending-objects-in-javascript/
function extend(destination, source) {
for (var property in source) {
if (source[property] && source[property].constructor && source[property].constructor === Object) {
destination[property] = destination[property] || {};
arguments.callee(destination[property], source[property]);
} else {
destination[property] = source[property];
}
}
return destination;
}

module.exports = new Main();

0 comments on commit 1fe6951

Please sign in to comment.