-
Notifications
You must be signed in to change notification settings - Fork 474
/
Copy pathindex.js
217 lines (201 loc) · 7.12 KB
/
index.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
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
209
210
211
212
213
214
215
216
217
/**
* Builder
* @author zsx<zsx@zsxsoft.com>
*/
var express = require('express');
var path = require('path');
var Promise = require("bluebird");
var fs = Promise.promisifyAll(require('fs'));
var app = new express();
var queueReady = ["server"];
var port = process.env.PORT || 3000;
var httpServer = "http://127.0.0.1:" + port + "/";
var pageWidth = 1900; // Magic number!
var pageHeight = 3800;
String.prototype.repeat = function(count) {
return new Array(count + 1).join(this);
}
/**
* Search Github
* Useless
* Didn't import the dependencies in the package.json.
*/
var searchGitHub = function searchGitHub() {
var request = require("request");
var async = require("async");
var originalData = require('./ux/WebFrontEndStack.json');
var q = async.queue(function(object, callback) {
if (object.noRequest || object.github || !object.url || /mozilla|wikipedia/.test(object.url)) {
callback(false);
return;
}
console.log("Running " + object.name);
request(object.url, function(err, res, body) {
if (!err && res.statusCode == 200) {
var rep = body.match(/<a.+?href=("|')([a-z:\/]+?github.com.+?)("|')/);
if (rep === null) {
callback(false);
return;
}
object.github = rep[2];
callback(true);
}
});
}, 5);
var addQueue = function addQueue(object) {
q.push(object, function(err) {
if (err) console.log(object.name + " = " + object.github);
});
if (object.children) {
object.children.forEach(function(val) {
addQueue(val);
});
}
};
addQueue(originalData);
q.push({noRequest: true}, function() {
console.log(originalData);
console.log(JSON.stringify(originalData));
fs.writeFileAsync("./ux/WebFrontEndStack.json2", JSON.stringify(originalData), "utf-8");
});
}
/**
* Use to build a promise for some fucking async api.
* @param object object
**/
var promisify = function promisify(object) {
for (var key in object) {
(function(key, asyncKey) {
object[asyncKey] = function() {
var resolver = Promise.defer();
var len = arguments.length;
var argu = new Array(len + 1);
for (var i = 0; i < len; i++) { // I think a Array.from is more convenient.
argu[i] = arguments[i];
}
argu[len] = function() {
resolver.resolve.apply(resolver, arguments); // Callback arguments
};
object[key].apply(object, argu);
return resolver.promise;
}
})(key, key + "Async");
}
}
/**
* Recursion to generate readme
* @param object object
* @param int deep
* @return string
*/
var buildReadme = function buildReadme(object, deep) {
var deeper = deep + 1;
var deepString = "\t".repeat(deep) + "- ";
var ret = [];
ret.push((function(deepString, name, url, github) {
var haveUrl = !!url;
var haveGitHub = !!github;
return deepString + (haveUrl ? "[" : "") + object.name + (haveUrl ? "](" + url + ")" : "") + (haveGitHub ? " [\[GitHub\]](" + github + ")" : "");
})(deepString, object.name, object.url, object.github));
if (object.children) {
object.children.map(function(value, index) {
ret.push(buildReadme(value, deeper));
});
}
return ret.join("\n");
}
var actions = {
/**
* For running phantomjs to take a screenshot for the webpage
* @return Promise<any>
*/
phantomjs: function phantomjs() {
return new Promise(function(resolve, reject) {
var phantom = require('phantom');
var ph;
var page;
promisify(phantom);
// What the fucking API
return phantom.createAsync().then(function(phantom) {
ph = phantom;
promisify(ph);
console.log("Created Phantomjs");
return ph.createPageAsync();
}).then(function(pg) {
page = pg;
promisify(pg);
return page.setAsync('viewportSize', {
width: pageWidth,
height: pageHeight
});
}).then(function() {
console.log("Set viewportSize");
return page.openAsync(httpServer);
}).then(function(status) {
console.log("Rendered HTML, the image will be saved after 2 seconds.");
if (status == "success") {
return Promise.delay(2000);
} else {
return reject(status);
}
}).then(function() {
return page.renderAsync(path.join(__dirname, 'Web Front End Stack.png'));
}).then(function() {
console.log("The image saved successfully!");
return resolve();
}).then(function() {
page.close();
ph.exit();
});
});
},
/**
* To rebuild the README.md
* @return Promise<any>
*/
readme: function readme() {
var json = require('./ux/WebFrontEndStack.json');
return Promise.resolve().then(function() {
return fs.readFileAsync("./README.md", "utf-8");
}).then(function(fileContent) {
var ret = buildReadme(json, 0);
fileContent = fileContent.replace(/<\!--BUILD_START-->[\d\D]+?<\!--BUILD_END-->/, "{%BuildStart%}")
return fs.writeFileAsync("./README.md", fileContent.replace("{%BuildStart%}", "<!--BUILD_START-->\n\n" + ret + "\n\n<!--BUILD_END-->", "utf-8"));
}).then(function() {
console.log('Readme built successfully!');
})
},
/**
* To start an express server
* @return Promise<any>
*/
server: function server() {
return new Promise(function(resolver, reject) {
return app
.set('port', port)
.set('view engine', 'html')
.use(express.static(path.join(__dirname, '/ux')))
.use('/', function(req, res) {
res.redirect('/WebFrontEndStack.htm');
})
.listen(port, function() {
console.info('Express started on: http://127.0.0.1:' + port);
resolver(app);
});
});
}
};
var queue = [actions.server()];
process.argv.forEach(function(val) {
if (val in actions) {
console.info("Task: " + val);
queue.push(actions[val]());
}
});
var promise = Promise.all(queue);
if (queue.length > 1) { // for somebody who only want to start the server.
promise.then(function() {
console.log("OK!");
process.exit(0);
});
}