Skip to content

Commit

Permalink
Merge pull request #142 from plentico/issue-137
Browse files Browse the repository at this point in the history
Replace generate with embed - Issue 137
  • Loading branch information
jimafisk authored Mar 17, 2021
2 parents 4141f02 + 8a7f0c2 commit e666beb
Show file tree
Hide file tree
Showing 291 changed files with 258 additions and 74,788 deletions.
18 changes: 4 additions & 14 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,12 @@ func Build() error {
build.Log("Creating '" + buildDir + "' build directory")

// Add core NPM dependencies if node_module folder doesn't already exist.
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)
if err = common.CheckErr(err); err != nil {
if err = common.CheckErr(build.NpmDefaults(tempBuildDir, defaultsNodeModulesFS)); err != nil {
return err
}

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

Expand All @@ -149,7 +143,7 @@ func Build() error {
} else {

// Prep the client SPA.
err = build.Client(buildPath, tempBuildDir, ejectedPath)
err = build.Client(buildPath, tempBuildDir, defaultsEjectedFS)
if err = common.CheckErr(err); err != nil {
return err
}
Expand All @@ -172,12 +166,8 @@ func Build() error {
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
if err = common.CheckErr(build.EjectClean(tempFiles, ejectedPath)); err != nil {
return err
}
}

// only relates to defer recover
return err

Expand Down
43 changes: 32 additions & 11 deletions cmd/build/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package build

import (
"embed"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -19,7 +21,7 @@ import (
var SSRctx *v8go.Context

// Client builds the SPA.
func Client(buildPath string, tempBuildDir string, ejectedPath string) error {
func Client(buildPath string, tempBuildDir string, defaultsEjectedFS embed.FS) error {

defer Benchmark(time.Now(), "Compiling client SPA with Svelte")

Expand Down Expand Up @@ -105,8 +107,28 @@ func Client(buildPath string, tempBuildDir string, ejectedPath string) error {

}

routerPath := tempBuildDir + "ejected/router.svelte"
var componentStr string
if _, err := os.Stat(routerPath); err == nil {
// The router has been ejected to the filesystem.
component, err := ioutil.ReadFile(routerPath)
if err != nil {
return fmt.Errorf("can't read component file: %s %w%s", routerPath, err, common.Caller())
}
componentStr = string(component)
} else if os.IsNotExist(err) {
// The router has not been ejected, use the embedded defaults.
ejected, err := fs.Sub(defaultsEjectedFS, "defaults")
if err != nil {
common.CheckErr(fmt.Errorf("Unable to get ejected defaults: %w", err))
}
ejected.Open(routerPath)
routerComp, _ := ejected.Open(routerPath)
routerCompBytes, _ := ioutil.ReadAll(routerComp)
componentStr = string(routerCompBytes)
}
// Compile router separately since it's ejected from core.
if err = (compileSvelte(ctx, SSRctx, ejectedPath+"/router.svelte", buildPath+"/spa/ejected/router.js", stylePath, tempBuildDir)); err != nil {
if err = (compileSvelte(ctx, SSRctx, "ejected/router.svelte", componentStr, buildPath+"/spa/ejected/router.js", stylePath, tempBuildDir)); err != nil {
return err
}

Expand All @@ -131,7 +153,12 @@ func Client(buildPath string, tempBuildDir string, ejectedPath string) error {
// Replace .svelte file extension with .js.
destFile = strings.TrimSuffix(destFile, filepath.Ext(destFile)) + ".js"

if err = compileSvelte(ctx, SSRctx, layoutPath, destFile, stylePath, tempBuildDir); err != nil {
component, err := ioutil.ReadFile(layoutPath)
if err != nil {
return fmt.Errorf("can't read component file: %s %w%s", layoutPath, err, common.Caller())
}
componentStr := string(component)
if err = compileSvelte(ctx, SSRctx, layoutPath, componentStr, destFile, stylePath, tempBuildDir); err != nil {
return fmt.Errorf("%w%s", err, common.Caller())
}

Expand Down Expand Up @@ -168,16 +195,10 @@ func Client(buildPath string, tempBuildDir string, ejectedPath string) error {
}

func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, layoutPath string,
destFile string, stylePath string, tempBuildDir string) error {

component, err := ioutil.ReadFile(layoutPath)
if err != nil {
return fmt.Errorf("can't read component file: %s %w%s", layoutPath, err, common.Caller())
}
componentStr := string(component)
componentStr string, destFile string, stylePath string, tempBuildDir string) error {

// Compile component with Svelte.
_, err = ctx.RunScript("var { js, css } = svelte.compile(`"+componentStr+"`, {css: false, hydratable: true});", "compile_svelte")
_, err := ctx.RunScript("var { js, css } = svelte.compile(`"+componentStr+"`, {css: false, hydratable: true});", "compile_svelte")
if err != nil {
return fmt.Errorf("can't compile component file %s with Svelte: %w%s", layoutPath, err, common.Caller())
}
Expand Down
32 changes: 0 additions & 32 deletions cmd/build/eject_clean.go

This file was deleted.

48 changes: 28 additions & 20 deletions cmd/build/eject_copy.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package build

import (
"embed"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"plenti/common"
Expand All @@ -11,21 +13,25 @@ import (
)

// EjectCopy does a direct copy of any ejectable js files needed in spa build dir.
func EjectCopy(buildPath string, tempBuildDir string, ejectedDir string) error {
func EjectCopy(buildPath string, tempBuildDir string, defaultsEjectedFS embed.FS) error {

defer Benchmark(time.Now(), "Copying ejectable core files for build")

Log("\nCopying ejectable core files to their destination:")

copiedSourceCounter := 0

ejectedFilesErr := filepath.Walk(ejectedDir, func(ejectPath string, ejectFileInfo os.FileInfo, err error) error {
ejected, err := fs.Sub(defaultsEjectedFS, "defaults")
if err != nil {
common.CheckErr(fmt.Errorf("Unable to get ejected defaults: %w", err))
}
ejectedFilesErr := fs.WalkDir(ejected, ".", func(ejectPath string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("can't stat %s: %w", ejectPath, err)
}
// Make list of files not to copy to build.
excludedFiles := []string{
ejectedDir + "/build.js",
"ejected/build.js",
}
// Check if the current file is in the excluded list.
excluded := false
Expand All @@ -38,25 +44,27 @@ func EjectCopy(buildPath string, tempBuildDir string, ejectedDir string) error {
if filepath.Ext(ejectPath) == ".js" && !excluded {

destPath := buildPath + "/spa/"
if err := os.MkdirAll(destPath+strings.TrimPrefix(ejectedDir, tempBuildDir), os.ModePerm); err != nil {
if err := os.MkdirAll(destPath+strings.TrimPrefix("ejected", tempBuildDir), os.ModePerm); err != nil {
return err
}

from, err := os.Open(ejectPath)
if err != nil {
return fmt.Errorf("Could not open source .js file for copying: %w%s", err, common.Caller())
var ejectedContent []byte
if _, err := os.Stat(ejectPath); err == nil {
ejectedContent, err = ioutil.ReadFile(ejectPath)
if err != nil {
return fmt.Errorf("can't read .js file: %s %w%s", ejectPath, err, common.Caller())
}
} else if os.IsNotExist(err) {
ejectedFile, err := ejected.Open(ejectPath)
if err != nil {
return fmt.Errorf("Could not open source .js file for copying: %w%s", err, common.Caller())
}
ejectedContent, err = ioutil.ReadAll(ejectedFile)
if err != nil {
return fmt.Errorf("Can't read ejected .js file: %w%s", err, common.Caller())
}
}
defer from.Close()

to, err := os.Create(destPath + strings.TrimPrefix(ejectPath, tempBuildDir))
if err != nil {
return fmt.Errorf("Could not create destination .js file for copying: %w%s", err, common.Caller())
}
defer to.Close()

_, err = io.Copy(to, from)
if err != nil {
return fmt.Errorf("Could not copy .js from source to destination: %w%s", err, common.Caller())
if err := ioutil.WriteFile(destPath+ejectPath, ejectedContent, os.ModePerm); err != nil {
return fmt.Errorf("Unable to write file: %w%s", err, common.Caller())
}

copiedSourceCounter++
Expand Down
47 changes: 0 additions & 47 deletions cmd/build/eject_temp.go

This file was deleted.

36 changes: 23 additions & 13 deletions cmd/build/npm_defaults.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package build

import (
"embed"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"plenti/common"
"plenti/generated"
"time"
)

// NpmDefaults creates the node_modules folder with core defaults if it doesn't already exist.
func NpmDefaults(tempBuildDir string) error {
func NpmDefaults(tempBuildDir string, defaultsNodeModulesFS embed.FS) error {

defer Benchmark(time.Now(), "Setting up core NPM packages")

Expand All @@ -20,19 +20,29 @@ func NpmDefaults(tempBuildDir string) error {
destPath := tempBuildDir + "node_modules"

if _, err := os.Stat(destPath); os.IsNotExist(err) {
for file, content := range generated.Defaults_node_modules {
// Make file relative to where CLI is executed
file = destPath + "/" + file
// Create the directories needed for the current file
if err = os.MkdirAll(filepath.Dir(file), os.ModePerm); err != nil {
return fmt.Errorf("Unable to MkdirAll in NpmDefaults: %w%s", err, common.Caller())
nodeModules, err := fs.Sub(defaultsNodeModulesFS, "defaults")
if err != nil {
common.CheckErr(fmt.Errorf("Unable to get node_modules defaults: %w", err))
}
fs.WalkDir(nodeModules, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
// Create the directories needed for the current file
if err := os.MkdirAll(path, os.ModePerm); err != nil {
common.CheckErr(fmt.Errorf("Unable to create path(s) %s: %v", path, err))
}
return nil
}
content, _ := nodeModules.Open(path)
contentBytes, err := ioutil.ReadAll(content)
// Create the current default file
err := ioutil.WriteFile(file, content, os.ModePerm)
if err != nil {
return fmt.Errorf("Unable to write npm dependency file: %w%s", err, common.Caller())
if err := ioutil.WriteFile(path, contentBytes, 0755); err != nil {
common.CheckErr(fmt.Errorf("Unable to write node_modules file: %w", err))
}
}
return nil
})
}
return nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit e666beb

Please sign in to comment.