diff --git a/internal/bun/plan.go b/internal/bun/plan.go index 6364831f..c0b8b622 100644 --- a/internal/bun/plan.go +++ b/internal/bun/plan.go @@ -1,14 +1,59 @@ package bun import ( + "log" + + "github.com/moznion/go-optional" + "github.com/spf13/afero" "github.com/zeabur/zbpack/internal/nodejs" "github.com/zeabur/zbpack/pkg/types" ) +type bunPlanContext struct { + PackageJSON nodejs.PackageJSON + Src afero.Fs + + Framework optional.Option[types.BunFramework] +} + // GetMetaOptions is the options for GetMeta. type GetMetaOptions nodejs.GetMetaOptions // GetMeta gets the metadata of the Node.js project. func GetMeta(opt GetMetaOptions) types.PlanMeta { - return nodejs.GetMeta(nodejs.GetMetaOptions(opt)) + packageJSON, err := nodejs.DeserializePackageJSON(opt.Src) + if err != nil { + log.Printf("Failed to read package.json: %v", err) + // not fatal + } + + ctx := &bunPlanContext{ + PackageJSON: packageJSON, + Src: opt.Src, + } + + meta := nodejs.GetMeta(nodejs.GetMetaOptions(opt)) + + framework := DetermineFramework(ctx) + meta["framework"] = string(framework) + + return meta +} + +// DetermineFramework determines the framework of the Bun project. +func DetermineFramework(ctx *bunPlanContext) types.BunFramework { + fw := &ctx.Framework + packageJSON := ctx.PackageJSON + + if framework, err := fw.Take(); err == nil { + return framework + } + + if _, isElysia := packageJSON.Dependencies["elysia"]; isElysia { + *fw = optional.Some(types.BunFrameworkElysia) + return fw.Unwrap() + } + + *fw = optional.Some(types.BunFrameworkNone) + return fw.Unwrap() } diff --git a/pkg/types/plan.go b/pkg/types/plan.go index ad386523..118e59e6 100644 --- a/pkg/types/plan.go +++ b/pkg/types/plan.go @@ -188,3 +188,12 @@ const ( ) //revive:enable:exported + +// BunFramework represents the framework of a Bun project. +type BunFramework string + +//revive:enable:exported +const ( + BunFrameworkElysia BunFramework = "elysia" + BunFrameworkNone BunFramework = "none" +)