Skip to content

Instantly share code, notes, and snippets.

@Urbiwanus
Created February 23, 2015 22:36
Show Gist options
  • Save Urbiwanus/1594a66a746e60b7b906 to your computer and use it in GitHub Desktop.
Save Urbiwanus/1594a66a746e60b7b906 to your computer and use it in GitHub Desktop.
Cordova-App-loader Grunt
Usage :
jsonmanifest: {
generate: {
options: {
basePath: '../www',
exclude: [],
//load all found assets
loadall: true,
//manually add files to the manifest
files: {},
//manually define the files that should be injected into the page
load: [],
// root location of files to be loaded in the load array.
root: "./",
order: [
'*.css',
'*.html',
'lib/vendor-lib.js'
]
},
src: [
'app/*.js',
'lib/*.js',
'css/*.css',
'templates/*.html'
],
dest: ['manifest.json']
}
}
//Code
grunt.registerMultiTask('jsonmanifest', 'Generate JSON Manifest for Hot Updates', function () {
var options = this.options({loadall: true, root: "./", files: {}, load: []});
var done = this.async();
var path = require('path');
this.files.forEach(function (file) {
var files;
//manifest format
var json = {
"files": options.files,
"load": options.load,
"root": options.root
};
//clear load array if loading all found assets
if (options.loadall) {
json.load = [];
}
// check to see if src has been set
if (typeof file.src === "undefined") {
grunt.fatal('Need to specify which files to include in the json manifest.', 2);
}
// if a basePath is set, expand using the original file pattern
if (options.basePath) {
files = grunt.file.expand({cwd: options.basePath}, file.orig.src);
} else {
files = file.src;
}
// Exclude files
if (options.exclude) {
files = files.filter(function (item) {
return options.exclude.indexOf(item) === -1;
});
}
// Set default destination file
if (!file.dest) {
file.dest = ['manifest.json'];
}
// add files
if (files) {
files.forEach(function (item) {
var hasher = require('crypto').createHash('sha256');
var filename = encodeURI(item);
var key = filename.split("/");
key = key[key.length - 1];
json.files[key] = {}
json.files[key]['filename'] = filename;
json.files[key]['version'] = hasher.update(grunt.file.read(path.join(options.basePath, item))).digest("hex")
if (options.loadall) {
json.load.push(filename);
}
});
//change order due
var loadOrdered = [];
if (options.order) {
grunt.log.writeln('Order items');
options.order.forEach(function (orderItem) {
pattern = orderItem.replace('*', '.*').replace(/\./g, '\\.');
var regex = new RegExp(pattern, 'g');
grunt.log.writeln('Sorting ' + orderItem + ' Pattern ' + regex);
json.load.forEach(function (loadItem) {
grunt.log.writeln('Comparing ' + pattern, ' with ' + loadItem);
if (loadItem.match(regex)) {
grunt.log.writeln(orderItem, ' matches ', loadItem);
loadOrdered.push(loadItem);
}
});
});
json.load.forEach(function (item) {
if (loadOrdered.indexOf(item) <= -1) {
loadOrdered.push(item);
}
});
json.load = loadOrdered;
}
}
//write out the JSON to the manifest files
file.dest.forEach(function (f) {
grunt.file.write(f, JSON.stringify(json, null, 2));
});
done();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment