-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
client.go
132 lines (115 loc) · 4.09 KB
/
client.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
package build
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Client builds the SPA.
func Client(buildPath string) {
fmt.Println("\nBuilding client SPA using svelte compiler")
stylePath := buildPath + "/spa/bundle.css"
// Clear out any previous CSS.
if _, stylePathExistsErr := os.Stat(stylePath); stylePathExistsErr == nil {
deleteStyleErr := os.Remove(stylePath)
if deleteStyleErr != nil {
fmt.Println(deleteStyleErr)
return
}
}
copiedSourceCounter := 0
compiledComponentCounter := 0
// Go through all file paths in the "/layout" folder.
layoutFilesErr := filepath.Walk("layout", func(layoutPath string, layoutFileInfo os.FileInfo, err error) error {
// Create destination path.
destFile := buildPath + strings.Replace(layoutPath, "layout", "/spa", 1)
// Make sure path is a directory
if layoutFileInfo.IsDir() {
// Create any sub directories need for filepath.
os.MkdirAll(destFile, os.ModePerm)
}
// Make list of files not to copy to build.
excludedFiles := []string{
"layout/ejected/build_client.js",
"layout/ejected/build_static.js",
"layout/ejected/server_router.js",
}
// Check if the current file is in the excluded list.
excluded := false
for _, excludedFile := range excludedFiles {
if excludedFile == layoutPath {
excluded = true
}
}
// If the file is already in .js format just copy it straight over to build dir.
if filepath.Ext(layoutPath) == ".js" && !excluded {
from, err := os.Open(layoutPath)
if err != nil {
fmt.Printf("Could not open source .js file for copying: %s\n", err)
}
defer from.Close()
to, err := os.Create(destFile)
if err != nil {
fmt.Printf("Could not create destination .js file for copying: %s\n", err)
}
defer to.Close()
_, fileCopyErr := io.Copy(to, from)
if err != nil {
fmt.Printf("Could not copy .js from source to destination: %s\n", fileCopyErr)
}
copiedSourceCounter++
}
// If the file is in .svelte format, compile it to .js
if filepath.Ext(layoutPath) == ".svelte" {
fileContentByte, readFileErr := ioutil.ReadFile(layoutPath)
if readFileErr != nil {
fmt.Printf("Could not read contents of svelte source file: %s\n", readFileErr)
}
fileContentStr := string(fileContentByte)
// Execute node script to compile .svelte to .js
compiledBytes, buildErr := exec.Command("node", "layout/ejected/build_client.js", fileContentStr).Output()
if buildErr != nil {
fmt.Printf("Could not compile svelte to JS: %s\n", buildErr)
}
compiledStr := string(compiledBytes)
compiledStrArray := strings.Split(compiledStr, "!plenti-split!")
// Get the JS only from the script output.
jsStr := strings.TrimSpace(compiledStrArray[0])
// Convert file extensions to be snowpack friendly.
jsStr = strings.Replace(jsStr, ".svelte", ".js", -1)
jsStr = strings.Replace(jsStr, "from \"svelte/internal\";", "from \"../web_modules/svelte/internal/index.js\";", -1)
jsStr = strings.Replace(jsStr, "from \"navaid\";", "from \"../web_modules/navaid.js\";", -1)
// Write compiled .js to build directory.
jsBytes := []byte(jsStr)
destFile = strings.TrimSuffix(destFile, filepath.Ext(destFile)) + ".js"
err := ioutil.WriteFile(destFile, jsBytes, 0755)
if err != nil {
fmt.Printf("Unable to write file: %v", err)
}
// Get the CSS only from the script output.
cssStr := strings.TrimSpace(compiledStrArray[1])
// If there is CSS, write it into the bundle.css file.
if cssStr != "null" {
cssFile, WriteStyleErr := os.OpenFile(stylePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if WriteStyleErr != nil {
fmt.Printf("Could not open bundle.css for writing: %s", WriteStyleErr)
}
defer cssFile.Close()
if _, err := cssFile.WriteString(cssStr); err != nil {
log.Println(err)
}
}
compiledComponentCounter++
}
return nil
})
if layoutFilesErr != nil {
fmt.Printf("Could not get layout file: %s", layoutFilesErr)
}
fmt.Printf("Number of source files copied: %d\n", copiedSourceCounter)
fmt.Printf("Number of components compiled: %d\n", compiledComponentCounter)
}