-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
128 lines (97 loc) · 3.18 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
/**
* @author Stanislav Kalashnik <[email protected]>
* @license GNU GENERAL PUBLIC LICENSE Version 3
*/
'use strict';
const
name = 'static',
log = require('runner-logger').wrap(name);
function start ( config, done ) {
const
files = new (require('node-static').Server)(config.path, config.staticOptions),
server = require('http').createServer(function ( request, response ) {
request.addListener('end', function () {
// static files
files.serve(request, response, function ( error ) {
const
address = request.connection.remoteAddress || log.colors.red('[0.0.0.0]'),
status = response.statusCode === 200 ? log.colors.green(response.statusCode) : log.colors.yellow(response.statusCode);
if ( error ) {
response.end();
}
log[error ? 'fail' : 'info'](
'%s\t%s\t%s\t%s',
address.replace('::ffff:', ''),
request.method,
error ? log.colors.red(error.status) : status,
request.url.replace(/\//g, log.colors.grey('/'))
);
});
}).resume();
});
server.on('listening', function () {
// port can be 0 from the start
config.port = server.address().port;
config.uri = 'http://' + require('ip').address() + ':' + config.port + '/' + config.open;
log.info('serving directory: ' + log.colors.bold(config.path));
log.info('web address: ' + log.colors.green.bold(config.uri));
if ( typeof config.onReady === 'function' ) {
config.onReady(config);
}
});
server.on('close', done);
server.on('error', function ( error ) {
log.fail(error.toString());
done();
});
server.listen(config.port);
return server;
}
function stop ( server ) {
if ( server ) {
server.close();
}
}
function generator ( config = {}, options = {} ) {
const
tasks = {},
path = require('path'),
open = require('opn');
let server;
// sanitize and extend defaults
config = Object.assign({
path: path.resolve(config.path || '.'),
open: '',
port: 8080,
onReady: null,
staticOptions: {
cache: false
}
}, config);
// sanitize and extend defaults
options = Object.assign({}, {
prefix: name + ':',
suffix: ''
}, options);
tasks[options.prefix + 'config' + options.suffix] = function () {
log.inspect(config, log);
};
tasks[options.prefix + 'start' + options.suffix] = function ( done ) {
server = start(config, done);
};
tasks[options.prefix + 'open' + options.suffix] = function () {
open(config.uri);
};
tasks[options.prefix + 'stop' + options.suffix] = function () {
stop(server);
server = null;
};
return tasks;
}
// export main actions
generator.methods = {
start: start,
stop: stop
};
// public
module.exports = generator;