Skip to content

Commit

Permalink
Remove dev/build entrypoint input
Browse files Browse the repository at this point in the history
  • Loading branch information
gschier committed Sep 10, 2024
1 parent 85e1fb1 commit f9e6e15
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
12 changes: 9 additions & 3 deletions cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import (
)

var buildCmd = &cobra.Command{
Use: "build",
Use: "build entrypoint",
Short: "Transpile code into a runnable plugin bundle",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
result := api.Build(ESLintBuildOptions(args))
if !fileExists("./package.json") {
ExitError("./package.json does not exist. Ensure that you are in a plugin directory?")
}

srcPath := "./src/index.ts"
fmt.Printf("Building %s...\n", srcPath)

result := api.Build(ESLintBuildOptions([]string{srcPath}))
for _, o := range result.OutputFiles {
fmt.Printf("Compiled to: %s\n", o.Path)
}
Expand Down
12 changes: 8 additions & 4 deletions cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ var devCmd = &cobra.Command{
Use: "dev",
Short: "Build plugin bundle continuously when the filesystem changes",
Long: "Monitor the filesystem and build the plugin bundle when something changes. Useful for plugin development.",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
ctx, errors := api.Context(ESLintBuildOptions(args))
if !fileExists("./package.json") {
ExitError("./package.json does not exist. Ensure that you are in a plugin directory?")
}

srcPath := "./src/index.ts"
ctx, errors := api.Context(ESLintBuildOptions([]string{srcPath}))
if errors != nil {
println("Failed to create esbuild context")
os.Exit(1)
}

fmt.Printf("Watching %s...\n", srcPath)

err := ctx.Watch(api.WatchOptions{})
CheckError(err)

fmt.Printf("watching %s...\n", args[0])

// Returning from main() exits immediately in Go.
// Block forever so that we keep watching and don't exit.
<-make(chan struct{})
Expand Down
28 changes: 1 addition & 27 deletions cmd_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"github.com/spf13/cobra"
"os"
"os/exec"
"path/filepath"
"strings"
)

var generateCmd = &cobra.Command{
Expand All @@ -21,7 +19,7 @@ var generateCmd = &cobra.Command{
pluginDir, err := pterm.DefaultInteractiveTextInput.WithDefaultValue(defaultPath).Show()
CheckError(err)

if dirExists(pluginDir) {
if fileExists(pluginDir) {
returnError("")
}

Expand Down Expand Up @@ -55,30 +53,6 @@ func runCmd(dir, cmd string, args ...string) {
CheckError(c.Wait())
}

func writeFile(path, contents string) {
CheckError(os.MkdirAll(filepath.Dir(path), 0755))
CheckError(os.WriteFile(path, []byte(contents), 0755))
}

func readFile(path string) string {
pkgBytes, err := TemplateFS.ReadFile(path)
CheckError(err)
return string(pkgBytes)
}

func copyFile(relPath, dstDir, name string) {
contents := readFile(filepath.Join("template", relPath))
contents = strings.ReplaceAll(contents, "yaak-plugin-name", name)
writeFile(filepath.Join(dstDir, relPath), contents)
}

func dirExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}

func returnError(msg string) {
pterm.Println(pterm.Red(msg))
}
5 changes: 4 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ func CheckError(err error) {
if err == nil {
return
}
ExitError(err.Error())
}

pterm.Println(pterm.Red(fmt.Sprintf("Error: %s", err.Error())))
func ExitError(msg string) {
pterm.Println(pterm.Red(fmt.Sprintf("Error: %s", msg)))
os.Exit(1)
}
31 changes: 31 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package yaakcli

import (
"os"
"path/filepath"
"strings"
)

func writeFile(path, contents string) {
CheckError(os.MkdirAll(filepath.Dir(path), 0755))
CheckError(os.WriteFile(path, []byte(contents), 0755))
}

func readFile(path string) string {
pkgBytes, err := TemplateFS.ReadFile(path)
CheckError(err)
return string(pkgBytes)
}

func copyFile(relPath, dstDir, name string) {
contents := readFile(filepath.Join("template", relPath))
contents = strings.ReplaceAll(contents, "yaak-plugin-name", name)
writeFile(filepath.Join(dstDir, relPath), contents)
}

func fileExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}

0 comments on commit f9e6e15

Please sign in to comment.