This repository was archived by the owner on Jan 14, 2023. It is now read-only.
forked from benekastah/templito
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplito-cli.coffee
129 lines (119 loc) · 3.79 KB
/
templito-cli.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
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
129
path = require 'path'
optimist = require 'optimist'
watch = require 'node-watch'
log = require('./utilities').log
templito = require './'
# TODO remove optimist. it's deprecated
argv = require('optimist')
.usage('Compiles underscore.js templates into javascript files.\n\n' +
'templito source-dir out-dir [options]')
.options(
c:
alias: 'compile-style'
describe: 'Options include: "combined" (single file), "directory" (one ' +
'file per directory) and "file" (one output file per input ' +
'file).'
default: 'directory'
p:
alias: "path-case"
describe: 'The casing for the object path part of an output ' +
"function's address. If the template is " +
'source_dir/a/b/c.html, then the object path part is ' +
'source_dir/a/b. Options include "camelCase", "CapitalCase", ' +
' and "snake_case".'
default: 'CapitalCase'
f:
alias: 'function-case'
describe: 'The casing for the output function\'s name. Options are the ' +
'same as for the path-case option.'
default: 'camelCase'
e:
alias: 'extension'
describe: 'templito will look for files with the given extension.'
default: '.html'
k:
alias: 'keep-extension'
describe: 'Whether or not the output files should keep the original ' +
'file extension as part of its name.'
default: false
n:
alias: 'namespace'
describe: 'The namespace to add your compiled template functions to.'
default: 'App'
s:
alias: 'template-settings'
describe: 'A javascript object that will override _.templateSettings.'
h:
alias: 'help'
describe: 'Show this help message and exit.'
w:
alias: 'watch'
describe: 'Watch the source directory for changes and recompile the ' +
'templates when a change is detected.'
C:
alias: 'clean'
describe: 'Empty the out-dir before compiling.'
U:
alias: 'unsafe-clean'
describe: 'Opt out of prompt before cleaning out-dir'
d:
alias: 'template-wrapper'
describe: 'Provide a function to pass the compiled template to. ' +
'Useful if you need to process the result of an underscore ' +
'template function call for any reason.'
F:
alias: 'footer'
describe: 'A file to append as a footer'
H:
alias: 'header'
describe: 'A file to append as a header'
).argv
show_help = (msg) ->
optimist.showHelp()
console.error msg if msg
process.exit msg ? -1 : 0
if argv.help
show_help()
# Underscorify hyphenated keys
re_hyphen = /(\w*)\-(\w*)/g
for key, value of argv
if re_hyphen.test key
_key = key.replace re_hyphen, '$1_$2'
argv[_key] = value
if argv.template_settings?
try
argv.template_settings = eval("(#{argv.template_settings});")
catch e
console.error "The template-settings you passed in " +
"(#{argv.template_settings}) does not appear to be a " +
"valid javascript object: #{e}"
process.exit -1
argv.source_dir = argv._[0]
argv.out_dir = argv._[1]
if not (argv.source_dir and argv.out_dir)
show_help 'Missing required argument'
argv.source_dir_basename = path.basename argv.source_dir
timeout = null
timeout_duration = 500
compile = ->
log 'Trying to compile templates...'
templito.compile argv
.then () ->
log 'Done.'
.catch (err) ->
log err
if err.stack
log err.stack
throw err
compile()
timeout_compile = ->
clearTimeout timeout
log "Compiling in #{timeout_duration}ms..."
timeout = setTimeout compile, timeout_duration
if argv.watch
watch argv.source_dir, (filename) ->
out_dir_match = filename.match argv.out_dir
if not out_dir_match or out_dir_match.index isnt 0
console.log()
log "Detected a change in #{JSON.stringify filename}"
timeout_compile()