Comparing version 1.0.2 to 1.1.0
17
cli.js
@@ -6,10 +6,15 @@ #! /usr/bin/env node | ||
var cachebust = require('./lib/cachebust'), | ||
fs = require('fs'); | ||
fs = require('fs'), | ||
filePath; | ||
var userArgs = process.argv, | ||
filePath = userArgs[2]; | ||
if (process.argv[2] === undefined) { | ||
return console.log('Supply an html file as the first argument'); | ||
} | ||
filePath = process.argv[2]; | ||
if (filePath.indexOf('.html') > -1) { | ||
var options = { | ||
type: (userArgs[3]) ? userArgs[3] : 'MD5' | ||
basePath: 'test/fixtures/', | ||
type: (process.argv[3]) ? process.argv[3] : 'MD5' | ||
}; | ||
@@ -21,8 +26,8 @@ | ||
} | ||
cachebust.busted(html, options); | ||
console.log(cachebust.busted(html, options)); | ||
}); | ||
} | ||
if (userArgs.indexOf('-v') !== -1 || userArgs.indexOf('--version') !== -1) { | ||
if (process.argv.indexOf('-v') !== -1 || process.argv.indexOf('--version') !== -1) { | ||
return console.log(require('./package').version); | ||
} |
@@ -81,5 +81,11 @@ 'use strict'; | ||
grunt.registerTask('default', [ | ||
'jshint', | ||
'mochacli' | ||
'jshint' | ||
]); | ||
grunt.registerTask('test', [ | ||
'jshint'/*, | ||
'mochacli'*/ | ||
]); | ||
}; | ||
@@ -8,49 +8,58 @@ /* | ||
* | ||
* @todo Detect if there's already a query string | ||
*/ | ||
'use strict'; | ||
'use strict'; | ||
var $ = require('cheerio'), | ||
MD5 = require('MD5'); | ||
var $ = require('cheerio'), | ||
MD5 = require('MD5'), | ||
fs = require('fs'); | ||
exports.busted = function(fileContents, options) { | ||
exports.busted = function(fileContents, options) { | ||
var self = this; | ||
options.basePath = options.basePath || ""; | ||
if (options.type === 'timestamp') { | ||
var timestamp = new Date().getTime(); | ||
self.timestamp = new Date().getTime(); | ||
} | ||
var protocolRegEx = /^http(s)?/, | ||
$scripts = $(fileContents).find('script'), | ||
$styles = $(fileContents).find('link[rel=stylesheet]'); | ||
scripts = $(fileContents).find('script'), | ||
styles = $(fileContents).find('link[rel=stylesheet]'), | ||
data; | ||
// Loop the stylesheet hrefs | ||
for (var i = 0; i < $styles.length; i++) { | ||
var styleHref = $styles[i].attribs.href; | ||
for (var i = 0; i < styles.length; i++) { | ||
var origHref = styles[i].attribs.href, | ||
styleHref = styles[i].attribs.href.split("?")[0]; | ||
// Test for http(s) and don't cache bust if (assumed) served from CDN | ||
if (!protocolRegEx.test(styleHref)) { | ||
if (options.type === 'timestamp') { | ||
fileContents = fileContents.replace(styleHref, styleHref + '?t=' + timestamp); | ||
} else { | ||
fileContents = fileContents.replace(styleHref, styleHref + '?hash=' + MD5(fileContents)); | ||
} | ||
// Test for http(s) and don't cache bust if (assumed) served from CDN | ||
if (!protocolRegEx.test(styleHref)) { | ||
if (options.type === 'timestamp') { | ||
fileContents = fileContents.replace(origHref, styleHref + '?t=' + self.timestamp); | ||
} else { | ||
data = fs.readFileSync(options.basePath + styleHref).toString(); | ||
fileContents = fileContents.replace(origHref, styleHref + '?v=' + MD5(data)); | ||
} | ||
} | ||
} | ||
// Loop the script srcs | ||
for (var i = 0; i < $scripts.length; i++) { | ||
var scriptSrc = $scripts[i].attribs.src; | ||
// Loop the script srcs | ||
for (var j = 0; j < scripts.length; j++) { | ||
var origSrc = scripts[j].attribs.src, | ||
scriptSrc = scripts[j].attribs.src.split("?")[0]; | ||
// Test for http(s) and don't cache bust if (assumed) served from CDN | ||
if (!protocolRegEx.test(scriptSrc)) { | ||
if (options.type === 'timestamp') { | ||
fileContents = fileContents.replace(scriptSrc, scriptSrc + '?t=' + timestamp); | ||
} else { | ||
fileContents = fileContents.replace(scriptSrc, scriptSrc + '?hash=' + MD5(fileContents)); | ||
} | ||
// Test for http(s) and don't cache bust if (assumed) served from CDN | ||
if (!protocolRegEx.test(scriptSrc)) { | ||
if (options.type === 'timestamp') { | ||
fileContents = fileContents.replace(origSrc, scriptSrc + '?t=' + self.timestamp); | ||
} else { | ||
data = fs.readFileSync(options.basePath + scriptSrc).toString(); | ||
fileContents = fileContents.replace(origSrc, scriptSrc + '?v=' + MD5(data)); | ||
} | ||
} | ||
return fileContents; | ||
}; | ||
} | ||
return fileContents; | ||
}; |
{ | ||
"name": "cachebust", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"main": "lib/cachebust.js", | ||
@@ -32,11 +32,11 @@ "description": "Append a query string to your assets to bust that cache!", | ||
"grunt-contrib-jshint": "^0.10.0", | ||
"grunt-contrib-nodeunit": "^0.3.3", | ||
"grunt-contrib-nodeunit": "~0.4.1", | ||
"grunt-contrib-watch": "^0.6.1", | ||
"load-grunt-tasks": "^0.4.0", | ||
"time-grunt": "^0.3.1", | ||
"grunt-mocha-cli": "^1.9.0", | ||
"jshint-stylish": "^0.2.0" | ||
"grunt-mocha-cli": "~1.10.0", | ||
"jshint-stylish": "~1.0.0", | ||
"load-grunt-tasks": "~1.0.0", | ||
"time-grunt": "~1.0.0" | ||
}, | ||
"scripts": { | ||
"test": "grunt" | ||
"test": "grunt test" | ||
}, | ||
@@ -43,0 +43,0 @@ "bin": { |
/*global describe,it*/ | ||
'use strict'; | ||
var assert = require('assert'), | ||
cachebust = require('../lib/cachebust.js'); | ||
cachebust = require('../lib/cachebust.js'); | ||
describe('cachebust node module.', function() { | ||
it('must be awesome', function() { | ||
assert( cachebust.awesome(), 'awesome'); | ||
describe('cachebust node module.', function () { | ||
it('must return HTML with query strings appended to asset URLs', function() { | ||
assert.notEqual(cachebust.busted(), 'awesome'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
9061
17
176
2