diff --git a/commands/types/dnscontrol.d.ts b/commands/types/dnscontrol.d.ts index 269e64e34..85e0d1859 100644 --- a/commands/types/dnscontrol.d.ts +++ b/commands/types/dnscontrol.d.ts @@ -110,6 +110,7 @@ interface ResponseHeaders { declare function require(name: `${string}.json`): any; +declare function require(name: `${string}.json5`): any; declare function require(name: string): true; /** diff --git a/commands/types/others.d.ts b/commands/types/others.d.ts index 31e6aa0d7..185ede344 100644 --- a/commands/types/others.d.ts +++ b/commands/types/others.d.ts @@ -1,4 +1,5 @@ declare function require(name: `${string}.json`): any; +declare function require(name: `${string}.json5`): any; declare function require(name: string): true; /** diff --git a/documentation/language-reference/top-level-functions/require.md b/documentation/language-reference/top-level-functions/require.md index b4fc7d6b5..6226e5c46 100644 --- a/documentation/language-reference/top-level-functions/require.md +++ b/documentation/language-reference/top-level-functions/require.md @@ -5,14 +5,14 @@ parameters: ts_ignore: true --- -`require(...)` loads the specified JavaScript or JSON file, allowing +`require(...)` loads the specified JavaScript, JSON, or JSON5 file, allowing to split your configuration across multiple files. A better name for this function might be "include". If the supplied `path` string ends with `.js`, the file is interpreted as JavaScript code, almost as though its contents had been included in -the currently-executing file. If the path string ends with `.json`, +the currently-executing file. If the path string ends with `.json` or `.json5` (case insensitive), `require()` returns the `JSON.parse()` of the file's contents. If the path string begins with a `./`, it is interpreted relative to @@ -81,7 +81,7 @@ function includeK8Sdev() { ``` {% endcode %} -### Example 3: JSON +### Example 3: JSON and JSON5 Requiring JSON files initializes variables: @@ -89,8 +89,11 @@ Requiring JSON files initializes variables: ```javascript var domains = require("./domain-ip-map.json") +var REG_MY_PROVIDER = NewRegistrar("none"); +var DSP_MY_DNSSERVER = NewDnsProvider("none"); + for (var domain in domains) { - D(domain, REG_MY_PROVIDER, PROVIDER, + D(domain, REG_MY_PROVIDER, DSP_MY_DNSSERVER, A("@", domains[domain]) ); } @@ -106,6 +109,11 @@ for (var domain in domains) { ``` {% endcode %} +JSON5 works the same way, but the filename ends in `.json5`. (Note: JSON5 +features are supported whether the filename ends with `.json` or `.json5`. +However please don't rely on JSON5 features in a `.json` file as this may +change some day.) + # Notes `require()` is *much* closer to PHP's `include()` function than it diff --git a/pkg/js/js.go b/pkg/js/js.go index dc054918d..7112e48f2 100644 --- a/pkg/js/js.go +++ b/pkg/js/js.go @@ -157,7 +157,8 @@ func require(call otto.FunctionCall) otto.Value { var value = otto.TrueValue() // If its a json file return the json value, else default to true - if strings.HasSuffix(filepath.Ext(relFile), "json") { + var ext = strings.ToLower(filepath.Ext(relFile)) + if strings.HasSuffix(ext, "json") || strings.HasSuffix(ext, "json5") { cmd := fmt.Sprintf(`JSON.parse(JSON.stringify(%s))`, string(data)) value, err = call.Otto.Run(cmd) } else { diff --git a/pkg/js/parse_tests/049-json5-require.js b/pkg/js/parse_tests/049-json5-require.js new file mode 100644 index 000000000..0c672f2ce --- /dev/null +++ b/pkg/js/parse_tests/049-json5-require.js @@ -0,0 +1,8 @@ +var domains = require('./domain-ip-map.json5') + +var domain = "foo.com" +var ip = domains["foo.com"] + +D(domain,"none", + A("@",ip) +); diff --git a/pkg/js/parse_tests/049-json5-require.json b/pkg/js/parse_tests/049-json5-require.json new file mode 100644 index 000000000..9e9ea7018 --- /dev/null +++ b/pkg/js/parse_tests/049-json5-require.json @@ -0,0 +1,18 @@ +{ + "registrars": [], + "dns_providers": [], + "domains": [ + { + "name": "foo.com", + "registrar": "none", + "dnsProviders": {}, + "records": [ + { + "type": "A", + "name": "@", + "target": "1.1.1.1" + } + ] + } + ] +} diff --git a/pkg/js/parse_tests/domain-ip-map.json5 b/pkg/js/parse_tests/domain-ip-map.json5 new file mode 100644 index 000000000..8eec061e7 --- /dev/null +++ b/pkg/js/parse_tests/domain-ip-map.json5 @@ -0,0 +1,7 @@ +// This is a comment. +{ + "foo.com": "1.1.1.1", + "bar.com": "5.5.5.5", +} + +// The "bar.com" line has a comma at the end.