-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
69 lines (53 loc) · 1.99 KB
/
app.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
express = require 'express'
path = require 'path'
compiler = require "#{__dirname}/lib/compiler"
stylus = require 'stylus'
http = require 'http'
eco = require 'eco'
fs = require 'fs'
module.exports = (options) ->
parseArgs = (args) ->
args = args[2..]
options = {}
i = 0
while i < args.length
options[args[i++]] = args[i++]
return options
copyDirStructureSync = (srcPath, destPath) ->
srcPathStat = fs.statSync(srcPath)
try
fs.mkdirSync(destPath, srcPathStat.mode)
catch err
throw err if err.code isnt 'EEXIST'
filePaths = fs.readdirSync(srcPath)
for filePath in filePaths
srcFilePath = path.join(srcPath, filePath)
fileStat = fs.statSync(srcFilePath)
if fileStat.isDirectory()
destFilePath = path.join(destPath, filePath)
copyDirStructureSync(srcFilePath, destFilePath)
options = parseArgs(process.argv)
port = options['--port'] or 8000
directory = options['--dir'] or process.cwd()
dest = options['--public'] or 'public'
src = options['--src'] or 'src'
destPath = path.resolve(directory, dest)
srcPath = path.resolve(directory, src)
unless path.existsSync(srcPath)
fs.mkdirSync(srcPath)
copyDirStructureSync(srcPath, destPath)
app = express()
app.use(express.bodyParser()) # pre-parses JSON body responses
app.use(express.errorHandler(stack: true, message: true, dump: true))
app.use(express.favicon(path.join(destPath, 'favicon.png')))
app.use(express.logger(format: '[:date] [:response-time] [:status] [:method] [:url]'))
app.use(express.static(destPath))
app.use(compiler(src: srcPath, dest: destPath, enable: ['coffeescript'])) # looks for cs files to render as js
app.use(stylus.middleware(debug: true, src: srcPath, dest: destPath))
app.get '/', (req, res) ->
res.render 'index'
app.get /^\/(.+?)\.html$/, (req, res) ->
res.render req.params[0]
server = http.createServer(app)
server.listen port, ->
console.log "Server at http://localhost:#{port}"