FEATURE: Enable "require()" function to accept json5 files (#3003)

Co-authored-by: charliez0 <charliez0sp@gmail.com>
This commit is contained in:
Tom Limoncelli 2024-06-11 15:27:11 -04:00 committed by GitHub
parent 2ccef49183
commit 8eb3c65050
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 49 additions and 5 deletions

View file

@ -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;
/**

View file

@ -1,4 +1,5 @@
declare function require(name: `${string}.json`): any;
declare function require(name: `${string}.json5`): any;
declare function require(name: string): true;
/**

View file

@ -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

View file

@ -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 {

View file

@ -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)
);

View file

@ -0,0 +1,18 @@
{
"registrars": [],
"dns_providers": [],
"domains": [
{
"name": "foo.com",
"registrar": "none",
"dnsProviders": {},
"records": [
{
"type": "A",
"name": "@",
"target": "1.1.1.1"
}
]
}
]
}

View file

@ -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.