Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/tests/npm/node_modules
/tests/npm/node_modules
\.idea/
10 changes: 6 additions & 4 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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).
Expand Down Expand Up @@ -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)
}
}

Expand Down
61 changes: 41 additions & 20 deletions motto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package motto
import (
"path/filepath"
"sync"

"errors"
"github.com/robertkrimen/otto"
)

Expand Down Expand Up @@ -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 {
isFilePath, _ := isFile(name);
if 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
}
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down