-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.coffee
executable file
·102 lines (86 loc) · 2.85 KB
/
server.coffee
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
#!/usr/bin/env coffee
express = require 'express'
request = require 'request'
watchr = require 'watchr'
exec = require('child_process').exec
proxy = require('http-proxy').createProxyServer()
argv = require 'optimist'
.usage 'Starts a local dev server that proxies DODP calls'
.demand 'r'
.alias 'r', 'remote-host'
.describe 'r', 'Proxy /DodpMobile, /DodpFiles and /CatalogSearch to this url'
.default 'r', 'http://m.e17.dk/'
.alias 't', 'target'
.describe 't', 'Target a specific skin folder'
.alias 'q', 'quiet'
.describe 'q', 'Be quieter please'
.alias 's', 'silence'
.describe 's', 'Silence please'
.alias 'p', 'port'
.describe 'p', 'Start the server at this port'
.default 'p', 8080
.argv
proxy.off( 'error' ).on 'error', (e) ->
unless argv.quiet or argv.silence
if e.code is 'ECONNRESET'
console.log 'The client reset connect'
else
console.log 'proxy error:', e
app = express()
app.use require('body-parser').urlencoded
extended: false
app.use require('morgan')("combined") if not (argv.quiet or argv.silence)
app
.use express.static( process.cwd() + '/build' )
.use (req, res, next) ->
if req.url.match( /^\/(Dodp(Mobile|Files)|CatalogSearch|dodServices)/ )
proxy.proxyRequest req, res, target: argv['remote-host']
else if req.url.match( /^\/proxyURL/ )
# Proxy request with data *and* headers forth and back
url = req.url.match /^\/proxyURL\?url=(.*)/
request( url: url[1], headers: req.headers ).pipe res
else if req.url.match /\.buildnumber$/
tries = 0
delay = 10
maxTries = 60*1000/delay
old_buildnumber = buildnumber
interval = setInterval(
->
tries += 1
if old_buildnumber isnt buildnumber or tries >= maxTries
res.write "" + buildnumber
res.end()
clearInterval interval
delay
)
else if req.url.match /test\/results/
console.log req.body
res.write "ok"
res.end()
else
next()
buildSource = (cb) ->
skinArg = if argv.target then "-s #{ argv.target }" else ""
exec "cake -dnt #{ skinArg } app", cb
buildSource -> console.log 'Finished build' if not argv.silence
server = app.listen argv.port, ->
if not argv.silence
console.log 'Listening on port %d', server.address().port
changedTimeout = null
buildnumber = 1
fileChanged = (filePath) ->
clearTimeout( changedTimeout ) if changedTimeout
if not argv.silence
console.log 'Rebuild after change to ' + filePath
changedTimeout = setTimeout( ->
buildSource ->
console.log 'Finished rebuild' if not argv.silence
buildnumber++
, 100
)
watchr.watch
paths: [ 'html', 'src', 'scss' ],
listeners:
change: ( changeType, filePath, fileCurrentStat, filePreviousStat ) ->
return if filePath.match(/skinconfig/i) or filePath.match(/_overrides/i)
fileChanged filePath