Skip to content

Commit

Permalink
fix: workspace parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Dec 27, 2023
1 parent bd355cd commit 6fcf00f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
3 changes: 3 additions & 0 deletions internal/js/.workspaces_test/other/foo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}
6 changes: 6 additions & 0 deletions internal/js/.workspaces_test/other/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "other",
"workspaces": {
"packages": ["foo"]
}
}
35 changes: 29 additions & 6 deletions internal/js/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,32 @@ type Workspaces struct {

type partialPackageJson struct {
path string
Main string `json:"main,omitempty"`
Name string `json:"name"`
Workspaces []string `json:"workspaces"`
Main string `json:"main,omitempty"`
Name string `json:"name"`
Workspaces interface{} `json:"workspaces"`
}

func castAnyArray[T any](arr []any) []T {
result := make([]T, len(arr))
for i, el := range arr {
result[i] = el.(T)
}
return result
}

func (p *partialPackageJson) workspaces() []string {
switch v := p.Workspaces.(type) {
case []any:
return castAnyArray[string](v)
case map[string]any:
if packages, ok := v["packages"]; ok {
switch vv := packages.(type) {
case []any:
return castAnyArray[string](vv)
}
}
}
return []string{}
}

func searchFirstPackageJsonWithWorkspaces(searchPath string) (*partialPackageJson, error) {
Expand All @@ -60,9 +83,9 @@ func searchFirstPackageJsonWithWorkspaces(searchPath string) (*partialPackageJso
}
err = json.Unmarshal(content, &result)
if err != nil {
return nil, err
return nil, fmt.Errorf("error parsing %q: %w", packageJsonPath, err)
}
if len(result.Workspaces) > 0 {
if len(result.workspaces()) > 0 {
result.path = searchPath
return &result, nil
} else {
Expand Down Expand Up @@ -109,7 +132,7 @@ func NewWorkspaces(searchPath string) (*Workspaces, error) {
workspacesMap := map[string]WorkspaceEntry{}

for _, dir := range dirsWithAPackageJson {
for _, ws := range rootPackageJson.Workspaces {
for _, ws := range rootPackageJson.workspaces() {
rel, _ := filepath.Rel(rootPackageJson.path, dir)
match, err := utils.GlobstarMatch(ws, rel)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/js/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ func TestNewWorkspaces(t *testing.T) {
}
}

func TestNewWorkspaces_parses_packages(t *testing.T) {
a := require.New(t)
result, err := NewWorkspaces(path.Join(workspacesTestDir, "other"))
a.NoError(err)
abs, _ := filepath.Abs(workspacesTestDir)
a.Equal(map[string]WorkspaceEntry{
"foo": {absPath: path.Join(abs, "other", "foo")},
}, result.ws)
}

func TestWorkspaces_ResolveFromWorkspaces(t *testing.T) {
abs, _ := filepath.Abs(workspacesTestDir)

Expand Down

0 comments on commit 6fcf00f

Please sign in to comment.