Skip to content

Commit

Permalink
Temp remove in-mem.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Jan 3, 2023
1 parent 6e8987d commit f5b2050
Show file tree
Hide file tree
Showing 27 changed files with 215 additions and 992 deletions.
28 changes: 10 additions & 18 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/plentico/plenti/cmd/build"
"github.com/plentico/plenti/common"
"github.com/plentico/plenti/readers"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -103,32 +102,25 @@ func Build() error {
if err != nil {
log.Fatal("\nError in ThemesMerge build step", 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 !common.UseMemFS {
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
}

}

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

if _, buildPathExistsErr := os.Stat(buildPath); buildPathExistsErr == nil {
build.Log("Removing old '" + buildPath + "' build directory")
if err = os.RemoveAll(buildPath); err != nil {
log.Fatal("\nCan't remove \"%v\" folder from previous build", err, buildDir)
}
}

// Create the buildPath directory.
build.Log("Creating '" + buildDir + "' build directory")
if err := os.MkdirAll(buildPath, os.ModePerm); err != nil {
// bail on error in build
log.Fatal("Unable to create \"%v\" build directory: %s", err, buildDir)
}

// Directly copy .js that don't need compiling to the build dir.
err = build.EjectCopy(buildPath, defaultsEjectedFS)
Expand Down
33 changes: 10 additions & 23 deletions cmd/build/assets_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"time"

"github.com/plentico/plenti/common"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -60,29 +59,29 @@ func copyAssetsFromTheme(assetsDir string, buildPath string, copiedSourceCounter
}
from, err := ThemeFs.Open(path)
if err != nil {
return fmt.Errorf("Could not open asset %s for copying: %w%s\n", path, err, common.Caller())
return fmt.Errorf("Could not open asset %s for copying: %w\n", path, err)

}
defer from.Close()

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

}
defer to.Close()

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

}

index = append(index, path)
copiedSourceCounter++
return nil
}); err != nil {
return 0, fmt.Errorf("Could not get asset file from virtual theme build: %w%s\n", err, common.Caller())
return 0, fmt.Errorf("Could not get asset file from virtual theme build: %w\n", err)
}

err := createAssetsIndex(buildPath, index)
Expand All @@ -109,10 +108,6 @@ func copyAssetsFromProject(assetsDir string, buildPath string, copiedSourceCount
}
destPath := buildPath + "/" + assetPath
if assetFileInfo.IsDir() {
// no dirs
if common.UseMemFS {
return nil
}
// Make directory if it doesn't exist.
// Move on to next path.
if err = os.MkdirAll(destPath, os.ModePerm); err != nil {
Expand All @@ -121,31 +116,23 @@ func copyAssetsFromProject(assetsDir string, buildPath string, copiedSourceCount
return nil

}
if common.UseMemFS {
bd, err := os.ReadFile(assetPath)
if err != nil {
return fmt.Errorf("Could not open asset %s for copying: %w%s\n", assetPath, err, common.Caller())
}
common.Set(destPath, assetPath, &common.FData{B: bd})
return nil
}
from, err := os.Open(assetPath)
if err != nil {
return fmt.Errorf("Could not open asset %s for copying: %w%s\n", assetPath, err, common.Caller())
return fmt.Errorf("Could not open asset %s for copying: %w\n", assetPath, err)

}
defer from.Close()

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

}
defer to.Close()

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

}

Expand All @@ -154,7 +141,7 @@ func copyAssetsFromProject(assetsDir string, buildPath string, copiedSourceCount
return nil
})
if err != nil {
return 0, fmt.Errorf("Could not get asset file: %w%s\n", err, common.Caller())
return 0, fmt.Errorf("Could not get asset file: %w\n", err)
}

