Skip to content

Commit

Permalink
live reload
Browse files Browse the repository at this point in the history
  • Loading branch information
padraicbc committed Feb 17, 2021
1 parent 13e13af commit dc825c1
Show file tree
Hide file tree
Showing 24 changed files with 550 additions and 171 deletions.
85 changes: 59 additions & 26 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"log"
"os"
"path/filepath"
"plenti/cmd/build"
Expand Down Expand Up @@ -48,18 +47,19 @@ you need to deploy for your website.`,
}

// Build creates the compiled app that gets deployed.
func Build() {
func Build() error {

defer build.Benchmark(time.Now(), "Total build", true)

build.CheckVerboseFlag(VerboseFlag)
build.CheckBenchmarkFlag(BenchmarkFlag)

var err error
// Handle panic when someone tries building outside of a valid Plenti site.
defer func() {
if r := recover(); r != nil {
fmt.Println("Please create a valid Plenti project or fix your app structure before trying to run this command again.")
fmt.Printf("Error: %v \n\n", r)
err = fmt.Errorf("panic recovered in Build: %v", r)
}
}()

Expand All @@ -70,18 +70,23 @@ 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, err = build.ThemesCopy("themes/"+theme, themeOptions)
common.CheckErr(err)
if err = common.CheckErr(err); err != nil {
return err
}

// Merge the current project files with the theme.
err = build.ThemesMerge(tempBuildDir, buildDir)
common.CheckErr(err)
if err = common.CheckErr(build.ThemesMerge(tempBuildDir, buildDir)); err != nil {
return err
}

}

// Get the full path for the build directory of the site.
Expand All @@ -90,63 +95,91 @@ func Build() {
// Clear out any previous build dir of the same name.
if _, buildPathExistsErr := os.Stat(buildPath); buildPathExistsErr == nil {
build.Log("Removing old '" + buildPath + "' build directory")
if err = common.CheckErr(os.RemoveAll(buildPath)); err != nil {
return err
}

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

// Create the buildPath directory.
if err := os.MkdirAll(buildPath, os.ModePerm); err != nil {
// bail on error
log.Fatalf("Unable to create \"%v\" build directory: %s\n", buildDir, err)
// bail on error in build
if err = common.CheckErr(fmt.Errorf("Unable to create \"%v\" build directory: %s", err, buildDir)); err != nil {
return err
}

}
build.Log("Creating '" + buildDir + "' build directory")

// Add core NPM dependencies if node_module folder doesn't already exist.
err = build.NpmDefaults(tempBuildDir)
common.CheckErr(err)
if err = common.CheckErr(build.NpmDefaults(tempBuildDir)); err != nil {
return err
}

// Write ejectable core files to filesystem before building.
tempFiles, ejectedPath, err := build.EjectTemp(tempBuildDir)
common.CheckErr(err)
if err = common.CheckErr(err); err != nil {
return err
}

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

// Directly copy static assets to the build dir.
common.CheckErr(build.AssetsCopy(buildPath, tempBuildDir))
if err = common.CheckErr(build.AssetsCopy(buildPath, tempBuildDir)); err != nil {
return err
}

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

common.CheckErr(build.NodeExec(clientBuildStr, staticBuildStr, allNodesStr))
if err = common.CheckErr(build.NodeExec(clientBuildStr, staticBuildStr, allNodesStr)); err != nil {
return err
}
} else {

// Prep the client SPA.

common.CheckErr(build.Client(buildPath, tempBuildDir, ejectedPath))
err = build.Client(buildPath, tempBuildDir, ejectedPath)
if err = common.CheckErr(err); err != nil {
return err
}

// Build JSON from "content/" directory.
common.CheckErr(build.DataSource(buildPath, siteConfig, tempBuildDir))
err = build.DataSource(buildPath, siteConfig, tempBuildDir)
if err = common.CheckErr(err); err != nil {
return err
}

}

// Run Gopack (custom Snowpack alternative) for ESM support.
common.CheckErr(build.Gopack(buildPath))
if err = common.CheckErr(build.Gopack(buildPath)); err != nil {
return err
}

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

}

Expand Down
19 changes: 13 additions & 6 deletions cmd/build/assets_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"path/filepath"
"plenti/common"
"strings"
"time"
)
Expand All @@ -22,42 +23,48 @@ func AssetsCopy(buildPath string, tempBuildDir string) error {

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

err := filepath.Walk(assetsDir, func(assetPath string, assetFileInfo os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("can't stat %s: %w", assetPath, err)
}
destPath := buildPath + "/" + strings.TrimPrefix(assetPath, tempBuildDir)
if assetFileInfo.IsDir() {
// Make directory if it doesn't exist.
// Move on to next path.
return os.MkdirAll(destPath, os.ModePerm)
if err = os.MkdirAll(destPath, os.ModePerm); err != nil {
return fmt.Errorf("cannot create asset dir %s: %w", assetPath, err)
}
return nil

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

}
defer from.Close()

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

}
defer to.Close()

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

}

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

}

Expand Down

0 comments on commit dc825c1

Please sign in to comment.