1 | CoffeeScript = require 'coffee-script'
|
2 | HamlCoffee = require '../haml-coffee'
|
3 | fs = require 'fs'
|
4 |
|
5 | red = '\u001b[31m'
|
6 | reset = '\u001b[0m'
|
7 |
|
8 | module.exports = class CoffeeMaker
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
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 |
|
29 | templateName = filename.match(/([^\.]+)(\.html)?\.haml[c]?$/)?[1] unless templateName
|
30 |
|
31 |
|
32 |
|
33 |
|
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 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | @compile = (source, templateName, namespace = null, compilerOptions = {}) ->
|
71 | output = ''
|
72 |
|
73 |
|
74 |
|
75 |
|
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
|