-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlebars-cli
executable file
·73 lines (62 loc) · 1.47 KB
/
handlebars-cli
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
#!/bin/luajit
local handlebars = require("lib.handlebars")
local cjson = require("cjson.safe")
local err_printf = require("lib.handlebars.util").err_printf
local function read_file(path)
if path == nil then return nil end
local file, data, err
file, err = io.open(path, 'r+')
if not file then
err_printf(err)
return nil
end
data, err = file:read('*all')
if not data then
err_printf(err)
return nil
end
return data
end
local arguments = {
}
for i, v in ipairs(arg) do
local m = v:match("[-][-](.*)")
if m then
arguments[m] = arg[i+1]
end
end
local helpers
if arguments.helpers then
helpers = io.open(arguments.helpers .. ".lua", 'r+')
if helpers then
helpers = require(arguments.helpers)
end
else
arguments.helpers = nil
end
local handlebarsc = handlebars.new(helpers and arguments.helpers, helpers)
local template, err = handlebarsc:compile_template_file(arguments.input)
if not template then
err_printf("Failed to generate template: %s", err)
return 1
end
local data
data = read_file(arguments.variables)
if data then
if arguments.variables:match(".*.lua") then
-- err_printf(data)
data, err = loadstring(data)
if err ~= nil then
err_printf("error: %s", err)
return nil
end
data = data()
else
data, err = cjson.decode(data)
if not data then
err_printf(err)
return
end
end
end
print(template(data))