-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
117 lines (97 loc) · 3.56 KB
/
main.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
#!/usr/bin/env node
var fs = require('fs'),
prepare = require('./prepare'),
url = require('url'),
path = require('path'),
http = require('http'),
fcgi = require('node-fastcgi')
connect = require('connect'),
connect.logger = require('morgan'),
connect.static = require('serve-static'),
connect.bodyParser = require('body-parser'),
Module = require('module');
Array.prototype.flatten = function() {
return Array.prototype.concat.apply([], this);
};
var version = require('./package.json').version;
console.error('Welcome to Prepare.js version %s!', version);
process.on('uncaughtException', function(error) {
console.error(error);
process.exit(1);
});
var optimist = require('optimist')
.usage('Usage:\tprepare [--http [port]|--fcgi [socket]] [--path/-p path]\n'
+ ' Runs JavaScript on the server in a browser-like environment.\n'
+ ' If "path" is a file, executes it and writes the result to stdout.\n'
+ ' Otherwise, if it is a directory, starts a server with that directory as the root directory.')
.alias('help', 'h')
.alias('path', 'p')
.describe({
path: 'File/directory to work with',
http: 'Run as HTTP server (optional: port to listen on, default is 8080)',
fcgi: 'Run as FastCGI server (optional: socket to listen on, default is file descriptor 0)',
help: 'Display this message'
}),
argv = optimist.argv;
if (argv.http && argv.fcgi) {
console.error(optimist.help());
throw 'Cannot run in both HTTP and FastCGI mode.';
}
if (argv.help) {
console.error(optimist.help());
process.exit();
}
var defaults = {
fcgi: { fd: 0 },
http: 8080
};
var argPath = argv.path || '.';
var stats = fs.statSync(argPath);
if (stats.isFile()) {
prepare({
file: argPath,
callback: function (err, html) {
console.log(html);
}
});
} else if (stats.isDirectory()) {
process.chdir(argPath);
if (path.dirname(path.resolve(argPath)) !== 'public') {
try {
process.chdir('public');
} catch (e) {}
}
console.error('Hosting server in ' + process.cwd());
var isDirectory = function(file) {
return fs.existsSync(file) && fs.statSync(file).isDirectory();
}
var modulePaths = Module._nodeModulePaths(process.cwd()).filter(isDirectory)
.map(function(dir) {
return fs.readdirSync(dir).map(function(file) {
return path.join(path.resolve(dir, file), 'public');
}).filter(isDirectory);
}).flatten();
var paths = [process.cwd()].concat(modulePaths);
var processRequest = connect()
.use(connect.logger('combined', { stream: process.stderr }))
.use(connect.bodyParser.urlencoded({ extended: true }));
paths.forEach(function(path) {
processRequest = processRequest.use(prepare.middleware(path));
});
paths.forEach(function(path) {
processRequest = processRequest.use(connect.static(path));
});
var server, listen;
if (argv.fcgi) {
server = fcgi;
listen = typeof argv.fcgi === 'string' ? argv.fcgi : defaults.fcgi;
} else {
server = http;
listen = typeof argv.http === 'number' ? argv.http : defaults.http;
}
server.createServer(processRequest).listen(listen);
if (argv.fcgi)
console.error('FastCGI server listening on %s...', typeof listen.fd === 'number' ? 'file descriptor ' + listen.fd : 'socket ' + listen);
else
console.error('HTTP server listening on port %d...', listen);
}