-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathless.go
159 lines (140 loc) · 3.04 KB
/
less.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package less
import (
"encoding/json"
"errors"
"gopkg.in/olebedev/go-duktape.v2"
"io/ioutil"
"os"
)
var (
ctx *duktape.Context
r Reader
w Writer
)
type Reader interface {
ReadFile(string) ([]byte, error)
}
type Writer interface {
WriteFile(string, []byte, os.FileMode) error
}
type reader struct{}
func (reader) ReadFile(path string) ([]byte, error) {
return ioutil.ReadFile(path)
}
type writer struct{}
func (writer) WriteFile(path string, data []byte, mode os.FileMode) error {
return ioutil.WriteFile(path, data, mode)
}
func readFile(c *duktape.Context) int {
var path = c.SafeToString(-1)
if path == "" {
return 0
}
bytes, err := r.ReadFile(path)
if err != nil {
bytes, err = Asset(path)
if err != nil {
return 0
}
}
c.PushString(string(bytes))
return 1
}
func readFileFromAssets(c *duktape.Context) int {
var path = c.SafeToString(-1)
if path == "" {
return 0
}
bytes, err := Asset(path)
if err != nil {
return 0
}
c.PushString(string(bytes))
return 1
}
func writeFile(c *duktape.Context) int {
var data = []byte(c.SafeToString(-1))
var path = c.SafeToString(-2)
if path == "" {
return 0
}
err := w.WriteFile(path, data, 0644)
if err != nil {
return 0
}
return 1
}
func SetReader(customReader Reader) {
r = customReader
}
func SetWriter(customWriter Writer) {
w = customWriter
}
func RenderFile(input, output string, mods ...map[string]interface{}) error {
if input == "" {
return errors.New("No input path provided")
}
if output == "" {
output = input + ".css"
}
var options = map[string]interface{}{}
if len(mods) > 0 {
options = mods[0]
}
options["filename"] = input
encodedOptions, err := json.Marshal(options)
if err != nil {
return err
}
ctx.EvalString(`
try {
var fs = require('./assets/less-go/fs');
var less = require('./assets/less-go');
var data = fs.readFileSync("` + input + `");
print('Rendering less: ', data.slice(0,50) + '...');
less.render(data, ` + string(encodedOptions) + `, function (e, output) {
if (e == null) {
print("Rendered");
writeFile("` + output + `", output.css);
} else {
print('Render error', e.stack);
e.stack
}
});
} catch (e) {
print("ERROR!", e.stack);
e.stack
}
`)
result := ctx.GetString(-1)
ctx.Pop()
if result != "" {
return errors.New(result)
}
return nil
}
func init() {
r = reader{}
w = writer{}
ctx = duktape.New()
ctx.PushGlobalGoFunction("readFile", readFile)
ctx.PushGlobalGoFunction("readFileFromAssets", readFileFromAssets)
ctx.PushGlobalGoFunction("writeFile", writeFile)
ctx.EvalString(`
Duktape.modSearch = function (id, require, exports, module) {
id = id.replace(/\.js$/, "");
var res = readFileFromAssets(id + ".js");
if (typeof res === 'string') {
return res;
}
var res = readFileFromAssets(id + "/index.js");
if (typeof res === 'string') {
return 'module.exports = require("' + id + '/index.js")';
}
throw new Error('module not found: ' + id);
};
`)
// result := ctx.GetString(-1)
// ctx.Pop()
// fmt.Println("result is:", result)
}