grunt-rev-json
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "grunt-rev-json", | ||
"description": "Grunt task to digest files and output rev for each file in a json.", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/ashi009/grunt-rev-json", | ||
@@ -6,0 +6,0 @@ "author": "Xiaoyi Shi <ashi009@gmail.com>", |
@@ -1,2 +0,2 @@ | ||
# grunt-rev-json | ||
# grunt-rev-json [![NPM version](https://badge.fury.io/js/grunt-rev-json.png)](http://badge.fury.io/js/grunt-rev-json) [![Dependencies](https://david-dm.org/ashi009/grunt-rev-json.png)](https://david-dm.org/ashi009/grunt-rev-json) | ||
@@ -34,3 +34,3 @@ Digest files and output rev for each file in a json. | ||
urlSafe: true, | ||
// prefix length of encoded digest | ||
// prefix length of encoded digest, pass 0 or -1 to use complete digest | ||
length: 8 | ||
@@ -45,1 +45,45 @@ }, | ||
``` | ||
### Example | ||
```sh | ||
$ pwd | ||
/Users/xiaoyi/Projects/public | ||
$ tree | ||
. | ||
├── scripts | ||
│ ├── desktop.js | ||
│ ├── error.js | ||
│ └── moment.js | ||
└── styles | ||
├── desktop.css | ||
└── mobile.css | ||
``` | ||
```js | ||
grunt.initConfig({ | ||
rev_json: { | ||
options: { | ||
algorithm: 'sha224', | ||
length: -1 | ||
}, | ||
static: { | ||
src: ['public/**/*.{js,css}'], | ||
dest: 'public/rev.json' | ||
} | ||
} | ||
}); | ||
``` | ||
```json | ||
{ | ||
"scripts/desktop.js": "eSgRA3v4XUcOM66neA7Y2ZfMOX5w0hNWVfuHpQ", | ||
"scripts/error.js": "ZQIiKlJ0_Nj0F8-hi1mnYccZ61rJ5n426vgVBw", | ||
"scripts/mobile.js": "QxB9zfm1PyhYKFMBiqXh9ox8fmCcNmwXWkTRFg", | ||
"styles/desktop.css": "xjrIkYLwmiL6tTMduEjtziR3cr3bJkxyHZ3wQw", | ||
"styles/mobile.css": "YcJ2rs1SI7kbcjgnVO8K0Bf1moFVLwmbPHrVaw" | ||
} | ||
``` | ||
Output JSON will use src path (relative to output file location) as key, and | ||
digest as value. |
@@ -8,6 +8,5 @@ var pth = require('path'); | ||
crc32c: function(buf) { | ||
var checksum = crc32c.calculate(buf); | ||
var buf = new Buffer(4); | ||
buf.writeUInt32BE(checksum, 0); | ||
return buf; | ||
var checksum = new Buffer(4); | ||
checksum.writeUInt32BE(crc32c.calculate(buf), 0); | ||
return checksum; | ||
}, | ||
@@ -43,34 +42,35 @@ default: function(algorithm) { | ||
grunt.registerMultiTask('rev_json', 'Digest files and outpu as a json file.', function() { | ||
grunt.registerMultiTask('rev_json', 'Digest files and outpu as a json file.', function() { | ||
var options = this.options({ | ||
algorithm: 'crc32c', | ||
encoding: 'base64', | ||
urlSafe: true, | ||
length: 8 | ||
}); | ||
var options = this.options({ | ||
algorithm: 'crc32c', | ||
encoding: 'base64', | ||
urlSafe: true, | ||
length: 8 | ||
}); | ||
var hasher = options.algorithm; | ||
if (typeof hasher == 'string') | ||
hasher = kAlgorithms[hasher] || kAlgorithms.default(hasher); | ||
if (!hasher) | ||
return grunt.fail('Cannot find algorithm "' + options.algorithm + '".'); | ||
var hasher = options.algorithm; | ||
if (typeof hasher == 'string') | ||
hasher = kAlgorithms[hasher] || kAlgorithms.default(hasher); | ||
if (!hasher) | ||
return grunt.fail('Cannot find algorithm "' + options.algorithm + '".'); | ||
this.files.forEach(function(file) { | ||
var versions = {}; | ||
file.src.forEach(function(path) { | ||
var rev = hasher(grunt.file.read(path)).toString(options.encoding); | ||
if (options.encoding == 'base64' && options.urlSafe) | ||
rev = toUrlSafeBase64(rev); | ||
rev = rev.substr(0, options.length); | ||
grunt.log.writeln('File ' + chalk.cyan(path) + ' digested (' + | ||
chalk.cyan(rev) + ').'); | ||
versions[relativePath(file.dest, path)] = rev; | ||
this.files.forEach(function(file) { | ||
var versions = {}; | ||
file.src.forEach(function(path) { | ||
var rev = hasher(grunt.file.read(path)).toString(options.encoding); | ||
if (options.encoding == 'base64' && options.urlSafe) | ||
rev = toUrlSafeBase64(rev); | ||
if (options.length > 0) | ||
rev = rev.substr(0, options.length); | ||
grunt.log.writeln('File ' + chalk.cyan(path) + ' digested (' + | ||
chalk.cyan(rev) + ').'); | ||
versions[relativePath(file.dest, path)] = rev; | ||
}); | ||
grunt.file.write(file.dest, JSON.stringify(versions)); | ||
grunt.log.writeln('File ' + chalk.cyan(file.dest) + ' created (rev).'); | ||
}); | ||
grunt.file.write(file.dest, JSON.stringify(versions)); | ||
grunt.log.writeln('File ' + chalk.cyan(file.dest) + ' created (rev).') | ||
}); | ||
}); | ||
}; |
Sorry, the diff of this file is not supported yet
6398
88