-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: catch bun install error when package is in dependencies and dev …
…dependencies
- Loading branch information
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package extension | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path" | ||
) | ||
|
||
type npmPackage struct { | ||
Dependencies map[string]string `json:"dependencies"` | ||
DevDependencies map[string]string `json:"devDependencies"` | ||
} | ||
|
||
// When a package is defined in both dependencies and devDependencies, bun will crash | ||
func canRunBunOnPackage(packagePath string) bool { | ||
packageJson, err := os.ReadFile(path.Join(packagePath, "package.json")) | ||
|
||
if err != nil { | ||
return false | ||
} | ||
|
||
var npmPackage npmPackage | ||
|
||
if json.Unmarshal(packageJson, &npmPackage) != nil { | ||
return false | ||
} | ||
|
||
for name, _ := range npmPackage.Dependencies { | ||
if _, ok := npmPackage.DevDependencies[name]; ok { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package extension | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"path" | ||
"testing" | ||
) | ||
|
||
func TestValidPackageJsonBun(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
packageJson := `{ | ||
"dependencies": { | ||
"foo": "1.0.0" | ||
} | ||
}` | ||
|
||
if err := os.WriteFile(path.Join(tmpDir, "package.json"), []byte(packageJson), 0644); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.True(t, canRunBunOnPackage(tmpDir)) | ||
} | ||
|
||
func TestValidPackageJsonWithDevBun(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
packageJson := `{ | ||
"dependencies": { | ||
"foo": "1.0.0" | ||
}, | ||
"devDependencies": { | ||
"bar": "1.0.0" | ||
} | ||
}` | ||
|
||
if err := os.WriteFile(path.Join(tmpDir, "package.json"), []byte(packageJson), 0644); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.True(t, canRunBunOnPackage(tmpDir)) | ||
} | ||
|
||
func TestInvalidPackageJsonBun(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
packageJson := `{ | ||
"dependencies": { | ||
"foo": "1.0.0" | ||
}, | ||
"devDependencies": { | ||
"foo": "1.0.0" | ||
} | ||
}` | ||
|
||
if err := os.WriteFile(path.Join(tmpDir, "package.json"), []byte(packageJson), 0644); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.False(t, canRunBunOnPackage(tmpDir)) | ||
} |