Skip to content

Commit

Permalink
Optional static folder (#226).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Jan 7, 2023
1 parent 085e244 commit c405688
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cmd/build/media_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func copyMediaFromTheme(mediaDir string, buildPath string, index []string, copie
func copyMediaFromProject(mediaDir string, buildPath string, index []string, copiedSourceCounter int) ([]string, int, error) {

if _, err := os.Stat(mediaDir); err == nil {
// Media folder exists, loop through contents
// the "media" folder exists, loop through contents
err := filepath.WalkDir(mediaDir, func(mediaPath string, mediaFileInfo fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("can't stat %s: %w", mediaPath, err)
Expand Down
68 changes: 33 additions & 35 deletions cmd/build/static_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,50 +84,48 @@ func copyStaticFilesFromTheme(staticDir string, buildPath string, copiedSourceCo

func copyStaticFilesFromProject(staticDir string, buildPath string, copiedSourceCounter int) (int, error) {

// Exit function if "static/" directory does not exist.
if _, err := os.Stat(staticDir); os.IsNotExist(err) {
return 0, fmt.Errorf("%s driectory does not exist: %w", staticDir, err)
}
if _, err := os.Stat(staticDir); err == nil {
// The "static" folder exists, loop through contents
err := filepath.WalkDir(staticDir, func(staticPath string, staticFileInfo fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("\ncan't stat %s: %w", staticPath, err)
}
destPath := buildPath + "/" + strings.TrimPrefix(staticPath, staticDir)
if staticFileInfo.IsDir() {
// Make directory if it doesn't exist.
// Move on to next path.
if err = os.MkdirAll(destPath, os.ModePerm); err != nil {
return fmt.Errorf("\ncannot create static dir %s: %w", staticPath, err)
}
return nil

err := filepath.WalkDir(staticDir, func(staticPath string, staticFileInfo fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("\ncan't stat %s: %w", staticPath, err)
}
destPath := buildPath + "/" + strings.TrimPrefix(staticPath, staticDir)
if staticFileInfo.IsDir() {
// Make directory if it doesn't exist.
// Move on to next path.
if err = os.MkdirAll(destPath, os.ModePerm); err != nil {
return fmt.Errorf("\ncannot create static dir %s: %w", staticPath, err)
}
return nil
from, err := os.Open(staticPath)
if err != nil {
return fmt.Errorf("\nCould not open static file \"%s\" for copying: %w\n", staticPath, err)

}
from, err := os.Open(staticPath)
if err != nil {
return fmt.Errorf("\nCould not open static file \"%s\" for copying: %w\n", staticPath, err)
}
defer from.Close()

}
defer from.Close()
to, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("\nCould not create destination static file \"%s\" for copying: %w\n", destPath, err)

to, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("\nCould not create destination static file \"%s\" for copying: %w\n", destPath, err)
}
defer to.Close()

}
defer to.Close()
_, err = io.Copy(to, from)
if err != nil {
return fmt.Errorf("\nCould not copy static file from source \"%s\" to destination: %w\n", staticPath, err)

_, err = io.Copy(to, from)
if err != nil {
return fmt.Errorf("\nCould not copy static file from source \"%s\" to destination: %w\n", staticPath, err)
}

copiedSourceCounter++
return nil
})
if err != nil {
return 0, fmt.Errorf("\nCould not get static file: %w\n", err)
}

copiedSourceCounter++
return nil
})
if err != nil {
return 0, fmt.Errorf("\nCould not get static file: %w\n", err)
}

return copiedSourceCounter, nil
Expand Down

0 comments on commit c405688

Please sign in to comment.