From 86030ad6de20b2670c03e812e998be34274adc09 Mon Sep 17 00:00:00 2001 From: Edwsel Date: Fri, 25 May 2018 14:14:25 +0300 Subject: [PATCH 1/2] Add opportunity run script from source --- module.go | 10 ++++++---- motto.go | 59 +++++++++++++++++++++++++++++++++++++------------------ utils.go | 9 +++++++++ 3 files changed, 55 insertions(+), 23 deletions(-) diff --git a/module.go b/module.go index e593ae4..c4e95a2 100644 --- a/module.go +++ b/module.go @@ -21,16 +21,16 @@ type ModuleLoader func(*Motto) (otto.Value, error) // // "pwd" indicates current working directory, which might be used to search for // modules. -func CreateLoaderFromSource(source, pwd string) ModuleLoader { +func CreateLoaderFromSource(source, pwd string, filename string) ModuleLoader { return func(vm *Motto) (otto.Value, error) { // Wraps the source to create a module environment - source = "(function(module) {var require = module.require;var exports = module.exports;var __dirname = module.__dirname;\n" + source + "\n})" + source = "(function(module) {var require = module.require;var exports = module.exports;var __dirname = module.__dirname;var __filename = module.__filename;\n" + source + "\n})" // Provide the "require" method in the module scope. jsRequire := func(call otto.FunctionCall) otto.Value { jsModuleName := call.Argument(0).String() - moduleValue, err := vm.Require(jsModuleName, pwd) + moduleValue, err := vm.Require(jsModuleName, pwd, true) if err != nil { jsException(vm, "Error", "motto: "+err.Error()) } @@ -41,6 +41,8 @@ func CreateLoaderFromSource(source, pwd string) ModuleLoader { jsModule, _ := vm.Object(`({exports: {}})`) jsModule.Set("require", jsRequire) jsModule.Set("__dirname", pwd) + jsModule.Set("__filename", filename) + jsExports, _ := jsModule.Get("exports") // Run the module source, with "jsModule" as the "module" variable, "jsExports" as "this"(Nodejs capable). @@ -79,7 +81,7 @@ func CreateLoaderFromFile(filename string) ModuleLoader { pwd := filepath.Dir(filename) - return CreateLoaderFromSource(string(source), pwd)(vm) + return CreateLoaderFromSource(string(source), pwd, filename)(vm) } } diff --git a/motto.go b/motto.go index 562538b..10daa0e 100644 --- a/motto.go +++ b/motto.go @@ -7,7 +7,7 @@ package motto import ( "path/filepath" "sync" - + "errors" "github.com/robertkrimen/otto" ) @@ -41,15 +41,23 @@ type Motto struct { // Run a module or file func (m *Motto) Run(name string) (otto.Value, error) { - if ok, _ := isFile(name); ok { + var isFilePath bool + if isFilePath, _ := isFile(name); isFilePath { name, _ = filepath.Abs(name) } - return m.Require(name, ".") + return m.Require(name, ".", isFilePath) } // Require a module with cache -func (m *Motto) Require(id, pwd string) (otto.Value, error) { +func (m *Motto) Require(id, pwd string, isFilePath bool) (otto.Value, error) { + var source string + + if !isFilePath { + source = id + id = md5(id) + } + if cache, ok := m.cachedModule(id); ok { return cache, nil } @@ -69,26 +77,39 @@ func (m *Motto) Require(id, pwd string) (otto.Value, error) { return v, nil } - filename, err := FindFileModule(id, pwd, append(m.paths, globalPaths...)) - if err != nil { - return otto.UndefinedValue(), err - } + if len(source) > 0 { + v, err := CreateLoaderFromSource(source, pwd, "")(m) - // resove id - id = filename + if err != nil { + return otto.UndefinedValue(), err + } - if cache, ok := m.cachedModule(id); ok { - return cache, nil - } + m.addCachedModule(id, v) + return v, nil + } else { + filename, err := FindFileModule(id, pwd, append(m.paths, globalPaths...)) + if err != nil { + return otto.UndefinedValue(), err + } - v, err := CreateLoaderFromFile(id)(m) + // resove id + id = filename - if err != nil { - return otto.UndefinedValue(), err - } + if cache, ok := m.cachedModule(id); ok { + return cache, nil + } + + v, err := CreateLoaderFromFile(id)(m) + + if err != nil { + return otto.UndefinedValue(), err + } + + m.addCachedModule(id, v) + return v, nil + } - m.addCachedModule(id, v) - return v, nil + return otto.UndefinedValue(), errors.New("Not found data for require") } func (m *Motto) addCachedModule(id string, v otto.Value) { diff --git a/utils.go b/utils.go index 3c884aa..c52ce4b 100644 --- a/utils.go +++ b/utils.go @@ -7,9 +7,18 @@ package motto import ( "encoding/json" "io/ioutil" + "io" "os" + md5base "crypto/md5" ) +func md5(data string) string { + h := md5base.New() + io.WriteString(h,data) + + return string(h.Sum(nil)) +} + func isDir(path string) (bool, error) { fi, err := os.Stat(path) if err != nil { From fd6879980445e75a684921c3eb929ac0b203b9aa Mon Sep 17 00:00:00 2001 From: Edwsel Date: Fri, 25 May 2018 18:03:24 +0300 Subject: [PATCH 2/2] Fixed error! running script from file --- .gitignore | 3 ++- motto.go | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index a76e9f9..95347d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/tests/npm/node_modules \ No newline at end of file +/tests/npm/node_modules +\.idea/ diff --git a/motto.go b/motto.go index 10daa0e..0593c9d 100644 --- a/motto.go +++ b/motto.go @@ -41,11 +41,11 @@ type Motto struct { // Run a module or file func (m *Motto) Run(name string) (otto.Value, error) { - var isFilePath bool - if isFilePath, _ := isFile(name); isFilePath { + isFilePath, _ := isFile(name); + if isFilePath { name, _ = filepath.Abs(name) } - + return m.Require(name, ".", isFilePath) }