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

supabase / cli / 12751825638

13 Jan 2025 04:38PM UTC coverage: 58.406% (-0.03%) from 58.44%
12751825638

Pull #3038

github

web-flow
Merge 8acec9537 into 888c23692
Pull Request #3038: fix: defaults to json import map

10 of 16 new or added lines in 2 files covered. (62.5%)

9 existing lines in 4 files now uncovered.

7570 of 12961 relevant lines covered (58.41%)

202.7 hits per line

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

88.57
/internal/functions/new/new.go
1
package new
2

3
import (
4
        "context"
5
        _ "embed"
6
        "fmt"
7
        "html/template"
8
        "os"
9
        "path/filepath"
10

11
        "github.com/go-errors/errors"
12
        "github.com/spf13/afero"
13
        "github.com/supabase/cli/internal/utils"
14
        "github.com/supabase/cli/internal/utils/flags"
15
)
16

17
var (
18
        //go:embed templates/index.ts
19
        indexEmbed string
20
        //go:embed templates/deno.json
21
        denoEmbed string
22
        //go:embed templates/.npmrc
23
        npmrcEmbed string
24

25
        indexTemplate = template.Must(template.New("index").Parse(indexEmbed))
26
)
27

28
type indexConfig struct {
29
        URL   string
30
        Token string
31
}
32

33
func Run(ctx context.Context, slug string, fsys afero.Fs) error {
4✔
34
        // 1. Sanity checks.
4✔
35
        funcDir := filepath.Join(utils.FunctionsDir, slug)
4✔
36
        {
8✔
37
                if err := utils.ValidateFunctionSlug(slug); err != nil {
5✔
38
                        return err
1✔
39
                }
1✔
40
        }
41

42
        // 2. Create new function.
43
        {
3✔
44
                if err := utils.MkdirIfNotExistFS(fsys, funcDir); err != nil {
4✔
45
                        return err
1✔
46
                }
1✔
47

48
                // Load config if available
49
                if err := flags.LoadConfig(fsys); err != nil {
4✔
50
                        utils.CmdSuggestion = ""
2✔
51
                }
2✔
52

53
                if err := createTemplateFile(fsys, filepath.Join(funcDir, "index.ts"), indexTemplate, indexConfig{
2✔
54
                        URL:   utils.GetApiUrl("/functions/v1/" + slug),
2✔
55
                        Token: utils.Config.Auth.AnonKey,
2✔
56
                }); err != nil {
3✔
57
                        return errors.Errorf("failed to create function entrypoint: %w", err)
1✔
58
                }
1✔
59

60
                if err := afero.WriteFile(fsys, filepath.Join(funcDir, "deno.json"), []byte(denoEmbed), 0644); err != nil {
1✔
NEW
61
                        return errors.Errorf("failed to create deno.json config: %w", err)
×
UNCOV
62
                }
×
63

64
                if err := afero.WriteFile(fsys, filepath.Join(funcDir, ".npmrc"), []byte(npmrcEmbed), 0644); err != nil {
1✔
65
                        return errors.Errorf("failed to create .npmrc config: %w", err)
×
66
                }
×
67
        }
68

69
        fmt.Println("Created new Function at " + utils.Bold(funcDir))
1✔
70
        return nil
1✔
71
}
72

73
func createTemplateFile(fsys afero.Fs, path string, tmpl *template.Template, data interface{}) error {
2✔
74
        f, err := fsys.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
2✔
75
        if err != nil {
3✔
76
                return err
1✔
77
        }
1✔
78
        defer f.Close()
1✔
79
        return tmpl.Option("missingkey=error").Execute(f, data)
1✔
80
}
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