diff --git a/core/grpc_download.go b/core/grpc_asset.go similarity index 57% rename from core/grpc_download.go rename to core/grpc_asset.go index 3a42147..d5ea337 100644 --- a/core/grpc_download.go +++ b/core/grpc_asset.go @@ -2,10 +2,12 @@ package core import ( "archive/zip" + "fmt" "io" "io/fs" "net/http" "os" + "os/exec" "path/filepath" "runtime" "strings" @@ -47,6 +49,36 @@ func initLanguage() { if !(item.Os == runtime.GOOS && item.Arch == runtime.GOARCH) { continue } + node_dir := utils.ExecPath + "/language/" + item.Name + os.MkdirAll(node_dir, 0755) + path := os.Getenv("PATH") + newPath := "" + if path != "" { + newPath = fmt.Sprintf("%s:%s", node_dir, path) + } else { + newPath = node_dir + } + os.Setenv("PATH", newPath) + if _, err := os.Stat(node_dir + "/yarn"); err != nil { + resp, err := http.Get("https://gitee.com/sillybot/binary/releases/download/yarn/yarn.zip") + if err == nil { + go func() { + defer resp.Body.Close() + zipfile := node_dir + "/yarn.zip" + f, err := os.OpenFile(zipfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755) + if err != nil { + return + } + defer f.Close() + _, err = io.Copy(f, resp.Body) + if err != nil { + return + } + defer os.Remove(zipfile) + unzip(zipfile, 0777, false) + }() + } + } func() { dir := utils.ExecPath + "/language/" + item.Name data, _ := os.ReadFile(dir + "/version") @@ -54,7 +86,6 @@ func initLanguage() { return } console.Log("正在安装", item.Name, "执行环境....") - os.MkdirAll(utils.ExecPath+"/language/"+item.Name, 0755) resp, err := http.Get(item.Links[0]) if err != nil { return @@ -72,7 +103,7 @@ func initLanguage() { return } defer os.Remove(zipfile) - if err := unzip(zipfile, 0755); err == nil { + if err := unzip(zipfile, 0755, false); err == nil { os.WriteFile(dir+"/version", []byte(item.Version), 0755) } else { // fmt.Println(err) @@ -80,22 +111,23 @@ func initLanguage() { console.Log("安装", item.Name, "执行环境成功") }() } - // }() } -func unzip(filename string, perm fs.FileMode) error { +func unzip(filename string, perm fs.FileMode, pkg bool) error { zipFile, err := zip.OpenReader(filename) if err != nil { return err } defer zipFile.Close() - + top := "" for _, file := range zipFile.File { + if top == "" { + top = strings.Split(file.Name, "/")[0] + } // 忽略以 "__MACOSX/" 开头的文件 if strings.HasPrefix(file.Name, "__MACOSX/") { continue } - path := filepath.Join(filepath.Dir(filename), file.Name) if file.FileInfo().IsDir() { // 如果是目录则创建目录 @@ -109,26 +141,41 @@ func unzip(filename string, perm fs.FileMode) error { if err != nil { return err } + var de = func() error { + // 创建文件并解压缩数据 + zipFile, err := file.Open() + if err != nil { + return err + } + defer zipFile.Close() - // 创建文件并解压缩数据 - zipFile, err := file.Open() - if err != nil { - return err + localFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) + if err != nil { + return err + } + defer localFile.Close() + _, err = io.Copy(localFile, zipFile) + if err != nil { + return err + } + return nil } - defer zipFile.Close() + if file.Name != top+"/main.js" { + de() + } else { + if pkg { + defer func() { //安装依赖 + cmd := exec.Command(utils.ExecPath+"/language/node/yarn/bin/yarn", "install") + cmd.Dir = utils.ExecPath + "/plugins/" + top + console.Log(cmd.Output()) + de() + }() + } else { + de() + } - localFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) - if err != nil { - return err - } - defer localFile.Close() - - _, err = io.Copy(localFile, zipFile) - if err != nil { - return err } } } - return nil } diff --git a/core/grpc_plugins.go b/core/grpc_plugins.go index c460ad3..53693b6 100644 --- a/core/grpc_plugins.go +++ b/core/grpc_plugins.go @@ -203,6 +203,8 @@ func AddNodePlugin(path, name string) error { if name == "" { return nil } + uuid := nameUuid(name) + plugins.Set(uuid, "") pluginLock.Lock() defer pluginLock.Unlock() @@ -218,7 +220,7 @@ func AddNodePlugin(path, name string) error { if script == "" { return nil } - uuid := nameUuid(name) + plugins_id.Store(uuid, path) // fmt.Println("add,", uuid, name) f, cbs := pluginParse(script, uuid) diff --git a/core/node_os.go b/core/node_os.go index 8b35bf2..9f9141b 100644 --- a/core/node_os.go +++ b/core/node_os.go @@ -2,12 +2,15 @@ package core import ( "errors" + "fmt" "io/ioutil" "os" + "os/exec" "path/filepath" "runtime" "github.com/axgle/mahonia" + "github.com/cdle/sillyplus/utils" "github.com/dop251/goja" ) @@ -20,7 +23,27 @@ func getJsOs(vm *goja.Runtime, running func() bool) *goja.Object { } return data }) - + type ExecRequest struct { + Dir string `json:"dir"` + Command []string `json:"command"` + Env []string `json:"env"` + Path string `json:"path"` + } + jsos.Set("path", utils.ExecPath) + jsos.Set("exec", func(er ExecRequest) string { + cmd := exec.Command(er.Command[0], er.Command[1:]...) + cmd.Dir = er.Dir + cmd.Env = er.Env + if er.Path != "" { + cmd.Path = er.Path + } + fmt.Println("==", cmd.Path) + data, err := cmd.Output() + if err != nil { + panic(Error(vm, err)) + } + return string(data) + }) jsos.Set("readFileSync", func(path string, decode string) string { // 读取文件内容 content, err := ioutil.ReadFile(path) diff --git a/core/plugin_core.go b/core/plugin_core.go index cd24c75..712a50d 100644 --- a/core/plugin_core.go +++ b/core/plugin_core.go @@ -168,7 +168,7 @@ func initPlugins() { } } defer os.Remove(zipfile) - if err := unzip(zipfile, 0755); err != nil { + if err := unzip(zipfile, 0755, true); err != nil { return &storage.Final{ Error: errors.New("安装异常!" + err.Error()), } diff --git a/core/plugin_publish.go b/core/plugin_publish.go index 5523c28..2eee1fa 100644 --- a/core/plugin_publish.go +++ b/core/plugin_publish.go @@ -36,7 +36,7 @@ func CheckPluginAddress(address string) error { func initPluginPublish() { storage.Watch(sillyGirl, "plugin_sublink", func(old, address, key string) *storage.Final { - if strings.HasPrefix(address, "sub://") { + if strings.HasPrefix(address, "link://") { return nil } if err := CheckPluginAddress(address); err != nil { diff --git a/core/web.go b/core/web.go index dd5f142..9db7732 100644 --- a/core/web.go +++ b/core/web.go @@ -75,8 +75,8 @@ func initWeb() { uuid := c.Query("uuid") for _, f := range Functions { if f.UUID == uuid && f.Public { + plugin_downloads.Set(f.UUID, plugin_downloads.GetInt(f.UUID)+1) if f.Type == "goja" { - plugin_downloads.Set(f.UUID, plugin_downloads.GetInt(f.UUID)+1) c.String(200, publicScript(plugins.GetString(f.UUID))) return } else { @@ -97,13 +97,19 @@ func initWeb() { if err != nil { return err } + + if info.IsDir() && info.Name() == "node_modules" { + return filepath.SkipDir + } + if info.IsDir() { return nil } - // 将路径转换为相对路径 + // 将路径转换为相对路径 relPath, err := filepath.Rel(dir, path) is_index := relPath == "main.js" + relPath = name + "/" + relPath if err != nil { return err diff --git a/main.js b/main.js index 4894ddf..d7cb638 100755 --- a/main.js +++ b/main.js @@ -1 +1,9 @@ -/// \ No newline at end of file +/** + * @title . + * @create_at 2023-07-26 11:07:19 + * @description 🐒这个人很懒什么都没有留下 + * @author 佚名 + * @version v1.0.0 + */ + + const { sender: s, Bucket, Adapter, sleep } = require("sillygirl"); \ No newline at end of file diff --git a/proto3/note.sh b/proto3/note.sh index 71f0a11..76b218f 100644 --- a/proto3/note.sh +++ b/proto3/note.sh @@ -25,4 +25,7 @@ cp /Users/a1-6/Code/sillyplus/proto3/dist/sillygirl.js /Users/a1-6/Code/node/nod #压缩 cd /Users/a1-6/Code/nodes/node_darwin_arm64 && zip node_darwin_arm64.zip node -cd /Users/a1-6/Code/nodes/node_linux_amd64 && zip node_linux_amd64.zip node \ No newline at end of file +cd /Users/a1-6/Code/nodes/node_linux_amd64 && zip node_linux_amd64.zip node + +## +git add . && git commit -m "x" && git push \ No newline at end of file