Skip to content

Instantly share code, notes, and snippets.

@inkless
Last active December 15, 2015 12:39
Show Gist options
  • Save inkless/5261445 to your computer and use it in GitHub Desktop.
Save inkless/5261445 to your computer and use it in GitHub Desktop.
Grunt related files
// Build msdo.js to the dist directory. Embed date@version.
grunt.registerMultiTask(
"build",
"Concatenate source and build msdo.js to the dist directory. Embed date@version.",
function() {
// Concat specified files.
var compiled = "",
name = this.data.dest,
src = this.data.src,
version = grunt.config("pkg.version");
// append commit id to version
if (process.env.COMMIT) {
version += " " + process.env.COMMIT;
}
// conditionally concatenate source
src.forEach(function(filepath) {
compiled += grunt.file.read(filepath);
});
// Embed Version
// Embed Date
compiled = compiled.replace(/@VERSION/g, version)
.replace("@DATE", function() {
var date = new Date();
// YYYY-MM-DD
return [
date.getFullYear(),
date.getMonth() + 1,
date.getDate()].join("-");
});
// Write concatenated source to file
grunt.file.write(name, compiled);
// Fail task if errors were logged.
if (this.errorCount) {
return false;
}
// Otherwise, print a success message.
grunt.log.writeln("File '" + name + "' created.");
}
);
var fs = require('fs');
var path = require('path');
/** Quick Proxy methods **/
var proxy = function(f, c) {
var fn = f;
var cn = c;
return function() {
fn.apply(cn, Array.prototype.slice.call(arguments));
};
};
// True if the file path exists.
var fileExists = function() {
var filepath = path.join.apply(path, arguments);
return fs.existsSync(filepath);
};
var HisrcCommand = {
path: '',
name: '',
ext: '',
file: '',
callback: undefined,
context: undefined,
im: undefined,
init: function(fpath, fsuffix, fcallback, context) {
try {
this.file = fpath;
this.suffix = fsuffix;
var lslash = fpath.lastIndexOf('/');
var ldot = fpath.lastIndexOf('.');
this.path = fpath.substr(0, lslash + 1);
this.ext = fpath.substr(ldot + 1);
this.name = fpath.substr(lslash + 1, ldot - lslash - 1);
this.im = require('imagemagick');
this.callback = fcallback;
debugger;
this.im.identify(['-format', '%wx%h', this.file], proxy(this.resize, this));
} catch (e) {
grunt.log.write('error ' + e + "\n");
}
},
resize: function(err, output) {
try {
grunt.log.write("Resizing image: " + output + "\n");
this.baseWidth = Number(output.split('x')[0]);
this.baseHeight = Number(output.split('x')[1]);
grunt.log.write("To size: " + Math.round(this.baseWidth / 2) + "x" + Math.round(this.baseHeight / 2) + "\n");
this.im.resize({
width: Math.round(this.baseWidth / 2),
height: Math.round(this.baseHeight / 2),
srcPath: this.file,
dstPath: this.path + this.name.split(this.suffix[0]).join(this.suffix[1]) + '.' + this.ext
}, proxy(this.resize2, this));
} catch (e) {
grunt.log.write('error ' + e + "\n");
}
},
resize2: function(err) {
try {
grunt.log.write("Resizing lowrez\n");
grunt.log.write("To size: " + Math.round(this.baseWidth / 4) + "x" + Math.round(this.baseHeight / 4) + "\n");
this.im.resize({
width: Math.round(this.baseWidth / 4),
height: Math.round(this.baseHeight / 4),
srcPath: this.file,
dstPath: this.path + this.name.split(this.suffix[0]).join(this.suffix[2]) + '.' + this.ext
}, proxy(this.complete, this));
} catch (e) {
grunt.log.write('error ' + e + "\n");
}
},
complete: function() {
grunt.log.write(" - created responsive for " + this.path + this.name + "." + this.ext + "\n");
this.callback.apply(this.context, [this, true]);
},
};
grunt.registerMultiTask('imagemagick-hisrc', 'Performs a number of configured tasks', function() {
var done = this.async();
grunt.log.write("Beginning ImageMagick processing for HiSRC\n");
var fls = grunt.file.expand(this.data.files);
var i = 0;
var count = fls.length;
var suffix = this.data.suffix;
var cmds = [];
var cmd;
grunt.log.write("CWD:" + process.cwd() + "\n-files:" + fls.length + "\n-pattern:" + this.data.files + "\n");
function onCmdComplete(cmd, success) {
grunt.log.write("completed : " + cmd.file + "\n");
cmds.splice(cmds.indexOf(cmd), 1);
if (cmds.length < 1) {
done();
}
}
for (i = 0; i < fls.length; i++) {
cmd = Object.create(HisrcCommand);
cmds.push(cmd);
cmd.init(fls[i], suffix, onCmdComplete, this);
}
if (fls.length < 1) {
grunt.log.write("Nothing to do\n");
done();
} else {
grunt.log.write("all queued\n");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment