Skip to content

Commit

Permalink
postcss: Improve validation of option 'config'
Browse files Browse the repository at this point in the history
  • Loading branch information
deining committed May 22, 2023
1 parent 10d0fcc commit 9a0370e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
8 changes: 4 additions & 4 deletions hugolib/filesystems/basefs.go
Expand Up @@ -188,19 +188,19 @@ func (b *BaseFs) AbsProjectContentDir(filename string) (string, string, error) {

// ResolveJSConfigFile resolves the JS-related config file to a absolute
// filename. One example of such would be postcss.config.js.
func (fs *BaseFs) ResolveJSConfigFile(name string) string {
func (fs *BaseFs) ResolveJSConfigFile(name string) (string, bool) {
// First look in assets/_jsconfig
fi, err := fs.Assets.Fs.Stat(filepath.Join(files.FolderJSConfig, name))
if err == nil {
return fi.(hugofs.FileMetaInfo).Meta().Filename
return fi.(hugofs.FileMetaInfo).Meta().Filename, fi.IsDir()
}
// Fall back to the work dir.
fi, err = fs.Work.Stat(name)
if err == nil {
return fi.(hugofs.FileMetaInfo).Meta().Filename
return fi.(hugofs.FileMetaInfo).Meta().Filename, fi.IsDir()
}

return ""
return "", false
}

// MakePathRelative creates a relative path from the given filename.
Expand Down
8 changes: 6 additions & 2 deletions resources/resource_transformers/babel/babel.go
Expand Up @@ -134,13 +134,17 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
}

configFile = filepath.Clean(configFile)
isConfigFileDir := false

// We need an absolute filename to the config file.
if !filepath.IsAbs(configFile) {
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
configFile, isConfigFileDir = t.rs.BaseFs.ResolveJSConfigFile(configFile)
if isConfigFileDir {
logger.Warnf("babel config %q must be a file, not a directory", configFile)
}
if configFile == "" && t.options.Config != "" {
// Only fail if the user specified config file is not found.
return fmt.Errorf("babel config %q not found:", configFile)
return fmt.Errorf("babel config file %q not found", configFile)
}
}

Expand Down
8 changes: 6 additions & 2 deletions resources/resource_transformers/postcss/postcss.go
Expand Up @@ -172,13 +172,17 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
}

configFile = filepath.Clean(configFile)
isConfigFileDir := false

// We need an absolute filename to the config file.
if !filepath.IsAbs(configFile) {
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
configFile, isConfigFileDir = t.rs.BaseFs.ResolveJSConfigFile(configFile)
if configFile == "" && options.Config != "" {
// Only fail if the user specified config file is not found.
return fmt.Errorf("postcss config %q not found:", options.Config)
return fmt.Errorf("postcss config directory %q not found", options.Config)
}
if !isConfigFileDir {
logger.Warnf("postcss config %q must be a directory", options.Config)
}
}

Expand Down

0 comments on commit 9a0370e

Please sign in to comment.