Skip to content

Commit

Permalink
Allow build specific config files (#183).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Jun 21, 2022
1 parent 9deca67 commit 35413cc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
6 changes: 6 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var BenchmarkFlag bool
// MinifyFlag condenses the JavaScript output so it runs faster in the browser.
var MinifyFlag bool

// ConfigFileFlag allows you to point to a nonstandard sitewide configuration file for the build (instead of plenti.json).
var ConfigFileFlag string

func setBuildDir(siteConfig readers.SiteConfig) string {
buildDir := siteConfig.BuildDir
// Check if directory is overridden by flag.
Expand Down Expand Up @@ -56,6 +59,8 @@ func Build() error {
build.CheckVerboseFlag(VerboseFlag)
build.CheckBenchmarkFlag(BenchmarkFlag)
build.CheckMinifyFlag(MinifyFlag)
readers.CheckConfigFileFlag(ConfigFileFlag)

var err error
// Handle panic when someone tries building outside of a valid Plenti site.
defer func() {
Expand Down Expand Up @@ -169,4 +174,5 @@ func init() {
buildCmd.Flags().BoolVarP(&VerboseFlag, "verbose", "v", false, "show log messages")
buildCmd.Flags().BoolVarP(&BenchmarkFlag, "benchmark", "b", false, "display build time statistics")
buildCmd.Flags().BoolVarP(&MinifyFlag, "minify", "m", true, "minify JS output for faster performance")
buildCmd.Flags().StringVarP(&ConfigFileFlag, "config", "c", "plenti.json", "use a custom sitewide configuration file")
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.plenti.yaml)")
//rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.plenti.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand Down
7 changes: 4 additions & 3 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ var serveCmd = &cobra.Command{
s.Color("blue")
s.Start()

// Get settings from config file.
siteConfig, _ := readers.GetSiteConfig(".")

// Always set as Local when using serve command
build.Local = true

Expand All @@ -75,6 +72,9 @@ var serveCmd = &cobra.Command{
buildCmd.Run(cmd, args)
}

// Get settings from config file.
siteConfig, _ := readers.GetSiteConfig(".")

// Check flags and config for directory to build to
buildDir := setBuildDir(siteConfig)

Expand Down Expand Up @@ -150,6 +150,7 @@ func init() {
serveCmd.Flags().BoolVarP(&MinifyFlag, "minify", "m", true, "minify JS output for faster performance")
serveCmd.Flags().BoolVarP(&SSLFlag, "ssl", "s", false, "ssl/tls encryption to serve localhost over https")
serveCmd.Flags().BoolVarP(&build.Doreload, "live-reload", "L", false, "Enable live reload")
serveCmd.Flags().StringVarP(&ConfigFileFlag, "config", "c", "plenti.json", "use a custom sitewide configuration file")
//serveCmd.Flags().BoolVarP(&common.UseMemFS, "in-memory", "M", false, "Use in memory filesystem")
}

Expand Down
11 changes: 10 additions & 1 deletion readers/site_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ type ThemeOptions struct {
Exclude []string `json:"exclude,omitempty"`
}

// Create global var since cmd.ConfigFileFlag is a circular dependency.
var configFilePath string

// CheckConfigFileFlag sets global var to --config flag value (or defaults to plenti.json).
func CheckConfigFileFlag(flag string) {
// If --config flag is passed by user, this will be set to its value.
configFilePath = flag
}

// GetSiteConfig reads the site's configuration file values.
func GetSiteConfig(basePath string) (SiteConfig, string) {

var siteConfig SiteConfig

configPath := basePath + "/plenti.json"
configPath := basePath + "/" + configFilePath

// Read site config file from the project
configFile, _ := ioutil.ReadFile(configPath)
Expand Down

0 comments on commit 35413cc

Please sign in to comment.