Skip to content

Commit

Permalink
feat: add storefront-watch command
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Feb 11, 2024
1 parent a8f1283 commit 5538f0e
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmd/project/platform.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package project

import (
"encoding/json"
"fmt"
"github.com/FriendsOfShopware/shopware-cli/extension"
"github.com/FriendsOfShopware/shopware-cli/shop"
"github.com/spf13/cobra"
"os"
"path"
"path/filepath"
"strings"
)
Expand Down Expand Up @@ -44,3 +49,39 @@ func findClosestShopwareProject() (string, error) {

return "", fmt.Errorf("cannot find Shopware project in current directory")
}

func filterAndWritePluginJson(cmd *cobra.Command, projectRoot string, shopCfg *shop.Config) error {
sources := extension.FindAssetSourcesOfProject(cmd.Context(), projectRoot, shopCfg)

cfgs := extension.BuildAssetConfigFromExtensions(cmd.Context(), sources, extension.AssetBuildConfig{})

onlyExtensions, _ := cmd.PersistentFlags().GetString("only-extensions")
skipExtensions, _ := cmd.PersistentFlags().GetString("skip-extensions")

if onlyExtensions != "" && skipExtensions != "" {
return fmt.Errorf("only-extensions and skip-extensions cannot be used together")
}

if onlyExtensions != "" {
cfgs = cfgs.Only(strings.Split(onlyExtensions, ","))
}

if skipExtensions != "" {
cfgs = cfgs.Not(strings.Split(skipExtensions, ","))
}

if _, err := extension.InstallNodeModulesOfConfigs(cmd.Context(), cfgs, false); err != nil {
return err
}

pluginJson, err := json.MarshalIndent(cfgs, "", " ")
if err != nil {
return err
}

if err := os.WriteFile(path.Join(projectRoot, "var", "plugins.json"), pluginJson, os.ModePerm); err != nil {
return err
}

return nil
}
83 changes: 83 additions & 0 deletions cmd/project/project_storefront_watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package project

import (
"github.com/FriendsOfShopware/shopware-cli/extension"
"github.com/FriendsOfShopware/shopware-cli/shop"
"github.com/spf13/cobra"
"os"
"os/exec"
"strings"
)

var projectStorefrontWatchCmd = &cobra.Command{
Use: "storefront-watch [path]",
Short: "Starts the Shopware Storefront Watcher",
RunE: func(cmd *cobra.Command, args []string) error {
var projectRoot string
var err error

if len(args) == 1 {
projectRoot = args[0]
} else if projectRoot, err = findClosestShopwareProject(); err != nil {
return err
}

if err := extension.LoadSymfonyEnvFile(projectRoot); err != nil {
return err
}

shopCfg, err := shop.ReadConfig(projectConfigPath, true)
if err != nil {
return err
}

if err := filterAndWritePluginJson(cmd, projectRoot, shopCfg); err != nil {
return err
}

if err := runTransparentCommand(commandWithRoot(exec.CommandContext(cmd.Context(), "php", "bin/console", "feature:dump"), projectRoot)); err != nil {
return err
}

activeOnly := "--active-only"

if !themeCompileSupportsActiveOnly(projectRoot) {
activeOnly = "-v"
}

if err := runTransparentCommand(commandWithRoot(exec.CommandContext(cmd.Context(), "php", "bin/console", "theme:compile", activeOnly), projectRoot)); err != nil {
return err
}

if err := runTransparentCommand(commandWithRoot(exec.CommandContext(cmd.Context(), "php", "bin/console", "theme:dump"), projectRoot)); err != nil {
return err
}

if err := os.Setenv("PROJECT_ROOT", projectRoot); err != nil {
return err
}

if err := os.Setenv("STOREFRONT_ROOT", extension.PlatformPath(projectRoot, "Storefront", "")); err != nil {
return err
}

return runTransparentCommand(commandWithRoot(exec.CommandContext(cmd.Context(), "npm", "run-script", "hot-proxy"), extension.PlatformPath(projectRoot, "Storefront", "Resources/app/storefront")))
},
}

