UNPKG

3.42 kBtext/coffeescriptView Raw
1CoffeeScript = require 'coffee-script'
2HamlCoffee = require '../haml-coffee'
3fs = require 'fs'
4
5red = '\u001b[31m'
6reset = '\u001b[0m'
7
8module.exports = class CoffeeMaker
9
10 # Compiles a Haml-Coffee file to a JavaScript template.
11 # When the output template is omitted, it will be derived from the file name.
12 #
13 # @param [String] filename the Haml coffee file to compile
14 # @param [Object] compilerOptions the compiler options
15 # @param [String] namespace the template namespace
16 # @param [String] templateName the name of the output template
17 #
18 @compileFile = (filename, compilerOptions = {}, namespace = null, templateName = null) ->
19 output = ''
20
21 try
22 source = fs.readFileSync(filename).toString()
23 catch error
24 console.error " #{ red }Error opening file:#{ reset } %s", error
25 console.error error
26
27 try
28 # Derive template name from filename by remove .html and .haml
29 templateName = filename.match(/([^\.]+)(\.html)?\.haml[c]?$/)?[1] unless templateName
30
31 # Extend the options with the name and namespace so that the
32 # compiler has these configuration properties from the beginning
33 # and that the API for this method can stay the same.
34 compilerOptions.namespace = namespace
35 compilerOptions.name = templateName
36
37 if templateName
38 compiler = new HamlCoffee compilerOptions
39 compiler.parse source
40
41 haml = compiler.render()
42
43 else
44 console.error " #{ red }[haml coffee] no valid Haml extension.#{ reset }"
45 process.exit 1
46
47 catch error
48 console.error " #{ red }[haml coffee] error compiling Haml file:#{ reset } %s", error
49 console.error error.stack
50 process.exit 1
51
52 try
53 output = CoffeeScript.compile haml
54
55 catch error
56 console.error ' #{ red }[haml coffee] CoffeeScript compilation error:#{ reset } %s', error
57 console.error error.stack
58 process.exit 1
59
60 output
61
62 # Compiles a Haml-Coffee file to a JavaScript template.
63 # When the output template is omitted, it will be derived from the file name.
64 #
65 # @param [String] source the template source code
66 # @param [String] templateName the name of the output template
67 # @param [String] namespace the template namespace
68 # @param [Object] compilerOptions the compiler options
69 #
70 @compile = (source, templateName, namespace = null, compilerOptions = {}) ->
71 output = ''
72
73 # Extend the options with the name and namespace so that the
74 # compiler has these configuration properties from the beginning
75 # and that the API for this method can stay the same.
76 compilerOptions.namespace = namespace
77 compilerOptions.name = templateName
78
79 try
80 if templateName || compilerOptions.placement is 'amd'
81 compiler = new HamlCoffee compilerOptions
82 compiler.parse source
83 haml = compiler.render templateName, namespace
84
85 else
86 console.error " #{ red }[haml coffee] no template name given.#{ reset }"
87 process.exit 1
88
89 catch error
90 console.error " #{ red }[haml coffee] error compiling Haml file:#{ reset } %s", error
91 console.error error.stack
92 process.exit 1
93
94 try
95 output = CoffeeScript.compile haml
96
97 catch error
98 console.error ' #{ red }[haml coffee] CoffeeScript compilation error:#{ reset } %s', error
99 console.error error.stack
100 process.exit 1
101
102 output