Comparing version 0.0.1 to 0.0.2
110
lib/index.js
@@ -6,4 +6,4 @@ var fs = require('fs-extra'), | ||
function normalize(name, prefix) { | ||
return [prefix || '', name.replace(/[^\w_-]+/g, '_')].join('@'); | ||
function normalize(name) { | ||
return name.replace(/[^\w_-]+/g, '_'); | ||
} | ||
@@ -27,44 +27,52 @@ | ||
function takeScreenshot(done, selector, position, imageData, customName) { | ||
var fixedSelector = encodeURIComponent(selector), | ||
fixedName = customName ? normalize(customName) : normalize(client.currentTest.name, fixedSelector); | ||
function initializeCache(filepath, fixedName) { | ||
var cache = options.cache[fixedName] || {}; | ||
var filepath = path.join(options.path, fixedName); | ||
var indexFile = path.join(filepath, 'index.json'); | ||
var image = new Buffer(imageData, 'base64'); | ||
if (!cache.src && fs.existsSync(indexFile)) { | ||
cache.src = fs.readJsonSync(indexFile); | ||
} else { | ||
cache.src = []; | ||
} | ||
var data = { | ||
selector: selector, | ||
position: position | ||
}; | ||
if (!cache.add) { | ||
cache.filepath = filepath; | ||
var indexFile = path.join(filepath, 'index.json'), | ||
indexedImages = options.cache[fixedName] || []; | ||
options.cache[fixedName] = cache; | ||
if (!indexedImages.length && fs.existsSync(indexFile)) { | ||
indexedImages = fs.readJsonSync(indexFile); | ||
cache.add = function(position, cb) { | ||
cache.src.push(position); | ||
fs.outputJson(indexFile, cache.src, cb); | ||
}; | ||
} | ||
var screenshotFile = path.join(filepath, fixedName + '.png'); | ||
return cache; | ||
} | ||
fs.outputFileSync(screenshotFile, image); | ||
function takeScreenshot(done, position, imageData, customName) { | ||
var fixedName = normalize(customName || client.currentTest.name); | ||
var shot = gm(screenshotFile).quality(100); | ||
var loadImage = gm.subClass({ imageMagick: !options.gm }); | ||
shot.crop(data.position.width, data.position.height, data.position.left, data.position.top); | ||
var filepath = path.join(options.path, fixedName); | ||
var i = indexedImages.length; | ||
var image = new Buffer(imageData, 'base64'); | ||
if (customName) { | ||
i = fixedSelector + '_' + i; | ||
} | ||
var indexedImages = initializeCache(filepath, fixedName); | ||
data.filepath = path.join(filepath, i + '.png'); | ||
var imageChunk = path.join(filepath, indexedImages.src.length + '.png'); | ||
shot.write(data.filepath, function() { | ||
indexedImages.push(data); | ||
fs.outputFileSync(imageChunk, image); | ||
options.cache[fixedName] = indexedImages; | ||
var shot = loadImage(imageChunk).quality(100); | ||
fs.outputJson(indexFile, indexedImages, done); | ||
shot.crop(position.width, position.height, position.left, position.top); | ||
shot.write(imageChunk, function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
indexedImages.add(position, done); | ||
}); | ||
@@ -87,3 +95,3 @@ } | ||
api.screenshot(true, function(result) { | ||
takeScreenshot(cb, selector, position, result.value, customName); | ||
takeScreenshot(cb, position, result.value, customName); | ||
}); | ||
@@ -97,2 +105,17 @@ }); | ||
function diffFactory(client, options) { | ||
function retrieveCache(customName, fixedName) { | ||
var cache = options.cache[customName] || options.cache[fixedName]; | ||
if (cache && cache.src.length > 1) { | ||
var a = cache.src.length - 2, | ||
b = cache.src.length - 1; | ||
return { | ||
latest: path.join(cache.filepath, a + '.png'), | ||
current: path.join(cache.filepath, b + '.png'), | ||
filepathDiff: path.join(cache.filepath, a + '_' + b + '.png') | ||
}; | ||
} | ||
} | ||
return function(selector, done) { | ||
@@ -108,26 +131,23 @@ if (!selector) { | ||
var customName = normalize(selector), | ||
fixedName = normalize(this.currentTest.name, encodeURIComponent(selector)); | ||
fixedName = normalize(this.currentTest.name); | ||
client | ||
.perform(function(api, cb) { | ||
var indexedImages = options.cache[customName] || options.cache[fixedName]; | ||
var diffImages = retrieveCache(customName, fixedName); | ||
var diffImages = indexedImages.slice(-2), | ||
latestImage = diffImages.shift(), | ||
currentImage = diffImages.pop(); | ||
if (!currentImage) { | ||
if (!diffImages) { | ||
return cb(); | ||
} | ||
resemble(currentImage.filepath) | ||
.compareTo(latestImage.filepath) | ||
resemble(diffImages.current) | ||
.compareTo(diffImages.latest) | ||
.onComplete(function(data) { | ||
var x = indexedImages.length - 1; | ||
var filepath = path.dirname(currentImage.filepath); | ||
data.filepathDiff = path.join(filepath, (x - 1) + '_' + x + '.png'); | ||
done.call(client, data, cb); | ||
data.getDiffImage().pack() | ||
.pipe(fs.createWriteStream(diffImages.filepathDiff)) | ||
.on('finish', function() { | ||
done.call(client, { | ||
filepathDiff: diffImages.filepathDiff, | ||
imageDiff: data | ||
}, cb); | ||
}); | ||
}); | ||
@@ -134,0 +154,0 @@ }); |
{ | ||
"name": "dayguard", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
4663
5
127