UNPKG

3.01 kBtext/coffeescriptView Raw
1coffeecup = require './coffeecup'
2fs = require 'fs'
3path = require 'path'
4log = console.log
5{OptionParser} = require 'coffee-script/lib/coffee-script/optparse'
6
7argv = process.argv[2..]
8options = null
9
10handle_error = (err) -> console.log err.stack if err
11
12compile = (input_path, output_directory, js, namespace = 'templates') ->
13 fs.readFile input_path, 'utf-8', (err, contents) ->
14 handle_error err
15
16 name = path.basename input_path, path.extname(input_path)
17
18 if not js
19 output = coffeecup.render contents, options
20 ext = '.html'
21 else
22 func = coffeecup.compile contents, options
23 output = """
24 (function(){
25 this.#{namespace} || (this.#{namespace} = {});
26 this.#{namespace}[#{JSON.stringify name}] = #{func};
27 }).call(this);
28 """
29 ext = '.js'
30
31 write input_path, name, output, output_directory, ext
32
33write = (input_path, name, contents, output_directory, ext) ->
34 filename = name + ext
35 dir = output_directory or path.dirname input_path
36 path.exists dir, (exists) ->
37 unless exists then fs.mkdirSync dir
38
39 output_path = path.join dir, filename
40 contents = ' ' if contents.length <= 0
41 fs.writeFile output_path, contents, (err) ->
42 handle_error err
43 log contents if options.print
44 log "Compiled #{input_path}" if options.watch
45
46usage = '''
47 Usage:
48 coffeecup [options] path/to/template.coffee
49'''
50
51switches = [
52 ['-j', '--js', 'compile template to js function (template + embedded renderer)']
53 ['-b', '--bare', 'use with -j to compile template to js (template only)' ]
54 ['-c', '--core', 'use with -j to compile renderer to js (renderer only)' ]
55 ['-n', '--namespace [name]', 'global object holding the templates (default: "templates")']
56 ['-w', '--watch', 'watch templates for changes, and recompile']
57 ['-o', '--output [dir]', 'set the directory for compiled html']
58 ['-p', '--print', 'print the compiled html to stdout']
59 ['-f', '--format', 'apply line breaks and indentation to html output']
60 ['-u', '--utils', 'add helper locals (currently only "render")']
61 ['-z', '--optimize', 'optimize resulting JS']
62 ['-v', '--version', 'display coffeecup version']
63 ['-h', '--help', 'display this help message']
64]
65
66@run = ->
67 parser = new OptionParser switches, usage
68 options = parser.parse argv
69 args = options.arguments
70 delete options.arguments
71
72 log parser.help() if options.help or argv.length is 0
73 log coffeecup.version if options.version
74 if options.utils
75 options.locals ?= {}
76 options.locals.render = (file) ->
77 contents = fs.readFileSync file, 'utf-8'
78 coffeecup.render contents, options
79
80 if args.length > 0
81 file = args[0]
82
83 if options.watch
84 fs.watchFile file, {persistent: true, interval: 500}, (curr, prev) ->
85 return if curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime()
86 compile file, options.output, options.js, options.namespace
87
88 compile file, options.output, options.js, options.namespace
89