err = createAssetsIndex(buildPath, index)
Expand All @@ -168,12 +155,12 @@ func copyAssetsFromProject(assetsDir string, buildPath string, copiedSourceCount
func createAssetsIndex(buildPath string, index []string) error {
result, err := json.MarshalIndent(index, "", "\t")
if err != nil {
return fmt.Errorf("Unable to marshal JSON: %w%s", err, common.Caller())
return fmt.Errorf("Unable to marshal JSON: %w", err)
}
result = append(append([]byte("let allAssets = "), result...), []byte(";\nexport default allAssets;")...)
err = ioutil.WriteFile(buildPath+"/spa/ejected/cms/assets.js", result, os.ModePerm)
if err != nil {
return fmt.Errorf("Unable to write to asset index file: %w%s\n", err, common.Caller())
return fmt.Errorf("Unable to write to asset index file: %w\n", err)
}
return nil
}
40 changes: 15 additions & 25 deletions cmd/build/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"

"github.com/plentico/plenti/common"
"github.com/plentico/plenti/readers"
"github.com/spf13/afero"

Expand All @@ -31,10 +31,6 @@ func Client(buildPath string, defaultsEjectedFS embed.FS) error {
Log("\nCompiling client SPA with svelte")

stylePath := buildPath + "/spa/bundle.css"
if common.UseMemFS {
// clear styles as we append bytes.
common.Set(stylePath, "", &common.FData{})
}
allLayoutsPath := buildPath + "/spa/ejected/layouts.js"
// Initialize string for layouts.js component list.
var allLayoutsStr string
Expand All @@ -53,18 +49,18 @@ func Client(buildPath string, defaultsEjectedFS embed.FS) error {
compilerStr = strings.Replace(compilerStr, "const Url$1 = (typeof URL !== 'undefined' ? URL : require('url').URL);", "", 1)
ctx, err := v8go.NewContext(nil)
if err != nil {
return fmt.Errorf("Could not create Isolate: %w%s\n", err, common.Caller())
return fmt.Errorf("Could not create Isolate: %w\n", err)

}
_, err = ctx.RunScript(compilerStr, "compile_svelte")
if err != nil {
return fmt.Errorf("Could not add svelte compiler: %w%s\n", err, common.Caller())
return fmt.Errorf("Could not add svelte compiler: %w\n", err)

}

SSRctx, err = v8go.NewContext(nil)
if err != nil {
return fmt.Errorf("Could not create Isolate: %w%s\n", err, common.Caller())
return fmt.Errorf("Could not create Isolate: %w\n", err)

}
// Fix "ReferenceError: exports is not defined" errors on line 1319 (exports.current_component;).
Expand Down Expand Up @@ -129,14 +125,14 @@ func Client(buildPath string, defaultsEjectedFS embed.FS) error {
// The file has been ejected to the filesystem.
component, err := getVirtualFileIfThemeBuild(path)
if err != nil {
return fmt.Errorf("can't read component file: %s %w%s\n", path, err, common.Caller())
return fmt.Errorf("can't read component file: %s %w\n", path, err)
}
componentStr = string(component)
} else if os.IsNotExist(err) {
// The file has not been ejected, use the embedded defaults.
nonEjectedFS, err := fs.Sub(defaultsEjectedFS, "defaults")
if err != nil {
common.CheckErr(fmt.Errorf("Unable to get non ejected defaults: %w", err))
log.Fatal("Unable to get non ejected defaults: %w", err)
}
component, err := nonEjectedFS.Open(path)
if err != nil {
Expand Down Expand Up @@ -195,16 +191,10 @@ func Client(buildPath string, defaultsEjectedFS embed.FS) error {
}
}

if common.UseMemFS {
b := []byte(allLayoutsStr)
common.Set(allLayoutsPath, "", &common.FData{B: b})
return nil

}
// Write layouts.js to filesystem.
err = ioutil.WriteFile(allLayoutsPath, []byte(allLayoutsStr), os.ModePerm)
if err != nil {
return fmt.Errorf("Unable to write layouts.js file: %w%s\n", err, common.Caller())
return fmt.Errorf("Unable to write layouts.js file: %w\n", err)
}

Log("Number of components compiled: " + strconv.Itoa(compiledComponentCounter))
Expand All @@ -215,25 +205,25 @@ func copyNonSvelteFiles(layoutPath string, buildPath string) error {
if filepath.Ext(layoutPath) != ".svelte" {
from, err := os.Open(layoutPath)
if err != nil {
return fmt.Errorf("Could not open non-svelte layout %s for copying: %w%s\n", layoutPath, err, common.Caller())
return fmt.Errorf("Could not open non-svelte layout %s for copying: %w\n", layoutPath, err)
}
defer from.Close()

destPath := buildPath + "/spa/" + strings.TrimPrefix(layoutPath, "layouts/")
// Create any sub directories need for filepath.
if err := os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil {
return fmt.Errorf("can't make folders for '%s': %w%s\n", destPath, err, common.Caller())
return fmt.Errorf("can't make folders for '%s': %w\n", destPath, err)
}

to, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("Could not create non-svelte layout destination %s for copying: %w%s\n", destPath, err, common.Caller())
return fmt.Errorf("Could not create non-svelte layout destination %s for copying: %w\n", destPath, err)
}
defer to.Close()

_, err = io.Copy(to, from)
if err != nil {
return fmt.Errorf("Could not copy non-svelte layout from source %s to destination: %w%s\n", layoutPath, err, common.Caller())
return fmt.Errorf("Could not copy non-svelte layout from source %s to destination: %w\n", layoutPath, err)
}
}
return nil
Expand All @@ -252,12 +242,12 @@ func compileComponent(err error, layoutPath string, layoutFileInfo os.FileInfo,
// Get component file contents
component, err := getVirtualFileIfThemeBuild(layoutPath)
if err != nil {
return compiledComponentCounter, allLayoutsStr, fmt.Errorf("can't read component file: %s %w%s\n", layoutPath, err, common.Caller())
return compiledComponentCounter, allLayoutsStr, fmt.Errorf("can't read component file: %s %w\n", layoutPath, err)
}
componentStr := string(component)
// Actually compile component
if err = compileSvelte(ctx, SSRctx, layoutPath, componentStr, destFile, stylePath); err != nil {
return compiledComponentCounter, allLayoutsStr, fmt.Errorf("%w%s\n", err, common.Caller())
return compiledComponentCounter, allLayoutsStr, fmt.Errorf("%w\n", err)
}
// Create entry for layouts.js.
layoutSignature := strings.ReplaceAll(strings.ReplaceAll((layoutPath), "/", "_"), ".", "_")
Expand All @@ -277,12 +267,12 @@ func getVirtualFileIfThemeBuild(filename string) ([]byte, error) {
if ThemeFs != nil {
fileContents, err = afero.ReadFile(ThemeFs, filename)
if err != nil {
return []byte{}, fmt.Errorf("Can't read %s from virtual theme: %w%s\n", filename, err, common.Caller())
return []byte{}, fmt.Errorf("Can't read %s from virtual theme: %w\n", filename, err)
}
} else {
fileContents, err = ioutil.ReadFile(filename)
if err != nil {
return []byte{}, fmt.Errorf("Can't read %s from filesystem: %w%s\n", filename, err, common.Caller())
return []byte{}, fmt.Errorf("Can't read %s from filesystem: %w\n", filename, err)
}
}
return fileContents, nil
Expand Down
Loading

0 comments on commit f5b2050

Please sign in to comment.