func themeCompileSupportsActiveOnly(projectRoot string) bool {
themeFile := extension.PlatformPath(projectRoot, "Storefront", "Theme/Command/ThemeCompileCommand.php")

bytes, err := os.ReadFile(themeFile)
if err != nil {
return false
}

return !strings.Contains(string(bytes), "active-only")
}

func init() {
projectRootCmd.AddCommand(projectStorefrontWatchCmd)
projectStorefrontWatchCmd.PersistentFlags().String("only-extensions", "", "Only watch the given extensions")
projectStorefrontWatchCmd.PersistentFlags().String("skip-extensions", "", "Skips the given extensions")
}
25 changes: 25 additions & 0 deletions extension/asset_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path"
"slices"
"strings"

"github.com/FriendsOfShopware/shopware-cli/internal/asset"
Expand Down Expand Up @@ -617,6 +618,30 @@ func (c ExtensionAssetConfig) FilterByStorefrontAndEsBuild(esbuildEnabled bool)
return filtered
}

func (c ExtensionAssetConfig) Only(extensions []string) ExtensionAssetConfig {
filtered := make(ExtensionAssetConfig)

for name, entry := range c {
if slices.Contains(extensions, name) {
filtered[name] = entry
}
}

return filtered
}

func (c ExtensionAssetConfig) Not(extensions []string) ExtensionAssetConfig {
filtered := make(ExtensionAssetConfig)

for name, entry := range c {
if !slices.Contains(extensions, name) {
filtered[name] = entry
}
}

return filtered
}

type ExtensionAssetConfigEntry struct {
BasePath string `json:"basePath"`
Views []string `json:"views"`
Expand Down
31 changes: 31 additions & 0 deletions extension/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package extension

import (
"github.com/joho/godotenv"
"os"
"path"
"path/filepath"
"strings"
)
Expand All @@ -24,3 +26,32 @@ func IsContributeProject(projectRoot string) bool {

return false
}

func LoadSymfonyEnvFile(projectRoot string) error {
currentEnv := os.Getenv("APP_ENV")
if currentEnv == "" {
currentEnv = "dev"
}

possibleEnvFiles := []string{
path.Join(projectRoot, ".env"),
path.Join(projectRoot, ".env.dist"),
path.Join(projectRoot, ".env.local"),
path.Join(projectRoot, ".env."+currentEnv),
path.Join(projectRoot, ".env."+currentEnv+".local"),
}

foundEnvFiles := []string{}

for _, envFile := range possibleEnvFiles {
if _, err := os.Stat(envFile); err == nil {
foundEnvFiles = append(foundEnvFiles, envFile)
}
}

if len(foundEnvFiles) == 0 {
return nil
}

return godotenv.Overload(foundEnvFiles...)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/friendsofshopware/go-shopware-admin-api-sdk v0.0.0-20231210091330-92f38f1ae77c
github.com/google/uuid v1.6.0
github.com/gorilla/schema v1.2.1
github.com/joho/godotenv v1.5.1
github.com/manifoldco/promptui v0.9.0
github.com/microcosm-cc/bluemonday v1.0.26
github.com/olekukonko/tablewriter v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jaswdr/faker v1.19.1 h1:xBoz8/O6r0QAR8eEvKJZMdofxiRH+F0M/7MU9eNKhsM=
github.com/jaswdr/faker v1.19.1/go.mod h1:x7ZlyB1AZqwqKZgyQlnqEG8FDptmHlncA5u2zY/yi6w=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.17.5 h1:d4vBd+7CHydUqpFBgUEKkSdtSugf9YFmSkvUYPquI5E=
github.com/klauspost/compress v1.17.5/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
Expand Down

0 comments on commit 5538f0e

Please sign in to comment.