Skip to content

Commit

Permalink
clean deno
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Jun 7, 2022
1 parent a12b52c commit ce619f1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
nami
nami.exe
nami_*
*.tgz
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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
}
Expand Down
28 changes: 5 additions & 23 deletions nami.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
84 changes: 28 additions & 56 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ce619f1

Please sign in to comment.