Skip to content

Commit

Permalink
feat: force parse json files with jsonc to allow for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Oct 15, 2024
1 parent 27f391b commit 36417ad
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions sg/internal/source/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package source
import (
"fmt"
"io/fs"
"maps"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -84,6 +85,7 @@ func loadSourceFromPaths(contextRoot string, paths []string) ([]Source, error) {
relativeToContextRoot := relativeToContextRootFn(contextRoot)

var files []string
var jsonFiles []string

walk := func(path string, info fs.DirEntry, err error) error {
if err != nil {
Expand All @@ -95,7 +97,11 @@ func loadSourceFromPaths(contextRoot string, paths []string) ([]Source, error) {
}

if parser.FileSupported(path) {
files = append(files, path)
if filepath.Ext(path) == ".json" {
jsonFiles = append(jsonFiles, path)
} else {
files = append(files, path)
}
}

return nil
Expand All @@ -107,14 +113,21 @@ func loadSourceFromPaths(contextRoot string, paths []string) ([]Source, error) {
}
}

if len(files) < 1 {
if len(files)+len(jsonFiles) < 1 {
return nil, fmt.Errorf("no files found from given paths: %v", paths)
}

// parse json files with jsonc to allow comments
jsonConfigurations, err := parser.ParseConfigurationsAs(jsonFiles, parser.JSONC)
if err != nil {
return nil, fmt.Errorf("parse configurations: %w", err)
}
configurations, err := parser.ParseConfigurations(files)
if err != nil {
return nil, fmt.Errorf("parse configurations: %w", err)
}
maps.Copy(configurations, jsonConfigurations)

filePathsSorted := make([]string, 0, len(configurations))
for filePath := range configurations {
filePathsSorted = append(filePathsSorted, filePath)
Expand Down

0 comments on commit 36417ad

Please sign in to comment.