-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss.go
More file actions
62 lines (50 loc) · 1.05 KB
/
Copy pathcss.go
File metadata and controls
62 lines (50 loc) · 1.05 KB
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
package packer
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type CssCompiler struct {
options CssOptions
}
type CssOptions struct {
Files []string `json:"files"`
Output string `json:"output"`
}
func NewCssCompiler(options CssOptions) *CssCompiler {
return &CssCompiler{options: options}
}
func (c *CssCompiler) Run(path string) error {
if filepath.Ext(path) != ".css" {
return nil
}
styleSheets, err := c.compile()
if err != nil {
return err
}
return c.saveOutput(styleSheets)
}
func (c *CssCompiler) compile() (string, error) {
var sb strings.Builder
for _, fp := range c.options.Files {
files, err := getFilesFromPath(fp)
if err != nil {
return "", err
}
for _, f := range files {
b, err := ioutil.ReadFile(f)
if err != nil {
return "", err
}
sb.Write(b)
}
}
return sb.String(), nil
}
func (c *CssCompiler) saveOutput(s string) error {
if err := os.MkdirAll(filepath.Dir(c.options.Output), os.ModePerm); err != nil {
return err
}
return ioutil.WriteFile(c.options.Output, []byte(s), 0644)
}