-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgritty.js
124 lines (96 loc) · 2.28 KB
/
gritty.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
#!/usr/bin/env node
'use strict';
const args = require('yargs-parser')(process.argv.slice(2), {
boolean: [
'version',
'help',
'auto-restart',
'path',
],
number: [
'port',
],
string: [
'command',
'html-path',
],
alias: {
help: 'h',
version: 'v',
},
default: {
'port': process.env.PORT | 1337,
'auto-restart': true,
},
});
const getMessage = (a) => a.message;
main(args);
function main() {
if (args.help)
return help();
if (args.version)
return version();
if (args.path)
return path();
start({
port: args.port,
command: args.command,
htmlPath: args.htmlPath,
autoRestart: args.autoRestart,
});
}
function path() {
const {join} = require('path');
console.log(join(__dirname, '..'));
}
function start(options) {
const squad = require('squad');
const {
port,
command,
autoRestart,
htmlPath,
} = options;
check(port);
const DIR = htmlPath || __dirname + '/../';
console.log(DIR);
const gritty = require('../');
const http = require('http');
const express = require('express');
const io = require('socket.io');
const app = express();
const server = http.createServer(app);
const ip = process.env.IP || /* c9 */
'0.0.0.0';
app.use(gritty())
.use(express.static(DIR));
const socket = io.listen(server);
gritty.listen(socket, {
command,
autoRestart,
});
server.listen(port, ip)
.on('error', squad(exit, getMessage));
console.log(`url: http://localhost:${port}`);
}
function help() {
const bin = require('../help');
const usage = 'Usage: gritty [options]';
console.log(usage);
console.log('Options:');
for (const name of Object.keys(bin)) {
console.log(' %s %s', name, bin[name]);
}
}
function version() {
const pack = require('../package');
console.log('v' + pack.version);
}
function check(port) {
if (isNaN(port))
exit('port should be a number 0..65535');
}
function exit(msg) {
console.error(msg);
process.exit(-1);
}