Skip to content

Instantly share code, notes, and snippets.

@lylepratt
Last active August 4, 2016 18:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save lylepratt/d8bf84b3b7d6932e3549 to your computer and use it in GitHub Desktop.
Save lylepratt/d8bf84b3b7d6932e3549 to your computer and use it in GitHub Desktop.
Grunt Task to Generate Manifest for cordova-app-loader
/* This generates a manifest file for use with the cordova-app-loader tool:
* https://github.com/markmarijnissen/cordova-app-loader
*/
/*
* You can add settings to your grunt initConfig
//jsonmanifest settings
jsonmanifest: {
generate: {
options: {
basePath: 'where/you/files/live/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: "./"
},
src: [
'js/*.js',
'css/*.css'
],
dest: 'manifest.json'
}
}
*/
/*
* Example Output of task
{
"files": {
"bv-cordova.js": {
"filename": "js/089775a3-bv-cordova.js",
"version": "98af580a8bc88ee0ca248a99c975f29a55d8b99324c151a347afef2994327699"
},
"vendor.js": {
"filename": "js/7230ca97-vendor.js",
"version": "f3e66d9576c6f9e0bbd262e303a1fffa32812aa298f1a104cbb4fc9dacb2a38f"
},
"bv.js": {
"filename": "js/b68ba6b1-bv.js",
"version": "0ffb0ce5253d25fde5786ac06f89aa18586581817c0848ad7fb6f36eba68d2a0"
},
"bv-base.css": {
"filename": "css/36e2074b-bv-base.css",
"version": "ff612bd05801e08367ab5c16f96f31a6a93cfb85f2c4374d3b0f50fa56677978"
},
"bv-desktop.css": {
"filename": "css/3eb2ced8-bv-desktop.css",
"version": "b0bb89274a5c9a4efc441bd541e9fbb2e7da9efb13c09f85cd9f517f25b72a49"
}
},
"load": [
"js/089775a3-bv-cordova.js",
"js/7230ca97-vendor.js",
"js/b68ba6b1-bv.js",
"css/36e2074b-bv-base.css",
"css/3eb2ced8-bv-desktop.css"
],
"root": "./"
}
*/
//GRUNT TASK TO BUILD A JSON MANIFEST FILE FOR HOT CODE UPDATES
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("-").slice(1).join('-');
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);
}
});
}
//write out the JSON to the manifest files
file.dest.forEach(function(f) {
grunt.file.write(f, JSON.stringify(json, null, 2));
});
done();
});
});
@rajatrocks
Copy link

This sample Gruntfile expects filenames to be of the format "7230ca97-vendor.js" and then it strips off the first part to create the key "vendor.js". If your filenames don't have a "-" in them, this code returns an empty key ("") and the files object ends up with only the last item in your list because it keeps overwriting the key "".

var key = filename.split("-").slice(1).join('-');

To get this to work on my code (where there are no dashes in the filenames), I removed the line above and replaced it with:

var key = filename.split("/");
key = key[key.length -1];

which sets the key to just the file part of the filename.

@arieljake
Copy link

or...

var key = filename.split("/").pop();

@RLaptev
Copy link

RLaptev commented Apr 22, 2015

line 27 should be an array as so:
dest: ['manifest.json']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment