Skip to content

Instantly share code, notes, and snippets.

@SystemParadox
Last active May 12, 2016 15:27
Show Gist options
  • Save SystemParadox/21504931bffee2f5aa023f5011ee35ce to your computer and use it in GitHub Desktop.
Save SystemParadox/21504931bffee2f5aa023f5011ee35ce to your computer and use it in GitHub Desktop.
gulp plugin to generate manifest for cordova-app-loader
/***
Gulp plugin to generate a manifest.json for cordova-app-loader.
Example usage:
```
var updateManifest = require('./updateManifest');
gulp.task('manifest', function () {
gulp.src(['www/app.js', 'www/style.css'])
.pipe(updateManifest('manifest.json', { load: ['www/app.js'] })
.pipe(gulp.dest('www'));
});
```
Produces the following in `www/manifest.json`:
```
{
"files": {
"app.js": {
"version": "4004535bdc97a3b764cb1d0343d1bc813cbdce93",
"filename": "app.js"
},
"style.css": {
"version": "2eea6c3fa942a7386bebefddf84c0df214c90248",
"filename": "style.css"
}
},
"load": [
"app.js"
],
"version": "d21f0e85ad97235a7f934d6e0fc0b87025c447ec"
}
```
***/
var crypto = require('crypto');
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var File = gutil.File;
function checksum(data) {
return crypto.createHash('sha1').update(data).digest('hex');
}
module.exports = function (manifestFile, options) {
var versionChecksum = '';
var files = {};
function processFile(file, encoding, callback) {
if (file.isNull()) {
// ignore empty files
callback();
return;
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-cordova-app-loader-manifest', 'Streaming not supported'));
callback();
return;
}
var version = checksum(file.contents);
versionChecksum += version;
files[file.relative] = {
version: version,
filename: file.relative,
};
callback();
}
function endStream(callback) {
var contents = JSON.stringify({
files: files,
load: options.load || [],
version: checksum(versionChecksum),
}, null, 2);
this.push(new File({ path: manifestFile, contents: new Buffer(contents) }));
this.push(null);
callback();
}
return through.obj(processFile, endStream);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment