From efb8e9bbf491aede6395d8aaf9124cbb7785a3ea Mon Sep 17 00:00:00 2001 From: Craig Peterson Date: Mon, 27 Mar 2017 16:01:12 -0600 Subject: [PATCH] Adding simple require functionality to js (#58) * Adding simple require functionality to js * comment --- js/js.go | 18 ++++++++++++++++++ js/js_test.go | 4 +++- js/parse_tests/008-import.js | 2 ++ js/parse_tests/008-import.json | 19 +++++++++++++++++++ js/parse_tests/import.js | 3 +++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 js/parse_tests/008-import.js create mode 100644 js/parse_tests/008-import.json create mode 100644 js/parse_tests/import.js diff --git a/js/js.go b/js/js.go index 07ac236d3..115ffd675 100644 --- a/js/js.go +++ b/js/js.go @@ -2,6 +2,8 @@ package js import ( "encoding/json" + "fmt" + "io/ioutil" "github.com/StackExchange/dnscontrol/models" @@ -14,6 +16,8 @@ import ( func ExecuteJavascript(script string, devMode bool) (*models.DNSConfig, error) { vm := otto.New() + vm.Set("require", require) + helperJs := GetHelpers(devMode) // run helper script to prime vm and initialize variables if _, err := vm.Run(helperJs); err != nil { @@ -44,3 +48,17 @@ func ExecuteJavascript(script string, devMode bool) (*models.DNSConfig, error) { func GetHelpers(devMode bool) string { return _escFSMustString(devMode, "/helpers.js") } + +func require(call otto.FunctionCall) otto.Value { + file := call.Argument(0).String() + fmt.Printf("requiring: %s\n", file) + data, err := ioutil.ReadFile(file) + if err != nil { + panic(err) + } + _, err = call.Otto.Run(string(data)) + if err != nil { + panic(err) + } + return otto.TrueValue() +} diff --git a/js/js_test.go b/js/js_test.go index 35d4a3158..9178625de 100644 --- a/js/js_test.go +++ b/js/js_test.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "testing" + "unicode" "github.com/StackExchange/dnscontrol/models" ) @@ -25,7 +26,8 @@ func TestParsedFiles(t *testing.T) { t.Fatal(err) } for _, f := range files { - if filepath.Ext(f.Name()) != ".js" { + //run all js files that start with a number. Skip others. + if filepath.Ext(f.Name()) != ".js" || !unicode.IsNumber(rune(f.Name()[0])) { continue } t.Log(f.Name(), "------") diff --git a/js/parse_tests/008-import.js b/js/parse_tests/008-import.js new file mode 100644 index 000000000..99e27afef --- /dev/null +++ b/js/parse_tests/008-import.js @@ -0,0 +1,2 @@ + +require("js/parse_tests/import.js") diff --git a/js/parse_tests/008-import.json b/js/parse_tests/008-import.json new file mode 100644 index 000000000..63e441356 --- /dev/null +++ b/js/parse_tests/008-import.json @@ -0,0 +1,19 @@ +{ + "registrars": [], + "dns_providers": [], + "domains": [ + { + "name": "foo.com", + "registrar": "none", + "dnsProviders": {}, + "records": [ + { + "type": "A", + "name": "@", + "target": "1.2.3.4" + } + ], + "keepunknown": false + } + ] + } \ No newline at end of file diff --git a/js/parse_tests/import.js b/js/parse_tests/import.js new file mode 100644 index 000000000..f598ccae8 --- /dev/null +++ b/js/parse_tests/import.js @@ -0,0 +1,3 @@ +D("foo.com","none", + A("@","1.2.3.4") +); \ No newline at end of file