• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / cli / 11721004676

07 Nov 2024 10:19AM UTC coverage: 59.809% (-0.02%) from 59.826%
11721004676

Pull #2852

github

sweatybridge
fix: auto detect deno.json for globbed slugs
Pull Request #2852: fix: auto detect deno.json for globbed slugs

10 of 12 new or added lines in 1 file covered. (83.33%)

5 existing lines in 1 file now uncovered.

6384 of 10674 relevant lines covered (59.81%)

6.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.54
/internal/functions/deploy/deploy.go
1
package deploy
2

3
import (
4
        "context"
5
        "fmt"
6
        "os"
7
        "path/filepath"
8
        "strings"
9

10
        "github.com/go-errors/errors"
11
        "github.com/spf13/afero"
12
        "github.com/supabase/cli/internal/utils"
13
        "github.com/supabase/cli/pkg/cast"
14
        "github.com/supabase/cli/pkg/config"
15
        "github.com/supabase/cli/pkg/function"
16
)
17

18
func Run(ctx context.Context, slugs []string, projectRef string, noVerifyJWT *bool, importMapPath string, fsys afero.Fs) error {
7✔
19
        // Load function config and project id
7✔
20
        if err := utils.LoadConfigFS(fsys); err != nil {
7✔
21
                return err
×
22
        } else if len(slugs) > 0 {
11✔
23
                for _, s := range slugs {
9✔
24
                        if err := utils.ValidateFunctionSlug(s); err != nil {
6✔
25
                                return err
1✔
26
                        }
1✔
27
                }
28
        } else if slugs, err = GetFunctionSlugs(fsys); err != nil {
3✔
29
                return err
×
30
        }
×
31
        // TODO: require all functions to be deployed from config for v2
32
        if len(slugs) == 0 {
7✔
33
                return errors.Errorf("No Functions specified or found in %s", utils.Bold(utils.FunctionsDir))
1✔
34
        }
1✔
35
        functionConfig, err := GetFunctionConfig(slugs, importMapPath, noVerifyJWT, fsys)
5✔
36
        if err != nil {
5✔
37
                return err
×
38
        }
×
39
        api := function.NewEdgeRuntimeAPI(projectRef, *utils.GetSupabase(), NewDockerBundler(fsys))
5✔
40
        if err := api.UpsertFunctions(ctx, functionConfig); err != nil {
5✔
41
                return err
×
42
        }
×
43
        fmt.Printf("Deployed Functions on project %s: %s\n", utils.Aqua(projectRef), strings.Join(slugs, ", "))
5✔
44
        url := fmt.Sprintf("%s/project/%v/functions", utils.GetSupabaseDashboardURL(), projectRef)
5✔
45
        fmt.Println("You can inspect your deployment in the Dashboard: " + url)
5✔
46
        return nil
5✔
47
}
48

49
func GetFunctionSlugs(fsys afero.Fs) (slugs []string, err error) {
6✔
50
        pattern := filepath.Join(utils.FunctionsDir, "*", "index.ts")
6✔
51
        paths, err := afero.Glob(fsys, pattern)
6✔
52
        if err != nil {
6✔
53
                return nil, errors.Errorf("failed to glob function slugs: %w", err)
×
54
        }
×
55
        for _, path := range paths {
11✔
56
                slug := filepath.Base(filepath.Dir(path))
5✔
57
                if utils.FuncSlugPattern.MatchString(slug) {
9✔
58
                        slugs = append(slugs, slug)
4✔
59
                }
4✔
60
        }
61
        // Add all function slugs declared in config file
62
        for slug := range utils.Config.Functions {
8✔
63
                slugs = append(slugs, slug)
2✔
64
        }
2✔
65
        return slugs, nil
6✔
66
}
67

68
func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool, fsys afero.Fs) (config.FunctionConfig, error) {
13✔
69
        // Although some functions do not require import map, it's more convenient to setup
13✔
70
        // vscode deno extension with a single import map for all functions.
13✔
71
        fallbackExists := true
13✔
72
        if _, err := fsys.Stat(utils.FallbackImportMapPath); errors.Is(err, os.ErrNotExist) {
21✔
73
                fallbackExists = false
8✔
74
        } else if err != nil {
13✔
75
                return nil, errors.Errorf("failed to fallback import map: %w", err)
×
76
        }
×
77
        // Flag import map is specified relative to current directory instead of workdir
78
        if len(importMapPath) > 0 && !filepath.IsAbs(importMapPath) {
15✔
79
                importMapPath = filepath.Join(utils.CurrentDirAbs, importMapPath)
2✔
80
        }
2✔
81
        functionConfig := make(config.FunctionConfig, len(slugs))
13✔
82
        for _, name := range slugs {
28✔
83
                function := utils.Config.Functions[name]
15✔
84
                // Precedence order: flag > config > fallback
15✔
85
                functionDir := filepath.Join(utils.FunctionsDir, name)
15✔
86
                if len(function.Entrypoint) == 0 {
24✔
87
                        function.Entrypoint = filepath.Join(functionDir, "index.ts")
9✔
88
                }
9✔
89
                if len(importMapPath) > 0 {
18✔
90
                        function.ImportMap = importMapPath
3✔
91
                } else if len(function.ImportMap) == 0 {
22✔
92
                        denoJsonPath := filepath.Join(functionDir, "deno.json")
7✔
93
                        denoJsoncPath := filepath.Join(functionDir, "deno.jsonc")
7✔
94
                        if _, err := fsys.Stat(denoJsonPath); err == nil {
7✔
NEW
95
                                function.ImportMap = denoJsonPath
×
96
                        } else if _, err := fsys.Stat(denoJsoncPath); err == nil {
7✔
NEW
97
                                function.ImportMap = denoJsoncPath
×
98
                        } else if fallbackExists {
8✔
99
                                function.ImportMap = utils.FallbackImportMapPath
1✔
100
                        }
1✔
101
                }
102
                if noVerifyJWT != nil {
20✔
103
                        function.VerifyJWT = cast.Ptr(!*noVerifyJWT)
5✔
104
                }
5✔
105
                functionConfig[name] = function
15✔
106
        }
107
        return functionConfig, nil
13✔
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc