-
Notifications
You must be signed in to change notification settings - Fork 474
/
Copy pathindex.js
155 lines (145 loc) · 4.97 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
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 = 2200; // Magic number!
var pageHeight = 4000;
String.prototype.repeat = function(count) {
var ret = [];
while (count--) {
ret.push(this);
}
return ret.join("");
}
/**
* Use to build a promise for some fucking async api.
* @param object object
* @param string method
* @param array otherArguments
* @return Promise<any>
*/
var promiseFactory = function promiseFactory(object, otherArguments) {
var resolver = Promise.defer();
var argu = otherArguments;
if (!(argu instanceof Array)) {
argu = [];
}
argu.push(function() {
resolver.resolve.apply(resolver, arguments);
});
object.apply(object, argu);
return resolver.promise;
}
/**
* 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 = [deepString + object.name];
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;
// What the fucking API?
return promiseFactory(phantom.create).then(function(phantom) {
ph = phantom;
console.log("Created Phantomjs");
return promiseFactory(ph.createPage);
}).then(function(pg) {
page = pg;
return promiseFactory(page.set, ['viewportSize', {
width: pageWidth,
height: pageHeight
}]);
}).then(function(err) {
console.log("Set viewportSize");
return promiseFactory(page.open, [httpServer]);
}).then(function(status) {
console.log("Rendered HTML, the image will save after 2 seconds.");
if (status == "success") {
return Promise.delay(2000);
} else {
return reject(status);
}
}).then(function() {
return promiseFactory(page.render, [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.html');
})
.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);
});
}