Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #120

Merged
merged 5 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"plenti/cmd/build"
"plenti/common"
"plenti/readers"
"time"

Expand Down Expand Up @@ -68,28 +69,28 @@ func Build() {
buildDir := setBuildDir(siteConfig)

tempBuildDir := ""
var err error
// Get theme from plenti.json.
theme := siteConfig.Theme
// If a theme is set, run the nested build.
if theme != "" {
themeOptions := siteConfig.ThemeConfig[theme]
// Recursively copy all nested themes to a temp folder for building.
tempBuildDir = build.ThemesCopy("themes/"+theme, themeOptions)
tempBuildDir, err = build.ThemesCopy("themes/"+theme, themeOptions)
common.CheckErr(err)
// Merge the current project files with the theme.
build.ThemesMerge(tempBuildDir, buildDir)
err = build.ThemesMerge(tempBuildDir, buildDir)
common.CheckErr(err)
}

// Get the full path for the build directory of the site.
buildPath := filepath.Join(".", buildDir)

// Clear out any previous build dir of the same name.
if _, buildPathExistsErr := os.Stat(buildPath); buildPathExistsErr == nil {
deleteBuildErr := os.RemoveAll(buildPath)
build.Log("Removing old '" + buildPath + "' build directory")
if deleteBuildErr != nil {
log.Fatal(deleteBuildErr)

}
common.CheckErr(os.RemoveAll(buildPath))
}

// Create the buildPath directory.
Expand All @@ -101,44 +102,52 @@ func Build() {
build.Log("Creating '" + buildDir + "' build directory")

// Add core NPM dependencies if node_module folder doesn't already exist.
build.NpmDefaults(tempBuildDir)
err = build.NpmDefaults(tempBuildDir)
common.CheckErr(err)

// Write ejectable core files to filesystem before building.
tempFiles, ejectedPath := build.EjectTemp(tempBuildDir)
tempFiles, ejectedPath, err := build.EjectTemp(tempBuildDir)
common.CheckErr(err)

// Directly copy .js that don't need compiling to the build dir.
build.EjectCopy(buildPath, tempBuildDir, ejectedPath)
if err = build.EjectCopy(buildPath, tempBuildDir, ejectedPath); err != nil {
log.Fatal(err)
}

// Bundle the JavaScript dependencies needed for the build.
//bundledContent := build.Bundle()

// Directly copy static assets to the build dir.
build.AssetsCopy(buildPath, tempBuildDir)
common.CheckErr(build.AssetsCopy(buildPath, tempBuildDir))

// Run the build.js script using user local NodeJS.
if NodeJSFlag {
clientBuildStr := build.NodeClient(buildPath)
staticBuildStr, allNodesStr := build.NodeDataSource(buildPath, siteConfig)
build.NodeExec(clientBuildStr, staticBuildStr, allNodesStr)
clientBuildStr, err := build.NodeClient(buildPath)
common.CheckErr(err)
staticBuildStr, allNodesStr, err := build.NodeDataSource(buildPath, siteConfig)
common.CheckErr(err)

common.CheckErr(build.NodeExec(clientBuildStr, staticBuildStr, allNodesStr))
} else {

// Prep the client SPA.
build.Client(buildPath, tempBuildDir, ejectedPath)

common.CheckErr(build.Client(buildPath, tempBuildDir, ejectedPath))

// Build JSON from "content/" directory.
build.DataSource(buildPath, siteConfig, tempBuildDir)
common.CheckErr(build.DataSource(buildPath, siteConfig, tempBuildDir))

}

// Run Gopack (custom Snowpack alternative) for ESM support.
build.Gopack(buildPath)
common.CheckErr(build.Gopack(buildPath))

if tempBuildDir != "" {
// If using themes, just delete the whole build folder.
build.ThemesClean(tempBuildDir)
common.CheckErr(build.ThemesClean(tempBuildDir))
} else {
// If no theme, just delete any ejectable files that the user didn't manually eject.
build.EjectClean(tempFiles, ejectedPath)
common.CheckErr(build.EjectClean(tempFiles, ejectedPath))
}

}
Expand Down
30 changes: 17 additions & 13 deletions cmd/build/assets_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)

// AssetsCopy does a direct copy of any static assets.
func AssetsCopy(buildPath string, tempBuildDir string) {
func AssetsCopy(buildPath string, tempBuildDir string) error {

defer Benchmark(time.Now(), "Copying static assets into build dir")

Expand All @@ -23,41 +22,46 @@ func AssetsCopy(buildPath string, tempBuildDir string) {

// Exit function if "assets/" directory does not exist.
if _, err := os.Stat(assetsDir); os.IsNotExist(err) {
return
return err
}

assetFilesErr := filepath.Walk(assetsDir, func(assetPath string, assetFileInfo os.FileInfo, err error) error {
err := filepath.Walk(assetsDir, func(assetPath string, assetFileInfo os.FileInfo, err error) error {
destPath := buildPath + "/" + strings.TrimPrefix(assetPath, tempBuildDir)
if assetFileInfo.IsDir() {
// Make directory if it doesn't exist.
os.MkdirAll(destPath, os.ModePerm)
// Move on to next path.
return nil
return os.MkdirAll(destPath, os.ModePerm)

}
from, err := os.Open(assetPath)
if err != nil {
fmt.Printf("Could not open asset for copying: %s\n", err)
return fmt.Errorf("Could not open asset for copying: %w", err)

}
defer from.Close()

to, err := os.Create(destPath)
if err != nil {
fmt.Printf("Could not create destination asset for copying: %s\n", err)
return fmt.Errorf("Could not create destination asset for copying: %w", err)

}
defer to.Close()

_, fileCopyErr := io.Copy(to, from)
_, err = io.Copy(to, from)
if err != nil {
fmt.Printf("Could not copy asset from source to destination: %s\n", fileCopyErr)
return fmt.Errorf("Could not copy asset from source to destination: %w", err)

}

copiedSourceCounter++
return nil
})
if assetFilesErr != nil {
fmt.Printf("Could not get asset file: %s", assetFilesErr)
if err != nil {
return fmt.Errorf("Could not get asset file: %w", err)

}

Log("Number of assets copied: " + strconv.Itoa(copiedSourceCounter))
Log(fmt.Sprintf("Number of assets copied: %d", copiedSourceCounter))
return nil

}
1 change: 1 addition & 0 deletions cmd/build/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func Bundle() []byte {
//Externals: []string{"module", "fs", "path"},
Bundle: true,
})
// error?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that probably would be a good idea, bundle.go isn't actually being used at the moment... I had left it in there thinking it could be useful for pulling in libs or bundling for production, but we could probably remove it entirely for now.

if result.Errors != nil {
fmt.Printf("Error bundling dependencies for build script: %v\n", result.Errors)
}
Expand Down
Loading