-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresemble.js
90 lines (71 loc) · 2.16 KB
/
resemble.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* grunt-injector
* https://github.com/dfox-powell/grunt-injector
*
* Copyright (c) 2014 dtothefp
* Licensed under the MIT license.
*/
'use strict';
var path = require('path');
var spawn = require('child_process').spawn;
module.exports = function(grunt) {
grunt.registerMultiTask('resemble', 'The best Grunt plugin ever.', function() {
var done = this.async();
var opts = {
cwd: process.cwd(),
stdio: 'inherit'
};
var options = this.options({
screenshotRoot: '',
url: 'http://optimizely.com/',
width: 1024,
gm: false,
debug: false
});
var args = [ path.join(__dirname, '..', 'node_modules', 'resemble-cli', 'bin', 'run-resemble.js') ];
options.pages = [];
options.screensDir = this.data.dest ? this.data.dest : this.data.files[0].dest;
//globbing enabled
if(this.filesSrc.length > 0){
var urlSplitLast = options.url.split('/').pop();
var reg = new RegExp(urlSplitLast);
this.filesSrc.forEach(function(file){
if(reg.test(file)) {
file = file.replace(urlSplitLast, '');
}
var lastSlashInex = file.lastIndexOf('/');
var sliced = file.slice(lastSlashInex);
if(/\.html/.test(sliced) || /\.php/.test(sliced)) {
file = file.substr(0, lastSlashInex);
}
if(file.indexOf('/') === 0) {
file = file.replace(/\//, '');
}
if(!!file) {
options.pages.push(file);
}
});
} else {
this.files[0].orig.src.forEach(function(file, i) {
options.pages.push(file);
});
}
options.pages = options.pages.join(',');
for (var prop in options) {
var value = options[prop];
if(typeof value !== 'boolean' && !!value) {
options[prop] = prop + '=' + value;
} else if (typeof value === 'boolean' && !!value) {
options[prop] = '--' + prop;
}
if (!!options[prop]) {
args.push(options[prop]);
}
}
var cp = spawn('node', args, opts);
cp.on('close', function (code) {
console.log('child process finished: ' + code);
done();
});
});
};