aboutsummaryrefslogtreecommitdiffstats
path: root/lib/init.server.moon
blob: 5e0cb30d6b62823c8abbcfde66b4d546feca256c (plain)
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
export MODE, warn, relative, append, on_client
MODE = 'SERVER'

-- warning messages
warn = (...) ->
  io.stderr\write table.concat { ... }, '\t'
  io.stderr\write '\n'

-- relative imports
relative = do
  _require = require

  (base, sub) ->
    sub = 0 unless 'number' == type sub

    for i=1, sub
      base = base\match '^(.*)%.%w+$'

    (name, x) ->
      name = base .. name if '.' == name\sub 1, 1
      _require name

-- shorthand to append elements to body
buffer = ''
append = (val) ->
  buffer ..= val

import compile, insert_loader from require 'duct_tape'
insert_loader!

on_client = (fn, ...) ->
  args = {...}
  -- warn code
  append "<script type=\"application/lua\">
    local fn = #{compile fn}
    fn(#{table.concat [string.format '%q', v for v in *args ], ', '})
  </script>"

{
  -- access / flush the appended data
  flush: ->
    with x = buffer
      buffer = ''
}