-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcmd_publish.go
More file actions
114 lines (91 loc) · 2.6 KB
/
cmd_publish.go
File metadata and controls
114 lines (91 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package yaakcli
import (
"archive/zip"
"encoding/json"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"io"
"os"
"path/filepath"
"strings"
"time"
)
var publishCmd = &cobra.Command{
Use: "publish",
Short: "Publish a Yaak plugin version to the plugin registry",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pluginDir, err := os.Getwd()
CheckError(err)
if len(args) > 0 {
pluginDir, err = filepath.Abs(args[0])
CheckError(err)
}
BuildPlugin(pluginDir)
spinner, _ := pterm.DefaultSpinner.WithDelay(1 * time.Second).Start("Publishing plugin...")
zipPipeReader, zipPipeWriter := io.Pipe()
zipWriter := zip.NewWriter(zipPipeWriter)
selected := make(map[string]bool)
optionalFiles := []string{"package-lock.json"}
requiredFiles := []string{"README.md", "package.json", "build/index.js", "src/index.ts"}
for _, name := range optionalFiles {
selected[filepath.ToSlash(filepath.Clean(name))] = true
}
for _, name := range requiredFiles {
selected[filepath.ToSlash(filepath.Clean(name))] = true
_, err := os.Stat(filepath.Join(pluginDir, name))
if err != nil {
pterm.Warning.Printf("Missing required file: %s\n", name)
os.Exit(1)
}
}
spinner.UpdateText("Archiving plugin")
go func() {
defer func() {
CheckError(zipWriter.Close())
CheckError(zipPipeWriter.Close())
}()
err = filepath.Walk(pluginDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
relPath, err := filepath.Rel(pluginDir, path)
if err != nil {
return err
}
relPath = filepath.ToSlash(filepath.Clean(relPath)) // Normalize for zip entries
// Skip non-desired files or files not in src/ or build/ (we want those)
if !strings.HasPrefix(relPath, "src/") && !strings.HasPrefix(relPath, "build/") && !selected[relPath] {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer func(file *os.File) {
err := file.Close()
CheckError(err)
}(file)
writer, err := zipWriter.Create(relPath)
if err != nil {
return err
}
_, err = io.Copy(writer, file)
return err
})
CheckError(err)
}()
spinner.UpdateText("Uploading plugin")
req := NewAPIRequest("POST", "/plugins/publish", zipPipeReader)
body := SendAPIRequest(req)
var response struct {
Version string `json:"version"`
URL string `json:"url"`
}
CheckError(json.Unmarshal(body, &response))
spinner.Success("Plugin published ", response.Version, "\n → ", response.URL)
},
}