forked from unruledboy/DevelopmentStack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (230 loc) · 8.45 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* Builder
* @author zsx<[email protected]>
*/
var express = require('express');
var path = require('path');
var Promise = require("bluebird");
var request = require("request");
var async = require("async");
var fs = Promise.promisifyAll(require('fs'));
var app = new express();
var queueReady = ["server"];
var pageWidth = 3900; // Magic number!
var pageHeight = 3700;
var config = {
port: 3000,
update_existed_stargazers: false
}
var httpServer = "http://127.0.0.1:" + config.port + "/";
String.prototype.repeat = function(count) {
return new Array(count + 1).join(this);
}
/**
* 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 = {
/**
* Update the stargazers of the GitHub repo
* Be careful! There have the rate limit!
* @see https://developer.github.com/v3/#increasing-the-unauthenticated-rate-limit-for-oauth-applications
* @return Promise<any>
*/
updatestargazers: function updatestargazers() {
return new Promise(function(resolve, reject) {
var originalData = JSON.parse(fs.readFileSync("./ux/DevelopmentStack.json", "utf-8")); // Require will lock the file.
var getGitHubApi = function(github) {
var githubArray = github.split("/");
// I want a sprintf T_T
return "https://api.github.com/repos/" + githubArray[3] + "/" + githubArray[4];
};
var q = async.queue(function(object, callback) {
if (!object.github || (!config.update_existed_stargazers && object.stargazers)) {
callback(false);
return;
}
var githubUrl = getGitHubApi(object.github);
console.log("Getting " + githubUrl);
request({
url: githubUrl,
headers: {
"User-Agent": "https://github.com/unruledboy/DevelopmentStack" // GitHub required user-agent
}
}, function(err, res, body) {
if (!err && res.statusCode == 200) {
var json = JSON.parse(body);
if (json === null) {
callback(false);
return;
}
object.stargazers = json.stargazers_count;
callback(true);
} else {
if (res.statusCode == 403) { // Out of API limit!
console.error("Out of GitHub API limit!");
console.error("The limit will be reset when " + new Date(res.headers['x-ratelimit-reset'] * 1000));
q.kill();
reject("Out of GitHub API limit!");
}
callback(false);
}
});
}, 5);
var addQueue = function addQueue(object) {
q.push(object, function(err) {
if (err) console.log(object.name + " = " + object.stargazers);
});
if (object.children) {
object.children.forEach(function(val) {
addQueue(val);
});
}
};
addQueue(originalData);
q.push({ // For some reason, the ``drain`` will not be called.
noRequest: true
}, function() {
fs.writeFileAsync("./ux/DevelopmentStack.json", JSON.stringify(originalData), "utf-8").then(function() {
resolve();
});
});
return q;
})
},
/**
* For running phantomjs to take a screenshot for the webpage
* @return Promise<any>
*/
phantomjs: function phantomjs() {
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, 'Development Stack.png'));
}).then(function() {
console.log("The image saved successfully!");
page.close();
ph.exit();
});
},
/**
* To rebuild the README.md
* @return Promise<any>
*/
readme: function readme() {
var json = require('./ux/DevelopmentStack.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', config.port)
.set('view engine', 'html')
.use(express.static(path.join(__dirname, '/ux')))
.use('/', function(req, res) {
res.redirect('/DevelopmentStack.htm');
})
.listen(config.port, function() {
console.info('Express started on: http://127.0.0.1:' + config.port);
resolver(app);
});
});
}
};
var queue = [actions.server()];
process.argv.forEach(function(val) {
if (val in actions) {
console.info("Task: " + val);
queue.push(actions[val]());
} else if (val[0] == "-" && val.indexOf("=") >= 0) {
var obj = val.split("=");
var name = obj[0].split("--")[1];
var value = obj[1];
config[name] = value;
}
});
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);
});
}