Skip to content

Commit

Permalink
Build client in walk through files to elimate extra loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Apr 18, 2020
1 parent abb191e commit 93294ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ you need to deploy for your website.`,
}

// Build JSON from "content/" directory.
//nodesList := build.DataSource(buildPath)
nodesList := build.DataSource(buildPath)

// Build the client SPA.
build.Client(buildPath)

// Build the static HTML.
//build.Static(nodesList)
build.Static(nodesList)

},
}
Expand Down
32 changes: 13 additions & 19 deletions cmd/build/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ import (

// Client builds the SPA.
func Client(buildPath string) {
// Create list of all file paths in the "/layout" folder.
var layoutFiles []string
layoutFilesErr := filepath.Walk("layout", func(path string, info os.FileInfo, err error) error {
layoutFiles = append(layoutFiles, path)
return nil
})
if layoutFilesErr != nil {
fmt.Printf("Could not get layout file: %s", layoutFilesErr)
}

stylePath := buildPath + "/spa/bundle.css"
// Clear out any previous CSS.
Expand All @@ -33,12 +24,12 @@ func Client(buildPath string) {
}
}

for _, layoutFile := range layoutFiles {
// 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(layoutFile, "layout", "/spa", 1)
destFile := buildPath + strings.Replace(layoutPath, "layout", "/spa", 1)
// Make sure path is a directory
fileInfo, _ := os.Stat(layoutFile)
if fileInfo.IsDir() {
if layoutFileInfo.IsDir() {
// Create any sub directories need for filepath.
os.MkdirAll(destFile, os.ModePerm)
}
Expand All @@ -51,13 +42,13 @@ func Client(buildPath string) {
// Check if the current file is in the excluded list.
excluded := false
for _, excludedFile := range excludedFiles {
if excludedFile == layoutFile {
if excludedFile == layoutPath {
excluded = true
}
}
// If the file is already in .js format just copy it straight over to build dir.
if filepath.Ext(layoutFile) == ".js" && !excluded {
from, err := os.Open(layoutFile)
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)
}
Expand All @@ -75,8 +66,8 @@ func Client(buildPath string) {
}
}
// If the file is in .svelte format, compile it to .js
if filepath.Ext(layoutFile) == ".svelte" {
fileContentByte, readFileErr := ioutil.ReadFile(layoutFile)
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)
}
Expand Down Expand Up @@ -118,7 +109,10 @@ func Client(buildPath string) {
}
}
}

return nil
})
if layoutFilesErr != nil {
fmt.Printf("Could not get layout file: %s", layoutFilesErr)
}

fmt.Println("Running snowpack to build dependencies for esm support")
Expand Down

0 comments on commit 93294ce

Please sign in to comment.