forked from Sellsuki/kradan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkradan.js
executable file
·128 lines (113 loc) · 3.02 KB
/
kradan.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
#!/usr/bin/env node
'use strict'
var express = require('express')
var app = express()
var http = require('http').Server(app)
var io = require('socket.io')(http)
var chokidar = require('chokidar')
var helper = require('./helper.js')
var path = require('path')
var os = require('os')
var colors = require('colors/safe')
var minimist = require('minimist')
var port = minimist(process.argv.slice(2)).p || 1112
var ifaces = os.networkInterfaces()
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
})
/* port validation */
if (!Number.isInteger(port) || !(port > 1023 && port < 65535)) {
console.log(colors.error('Please specify a valid port number.'))
return
}
var data = {}
data.ls = helper.getDirJson('.')
data.list = helper.getDirList('.')
app.all('/*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'X-Requested-With')
next()
})
app.use('/files', express.static('.', {
dotfiles: 'allow',
setHeaders: function (res, path, stat) {
res.header('Content-Type', 'text/plain')
}
}))
var staticPath = path.join(__dirname, './dist')
app.use('/', express.static(staticPath))
app.get('/ls', function (req, res) {
let result = helper.getDirJson('.')
res.send(result)
})
io.on('connection', function (socket) {
io.emit('list', data.ls)
})
chokidar.watch('.', {
ignored: /[\/\\]\.|node_modules|\.git|\.DS_Store/,
ignorePermissionErrors: true,
persistent: true
}).on('all', (event, path) => {
switch (event) {
case 'unlink':
case 'unlinkDir':
data.ls = helper.getDirJson('.')
io.emit('list', data.ls)
break
case 'add':
if (!data.list.find(item => item === path)) {
data.ls = helper.getDirJson('.')
io.emit('list', data.ls)
}
break
case 'addDir':
if (!data.list.find(item => item === path + '/') && path !== '.') {
data.ls = helper.getDirJson('.')
io.emit('list', data.ls)
}
break
case 'change':
io.emit('change', '/' + path)
break
default:
console.log(event + ' : ' + path)
}
})
http.listen(port, function () {
console.log(colors.warn('Starting up kradan'))
console.log(colors.warn('Available on:'))
Object.keys(ifaces).forEach(function (dev) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4') {
console.log(colors.info(' http://' + details.address + `:${port}`))
}
})
})
console.log('Hit CTRL-C to stop the server')
})
// ----------------------------------------- //
if (process.platform === 'win32') {
require('readline').createInterface({
input: process.stdin,
output: process.stdout
}).on('SIGINT', function () {
process.emit('SIGINT')
})
}
process.on('SIGINT', function () {
console.log('\nhttp-server stopped.')
process.exit()
})
process.on('SIGTERM', function () {
console.log('\nhttp-server stopped.')
process.exit()
})