-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
208 lines (196 loc) · 6.16 KB
/
index.js
File metadata and controls
208 lines (196 loc) · 6.16 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var Hapi = require('hapi');
var Path = require('path');
var _ = require('lodash');
// Create a server with a host and port
var server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 3000
});
// Add the route
server.route(require('./routes'));
server.views({
engines: {
html: require('handlebars')
},
path: 'views',
helpersPath: 'views/helpers',
layoutPath: 'views/layout',
layout: 'default'
});
// Start the server
server.register({
register: require('good'),
options: {
reporters: [{
reporter: require('good-console'),
events: {
log: '*',
error: '*'
}
}]
}
}, function (err) {
if (err) {
throw err;
}
if (!module.parent) {
server.start(function () {
server.log('info', 'Server running at: ' + server.info.uri);
screenshot();
});
}
});
Pipeline = (function(){
return function () {
var self = this;
/**
* Pipeline
*
*
*/
var _pipeline = [];
var length = 0;
/**
* @description It is true if we are in flushing function now
* @type {boolean}
*/
var inFlush = false;
/**
* @description It true if a flushing function is scheduled, or if we are in flushing now
* @type {boolean}
*/
var willFlush = false;
self.push = function(func /** param1, param2 ... */){
var args = Array.prototype.slice.call(arguments, 1);
if(!_.isFunction(func))
throw new Error("param must be type of function");
func = _.bind.apply(_, [func, null].concat(args));
_pipeline.push(func);
return this;
};
self.unshift = function(func /** param1, param2 ... */){
var args = Array.prototype.slice.call(arguments, 1);
if(!_.isFunction(func))
throw new Error("param must be type of function");
func = _.bind.apply(_, [func, null].concat(args));
_pipeline.unshift(func);
return this;
};
self.flushing = function(num){
var tmp = null;
inFlush = true;
length = _.isNumber(num) ? num : _pipeline.length;
for (var i = 0; i < length; i++) {
tmp = _pipeline.shift();
if(_.isFunction(tmp))
tmp();
}
//reset
//_pipeline = [];
length = 0;
willFlush = false;
inFlush = false;
return this;
};
// set
self.willFlush = function( p ){
willFlush = !!p;
return this;
};
self.inFlush = function( p ){
inFlush = !!p;
return this;
};
// get
self.isWillFlush = function(){
return willFlush;
};
self.isInFlush = function(){
return inFlush;
};
self.requireFlush = function (num, second) {
if (! willFlush) {
var args = Array.prototype.slice.call(arguments, 0);
willFlush = true;
second = _.isUndefined(second) ? 0 : second;
setTimeout(function () {
var func = _.bind.apply(self, [self.flushing, null].concat(args));
func();
}, second);
}
return this;
};
self.reset = function () {
_pipeline = [];
length = 0;
willFlush = false;
inFlush = false;
return this;
};
self.sequenceFlush = function (option) {
if(self.isSequenceFlush)
return;
self._sequenceFlush(option);
};
self._sequenceFlush = function (option) {
self.isSequenceFlush = true;
self.requireFlush(1);
setTimeout(function () {
if(_pipeline.length > 0)
self._sequenceFlush(option);
else
self.isSequenceFlush = false;
}, option.duration);
};
self.getLength = function () {
return _pipeline.length;
};
};
})();
var child_process = require('child_process');
var fs = require('fs');
// how long to let phantomjs run before we kill it
var REQUEST_TIMEOUT = 15*1000;
// maximum size of result HTML. node's default is 200k which is too
// small for our docs.
var MAX_BUFFER = 5*1024*1024; // 5MB
PHANTOM_SCRIPT = fs.readFileSync("./bin/screenshot.js", "utf8");
function screenshotTask (num) {
console.log('http://localhost:3000/story/' + num + '"');
var pt = 'var url = "http://localhost:3000/story/' + num + '"; var urls = url.split("/");' + PHANTOM_SCRIPT;
child_process.execFile('/bin/bash', [
'-c',
// https://groups.google.com/forum/#!msg/meteor-talk/dPLss2idrJg/Dd4qL9O9d6AJ
// https://groups.google.com/d/msg/meteor-talk/dPLss2idrJg/rUmD8Ak9Ln8J
// --ssl-protocol=tlsv1
// https://groups.google.com/d/msg/meteor-talk/dPLss2idrJg/Dd4qL9O9d6AJ
// --ignore-ssl-errors=yes
("exec phantomjs --ignore-ssl-errors=yes --ssl-protocol=tlsv1 --load-images=yes /dev/stdin <<'END'\n" + pt + "END\n")],
{
timeout: REQUEST_TIMEOUT,
maxBuffer: MAX_BUFFER
}, function(error, stdout, stderr) {
// phantomjs failed. Don't send the error, instead send the
// normal page.
if(error){
if (error.code === 127)
console.log("phantomjs: phantomjs not installed. Download and install from http://phantomjs.org/");
else {
console.log("phantomjs: phantomjs failed:", error, "\nstderr:", stderr);
}
}
else
console.log("done: ");
});
}
var stories = require('./private/stories');
function screenshot (num) {
var a = new Pipeline();
for (var i = stories.length - 1; i >= 0; i--) {
a.push(screenshotTask.bind(null, i));
};
a.sequenceFlush({
duration: 1500 //ms
});
}