Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): support TOML configuration files (#4877)
* Add initial support for TOML in configuration files * Missed brace * Fix snapshots * refactor: move loadToml to utils * Use @iarna/toml * Add tests for loadToml * Fix test for CI * Remove jest-extended * Create snapshot for load-toml * Add feat to docs * lint docs * Use @iarna/toml/parse-string * Change path string
- Loading branch information
1 parent
42ba638
commit 7d78ce6
Showing
12 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"use strict"; | ||
|
||
const parse = require("@iarna/toml/parse-string"); | ||
|
||
module.exports = function(filePath, content) { | ||
try { | ||
return parse(content); | ||
} catch (error) { | ||
error.message = `TOML Error in ${filePath}:\n${error.message}`; | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests_integration/__tests__/__snapshots__/load-toml.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`TOML loads toml successfully 1`] = ` | ||
Object { | ||
"database": Object { | ||
"connection_max": 5000, | ||
"enabled": true, | ||
"ports": Array [ | ||
8001, | ||
8001, | ||
8002, | ||
], | ||
"server": "192.168.1.1", | ||
Symbol(type): Symbol(table), | ||
Symbol(declared): true, | ||
}, | ||
"owner": Object { | ||
"dob": 1979-05-27T15:32:00.000Z, | ||
"name": "Tom Preston-Werner", | ||
Symbol(type): Symbol(table), | ||
Symbol(declared): true, | ||
}, | ||
"title": "TOML Example", | ||
Symbol(type): Symbol(table), | ||
Symbol(declared): false, | ||
} | ||
`; | ||
|
||
exports[`TOML throws error on incorrect toml 1`] = ` | ||
"TOML Error in example.toml: | ||
Unknown character \\"47\\" at row 1, col 2, pos 1: | ||
1> ///ERROR/// | ||
^ | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"use strict"; | ||
|
||
const loadToml = require("../../src/utils/load-toml"); | ||
|
||
describe("TOML", () => { | ||
const exampleFilePath = "example.toml"; | ||
|
||
const exampleToml = ` | ||
# This is a TOML document. | ||
title = "TOML Example" | ||
[owner] | ||
name = "Tom Preston-Werner" | ||
dob = 1979-05-27T07:32:00-08:00 # First class dates | ||
[database] | ||
server = "192.168.1.1" | ||
ports = [ 8001, 8001, 8002 ] | ||
connection_max = 5000 | ||
enabled = true | ||
`; | ||
|
||
const wrongToml = "///ERROR///"; | ||
|
||
test("loads toml successfully", () => { | ||
const parsedToml = loadToml(exampleFilePath, exampleToml); | ||
expect(parsedToml).toMatchSnapshot(); | ||
}); | ||
|
||
test("throws error on incorrect toml", () => { | ||
expect(() => { | ||
loadToml(exampleFilePath, wrongToml); | ||
}).toThrow(); | ||
|
||
expect(() => { | ||
loadToml(exampleFilePath, wrongToml); | ||
}).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
trailingComma = "all" | ||
singleQuote = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function rcToml() { | ||
console.log.apply(null, [ | ||
"rc-toml/file.js", | ||
"should have trailing comma", | ||
"and single quotes" | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters