-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.co
executable file
·56 lines (48 loc) · 1.6 KB
/
cli.co
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
#!/usr/bin/env coco
#### `ckup` command line interface
ckup = require \ckup
o = require(\coco/optparse) do
print : 'write to stdout instead of .htm'
stdin : 'read stdin, then write to the given file or stdout'
eval : 'read arguments as source'
watch : 'watch and rerender on changes'
css : 'render as ccss'
version : 'show version'
help : 'show me'
v! if o.version
h! if o.help
files = o.$args
fs = require \fs
let
return listen! if o.stdin
return render files.join \\n if o.eval
for file of files
render fs.readFileSync(file, \utf8), file
watch file if o.watch
return if file or o.version or o.help
h v!
console.log '\nlistening to stdin...'
listen!
function render code, name, asis
kup = ckup.render if o.css then '<- (~> @_ = css it)\n' + code else code
return console.log \%s kup if not name or o.print
unless asis
if /\.\w+\.co$/itest name
# index.html.co => index.html
then name.=slice 0 -3
# doc.co => doc.htm
else name.=replace /(?:\.\w+)?$/ (if o.css then \.css else \.htm)
fs.writeFileSync name, kup
require(\util)log name
function listen
code = ''
stdin = process.openStdin!
stdin.on \data -> code += it
stdin.on \end -> render code, files.0, true
function watch file
curr, prev <- fs.watchFile file, {+persistent, interval: 500ms}
return if curr.size is prev.size and +curr.mtime is +prev.mtime
try render fs.readFileSync(file, \utf8), file
catch then console.error e?.stack or e
function v then console.log 'ckup ' + ckup.VERSION
function h then console.log 'Usage: ckup [options] [files]\n%s' o