1 | coffeecup = require './coffeecup'
|
2 | fs = require 'fs'
|
3 | path = require 'path'
|
4 | log = console.log
|
5 | {OptionParser} = require 'coffee-script/lib/coffee-script/optparse'
|
6 |
|
7 | argv = process.argv[2..]
|
8 | options = null
|
9 |
|
10 | handle_error = (err) -> console.log err.stack if err
|
11 |
|
12 | watch = (files, fn) ->
|
13 | if false
|
14 | for file in files
|
15 | do ->
|
16 | currentFile = file
|
17 | fs.watch currentFile, (event) ->
|
18 | fn(currentFile) if event is 'change'
|
19 | else
|
20 | for file in files
|
21 | do ->
|
22 | currentFile = file
|
23 | fs.watchFile currentFile, {persistent: true, interval: 500}, (curr, prev) ->
|
24 | fn(currentFile) unless curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime()
|
25 |
|
26 | compile = (input_path, output_directory, js, namespace = 'templates') ->
|
27 | if Array.isArray input_path
|
28 | i = 0
|
29 | body = ''
|
30 | appendTemplate = ->
|
31 | if i >= input_path.length or not options.package
|
32 | output = """
|
33 | (function(){
|
34 | this.#{namespace} || (this.#{namespace} = {});
|
35 | #{body}
|
36 | }).call(this);
|
37 | """
|
38 | write input_path[0], namespace, output, output_directory, '.js'
|
39 | else
|
40 | fs.readFile input_path[i], 'utf-8', (err, contents) ->
|
41 | handle_error err
|
42 | name = path.basename input_path[i], path.extname(input_path[i])
|
43 | func = coffeecup.compile contents, options
|
44 | body += "this.#{namespace}[#{JSON.stringify name}] = #{func};\n"
|
45 | i += 1
|
46 | appendTemplate()
|
47 | return appendTemplate()
|
48 | fs.readFile input_path, 'utf-8', (err, contents) ->
|
49 | handle_error err
|
50 |
|
51 | name = path.basename input_path, path.extname(input_path)
|
52 |
|
53 | if not js
|
54 | output = coffeecup.render contents, options
|
55 | ext = '.html'
|
56 | else
|
57 | func = coffeecup.compile contents, options
|
58 | output = """
|
59 | (function(){
|
60 | this.#{namespace} || (this.#{namespace} = {});
|
61 | this.#{namespace}[#{JSON.stringify name}] = #{func};
|
62 | }).call(this);
|
63 | """
|
64 | ext = '.js'
|
65 |
|
66 | write input_path, name, output, output_directory, ext
|
67 |
|
68 | write = (input_path, name, contents, output_directory, ext) ->
|
69 | filename = name + ext
|
70 | dir = output_directory or path.dirname input_path
|
71 | exists = fs.exists or path.exists
|
72 | exists dir, (exists) ->
|
73 | unless exists then fs.mkdirSync dir
|
74 |
|
75 | output_path = path.join dir, filename
|
76 | contents = ' ' if contents.length <= 0
|
77 | fs.writeFile output_path, contents, (err) ->
|
78 | handle_error err
|
79 | log contents if options.print
|
80 | log "Compiled #{input_path}" if options.watch
|
81 |
|
82 | usage = '''
|
83 | Usage:
|
84 | coffeecup [options] path/to/template.coffee
|
85 | '''
|
86 |
|
87 | switches = [
|
88 | ['-j', '--js', 'compile template to js function (template + embedded renderer)']
|
89 | ['-b', '--bare', 'use with -j to compile template to js (template only)' ]
|
90 | ['-c', '--core', 'use with -j to compile renderer to js (renderer only)' ]
|
91 | ['-n', '--namespace [name]', 'global object holding the templates (default: "templates")']
|
92 | ['-w', '--watch', 'watch templates for changes, and recompile']
|
93 | ['-o', '--output [dir]', 'set the directory for compiled html']
|
94 | ['-p', '--print', 'print the compiled html to stdout']
|
95 | ['-f', '--format', 'apply line breaks and indentation to html output']
|
96 | ['-u', '--utils', 'add helper locals (currently only "render")']
|
97 | ['-z', '--optimize', 'optimize resulting JS']
|
98 | ['-v', '--version', 'display coffeecup version']
|
99 | ['-h', '--help', 'display this help message']
|
100 | ['-k', '--package', 'use with -j to include all templates in a single [namespace].js']
|
101 | ]
|
102 |
|
103 | @run = ->
|
104 | parser = new OptionParser switches, usage
|
105 | options = parser.parse argv
|
106 | args = options.arguments
|
107 | delete options.arguments
|
108 |
|
109 | log parser.help() if options.help or argv.length is 0
|
110 | log coffeecup.version if options.version
|
111 | if options.utils
|
112 | options.locals ?= {}
|
113 | options.locals.render = (file) ->
|
114 | contents = fs.readFileSync file, 'utf-8'
|
115 | coffeecup.render contents, options
|
116 |
|
117 | if args.length > 0
|
118 | files = args[..]
|
119 |
|
120 | if options.watch
|
121 | watch files, (file) ->
|
122 | if options.package and options.js
|
123 | compile files, options.output, options.js, options.namespace
|
124 | else
|
125 | compile file, options.output, options.js, options.namespace
|
126 | if options.js and options.package
|
127 | compile files, options.output, options.js, options.namespace
|
128 | else
|
129 | for file in files
|
130 | compile file, options.output, options.js, options.namespace
|
131 |
|