|
4 | 4 | * crop image according to user arguments and its position on screen and save it |
5 | 5 | */ |
6 | 6 |
|
7 | | -var gm = require('gm'), |
8 | | - async = require('async'), |
9 | | - request = require('request'), |
10 | | - exclude = require('./exclude.js'); |
| 7 | +var Promise = require('bluebird'); |
| 8 | +var gm = require('gm'); |
| 9 | +Promise.promisifyAll(gm.prototype); |
| 10 | +var request = Promise.promisify(require('request'), {multiArg: true}); |
| 11 | +var exclude = require('./exclude.js'); |
11 | 12 |
|
12 | 13 | module.exports = function(res, done) { |
13 | 14 |
|
14 | | - var that = this, |
15 | | - excludeRect = res.excludeRect, |
16 | | - shot = gm(this.screenshot).quality(100), |
17 | | - cropDim; |
| 15 | + var ctx = this; |
| 16 | + var excludeRect = res.excludeRect; |
| 17 | + var shot = gm(this.screenshot).quality(100); |
| 18 | + var cropDim; |
18 | 19 |
|
19 | | - var x = parseInt(this.currentArgs.x, 10); |
20 | | - var y = parseInt(this.currentArgs.y, 10); |
21 | | - var width = parseInt(this.currentArgs.width, 10); |
22 | | - var height = parseInt(this.currentArgs.height, 10); |
| 20 | + return Promise.try(function() { |
23 | 21 |
|
24 | | - if (!isNaN(x) && !isNaN(y) && !isNaN(width) && !isNaN(height)) { |
| 22 | + var x = parseInt(this.currentArgs.x, 10); |
| 23 | + var y = parseInt(this.currentArgs.y, 10); |
| 24 | + var width = parseInt(this.currentArgs.width, 10); |
| 25 | + var height = parseInt(this.currentArgs.height, 10); |
25 | 26 |
|
26 | | - /** |
27 | | - * crop image with given arguments |
28 | | - */ |
29 | | - cropDim = { |
30 | | - x: x - res.scrollPos.x, |
31 | | - y: y - res.scrollPos.y, |
32 | | - width: width, |
33 | | - height: height |
34 | | - }; |
| 27 | + if (!isNaN(x) && !isNaN(y) && !isNaN(width) && !isNaN(height)) { |
35 | 28 |
|
36 | | - exclude(shot, excludeRect); |
37 | | - shot.crop(cropDim.width, cropDim.height, cropDim.x, cropDim.y); |
| 29 | + /** |
| 30 | + * crop image with given arguments |
| 31 | + */ |
| 32 | + cropDim = { |
| 33 | + x: x - res.scrollPos.x, |
| 34 | + y: y - res.scrollPos.y, |
| 35 | + width: width, |
| 36 | + height: height |
| 37 | + }; |
38 | 38 |
|
39 | | - } else if (res && res.elemBounding) { |
| 39 | + exclude(shot, excludeRect); |
| 40 | + shot.crop(cropDim.width, cropDim.height, cropDim.x, cropDim.y); |
40 | 41 |
|
41 | | - /** |
42 | | - * or use boundary of specific CSS element |
43 | | - */ |
44 | | - cropDim = { |
45 | | - x: res.elemBounding.left + (res.elemBounding.width / 2), |
46 | | - y: res.elemBounding.top + (res.elemBounding.height / 2), |
47 | | - width: isNaN(width) ? res.elemBounding.width : width, |
48 | | - height: isNaN(height) ? res.elemBounding.height : height |
49 | | - }; |
| 42 | + } else if (res && res.elemBounding) { |
50 | 43 |
|
51 | | - exclude(shot, excludeRect); |
52 | | - shot.crop(cropDim.width, cropDim.height, cropDim.x - (cropDim.width / 2), cropDim.y - (cropDim.height / 2)); |
| 44 | + /** |
| 45 | + * or use boundary of specific CSS element |
| 46 | + */ |
| 47 | + cropDim = { |
| 48 | + x: res.elemBounding.left + (res.elemBounding.width / 2), |
| 49 | + y: res.elemBounding.top + (res.elemBounding.height / 2), |
| 50 | + width: isNaN(width) ? res.elemBounding.width : width, |
| 51 | + height: isNaN(height) ? res.elemBounding.height : height |
| 52 | + }; |
53 | 53 |
|
54 | | - } else { |
55 | | - exclude(shot, excludeRect); |
56 | | - } |
| 54 | + exclude(shot, excludeRect); |
| 55 | + shot.crop(cropDim.width, cropDim.height, cropDim.x - (cropDim.width / 2), cropDim.y - (cropDim.height / 2)); |
57 | 56 |
|
58 | | - async.waterfall([ |
59 | | - /** |
60 | | - * save image to fs |
61 | | - */ |
62 | | - function(cb) { |
63 | | - if(!that.self.saveImages) { |
64 | | - return cb(); |
65 | | - } |
| 57 | + } else { |
| 58 | + exclude(shot, excludeRect); |
| 59 | + } |
| 60 | + }) |
| 61 | + .then(function() { |
| 62 | + if (!ctx.self.saveImages) return; |
66 | 63 |
|
67 | | - return shot.write(that.filename || that.baselinePath, cb); |
68 | | - }, |
69 | 64 | /** |
70 | | - * generate image buffer |
| 65 | + * save image to fs |
71 | 66 | */ |
72 | | - function() { |
73 | | - var cb = arguments[arguments.length - 1]; |
74 | | - return shot.toBuffer('PNG', cb); |
75 | | - }, |
| 67 | + return shot.writeAsync(ctx.filename || ctx.baselinePath) |
| 68 | + .then(function() { |
| 69 | + /** |
| 70 | + * generate image buffer |
| 71 | + */ |
| 72 | + return shot.toBufferAsync('PNG'); |
| 73 | + }) |
76 | 74 | /** |
77 | 75 | * upload image to applitools |
78 | 76 | */ |
79 | | - function(buffer) { |
80 | | - var cb = arguments[arguments.length - 1]; |
81 | | - if (!that.self.usesApplitools) { |
82 | | - return cb(); |
83 | | - } |
84 | | - request({ |
85 | | - qs: {apiKey: that.applitools.apiKey}, |
86 | | - url: that.self.host + '/api/sessions/running/' + that.self.sessionId, |
| 77 | + .then(function(buffer) { |
| 78 | + if (!ctx.self.usesApplitools) return; |
| 79 | + return request({ |
| 80 | + qs: {apiKey: ctx.applitools.apiKey}, |
| 81 | + url: ctx.self.host + '/api/sessions/running/' + ctx.self.sessionId, |
87 | 82 | method: 'POST', |
88 | | - headers: that.self.headers, |
89 | | - timeout: that.self.reqTimeout, |
| 83 | + headers: ctx.self.headers, |
| 84 | + timeout: ctx.self.reqTimeout, |
90 | 85 | json: { |
91 | 86 | 'appOutput': { |
92 | 87 | 'title': res.title, |
93 | 88 | 'screenshot64': new Buffer(buffer).toString('base64') |
94 | 89 | }, |
95 | | - 'tag': that.currentArgs.tag || '', |
96 | | - 'ignoreMismatch': that.currentArgs.ignoreMismatch || false |
| 90 | + 'tag': ctx.currentArgs.tag || '', |
| 91 | + 'ignoreMismatch': ctx.currentArgs.ignoreMismatch || false |
97 | 92 | } |
98 | | - }, cb); |
99 | | - } |
100 | | - ], done); |
101 | | - |
| 93 | + }); |
| 94 | + }); |
| 95 | + }).nodeify(done); |
102 | 96 | }; |
0 commit comments