-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (57 loc) · 1.77 KB
/
main.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
package formatcss
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
flag "github.com/ogier/pflag"
)
var replaceFlag, removeNestedCalcFlag, minFlag bool
func main() {
flag.BoolVarP(&replaceFlag, "replace", "r", false, "Find and Replace CSS Variables with their value")
flag.BoolVarP(&removeNestedCalcFlag, "nested-calc", "n", false, "Find and remove nested CSS 'calc()' expressions so 'calc' is only called once")
flag.BoolVarP(&minFlag, "minimize", "m", false, "Minimize CSS file")
flag.Parse()
if len(flag.Args()) < 1 {
fmt.Fprintf(os.Stderr, "Error: You must provide a file path to process\n")
fmt.Fprintf(os.Stdout, "%s foo.css\n", os.Args[0])
flag.PrintDefaults()
return
}
inputPath := flag.Args()[0]
cssByte, err := ioutil.ReadFile(inputPath)
if err != nil {
log.Panic(err)
}
css := string(cssByte)
if replaceFlag { // Remove and replace CSS variables
css = RemoveCSSVariables(css)
if removeNestedCalcFlag {
css = RemoveNestedCalc(css)
}
if minFlag { // Minimize CSS file
css = Minimize(css)
}
// Setup output file path
outputDir := filepath.Dir(inputPath)
inputFile := filepath.Base(inputPath)
outputFile := inputFile[0:len(inputFile)-3] + "min.css"
output := filepath.Join(outputDir, outputFile)
// Write CSS to file
ioutil.WriteFile(output, []byte(css), 0644)
} else if minFlag { // Minimize CSS file
css = Minimize(css)
// Setup output file path
outputDir := filepath.Dir(inputPath)
inputFile := filepath.Base(inputPath)
outputFile := inputFile[0:len(inputFile)-3] + "min.css"
output := filepath.Join(outputDir, outputFile)
// Write CSS to file
ioutil.WriteFile(output, []byte(css), 0644)
} else { // Format CSS file
css = FormatCSS(css)
// Write CSS to file
ioutil.WriteFile(inputPath, []byte(css), 0644)
}
}