diff --git a/.gitignore b/.gitignore index 970a9f0..dde130f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ nami +nami.exe nami_* *.tgz diff --git a/main.go b/main.go index dc3e898..7310d98 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,7 @@ import ( func main() { app := cli.NewApp() app.Name = "nami" - app.Version = "20220601" + app.Version = "20220606" app.Usage = "The easy way to download anything from anywhere" app.Authors = []*cli.Author{ { @@ -73,11 +73,11 @@ func main() { return nil } for _, v := range c.Args().Slice() { - name, kind, script, err := n.Parse(v) + name, script, err := n.Parse(v) if err != nil { return err } - f, err := n.Install(name, kind, script) + f, err := n.Install(name, script) if err != nil { return err } diff --git a/nami.go b/nami.go index ae83085..a8f9fc2 100644 --- a/nami.go +++ b/nami.go @@ -95,34 +95,16 @@ func (n *Nami) CleanCache() error { return nil } -func (n *Nami) Install(name, kind, script string) (func(), error) { +func (n *Nami) Install(name, script string) (func(), error) { if err := n.CleanCache(); err != nil { return nil, err } - if kind == "tengo" { - ts := tengo.NewScript([]byte(script)) - ts.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...)) - if _, err := ts.Run(); err != nil { - return nil, err - } - } - if kind == "deno" { - deno := filepath.Join(n.BinDir, "deno") - if runtime.GOOS == "windows" { - deno = filepath.Join(n.BinDir, "deno.exe") - } - cmd := exec.Command(deno, "run", "-r", "-A", "--unstable", script) - cmd.Env = append(os.Environ(), - "DENO_DIR="+n.DenoDir, - ) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return nil, err - } + ts := tengo.NewScript([]byte(script)) + ts.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...)) + if _, err := ts.Run(); err != nil { + return nil, err } - p := &Package{ Name: name, Files: make(map[string]string), diff --git a/parse.go b/parse.go index 06a0b50..ddb36f8 100644 --- a/parse.go +++ b/parse.go @@ -19,75 +19,47 @@ import ( "io" "io/ioutil" "net/http" - "os" - "path/filepath" - "runtime" "strings" ) -func (nm *Nami) Parse(input string) (string, string, string, error) { - n := input[strings.LastIndex(input, "/")+1:] - n = strings.TrimSuffix(n, ".tengo") - n = strings.TrimSuffix(n, ".js") - n = strings.TrimSuffix(n, ".ts") - k := "tengo" - if strings.HasSuffix(input, ".js") || strings.HasSuffix(input, ".ts") { - k = "deno" - deno := "deno" - if runtime.GOOS == "windows" { - deno = "deno.exe" - } - _, err := os.Stat(filepath.Join(nm.BinDir, deno)) - if err != nil { - if !os.IsNotExist(err) { - return "", "", "", err - } - return "", "", "", errors.New("Install deno first $ nami install deno") - } - } - s := input - if k == "tengo" && (strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")) { +func (nm *Nami) Parse(input string) (string, string, error) { + if strings.HasSuffix(input, ".tengo") && (strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")) { + n := input[strings.LastIndex(input, "/")+1:] + n = strings.TrimSuffix(n, ".tengo") res, err := http.Get(input) if err != nil { - return "", "", "", err + return "", "", err } defer res.Body.Close() b, err := io.ReadAll(res.Body) if err != nil { - return "", "", "", err + return "", "", err } - s = string(b) if res.StatusCode != 200 { - return "", "", "", errors.New("Package not found") + return "", "", errors.New("Package not found") } + return n, string(b), nil } - if k == "tengo" && !(strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")) { - st, err0 := os.Stat(input) - if err0 != nil && !os.IsNotExist(err0) { - return "", "", "", err0 - } - if err0 != nil || st.IsDir() { - res, err := http.Get("https://raw.githubusercontent.com/txthinking/nami/master/package/" + n + ".tengo") - if err != nil { - return "", "", "", err - } - defer res.Body.Close() - b, err := io.ReadAll(res.Body) - if err != nil { - return "", "", "", err - } - s = string(b) - if res.StatusCode != 200 { - return "", "", "", errors.New("Package not found") - } - } - if err0 == nil && !st.IsDir() { - b, err := ioutil.ReadFile(input) - if err != nil { - return "", "", "", err - } - s = string(b) + if strings.HasSuffix(input, ".tengo") && !(strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")) { + n := input[strings.LastIndex(input, "/")+1:] + n = strings.TrimSuffix(n, ".tengo") + b, err := ioutil.ReadFile(input) + if err != nil { + return "", "", err } + return n, string(b), nil + } + res, err := http.Get("https://raw.githubusercontent.com/txthinking/nami/master/package/" + input + ".tengo") + if err != nil { + return "", "", err + } + defer res.Body.Close() + b, err := io.ReadAll(res.Body) + if err != nil { + return "", "", err + } + if res.StatusCode != 200 { + return "", "", errors.New(res.Status + " " + string(b)) } - return n, k, s, nil + return input, string(b), nil }