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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ $ yaml json write test.yaml
}
```

### toml

You can read and write TOML, too.

To convert from TOML to YAML:

```tty
$ yaml toml read test.toml
foo:
bar: 7
somename:
partone:
one: 7
two:
- some string
- hello world
```

To convert from YAML to TOML:

```tty
$ yaml toml write test-toml.yaml
[foo]
bar = 7
baz = [ "some string", "hello world" ]
```

You can get more help by just typing `yaml`.

```bash
Expand All @@ -111,8 +138,10 @@ Usage: yaml <command> [<args>]
Some useful yaml commands are:
commands List all yaml commands
get Get a value from a YAML file
json Import/export YAML to/from JSON
set Set a value in a YAML file
template Instantiate a template file with a YAML file.
toml Import/export YAML to/from TOML

See 'yaml help <command>' for information on a specific command.
```
Expand Down
10 changes: 9 additions & 1 deletion lib/errors.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/get.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions lib/json.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/set.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/template.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions lib/toml.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions lib/yaml.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions libexec/yaml-toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# Usage: yaml toml [read|write] <path>
# Summary: Import/export YAML to/from TOML
# Help: Read a TOML file and dump YAML.
#
# yaml toml read test.json
#
# Read a YAML file and dump TOML
#
# yaml toml write test.yaml
#
exec node $_YAML_ROOT/lib/toml.js $@
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"homepage": "https://github.com/pandastrike/yaml-cli#readme",
"dependencies": {
"@iarna/toml": "^2.2.1",
"fairmont": "^1.0.0-beta-40",
"js-yaml": "^3.5.3",
"markup-js": "^1.5.21"
Expand Down
8 changes: 8 additions & 0 deletions src/errors.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ module.exports =
formattingJSON: (error) ->
console.error "yaml: unable to format result as JSON\n#{error.message}"
process.exit -1

parsingTOML: (error) ->
console.error "yaml: error parsing TOML\n#{error.message}"
process.exit -1

formattingTOML: (error) ->
console.error "yaml: unable to format result as TOML\n#{error.message}"
process.exit -1
49 changes: 49 additions & 0 deletions src/toml.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{async, call, read} = require "fairmont"
TOML = require "@iarna/toml"
YAML = require "./yaml"
errors = require "./errors"

[subcommand, path] = process.argv[2..]

_TOML =

read: async (_path) ->

path = if _path == "-" then process.stdin else _path

try
toml = yield read path
catch error
errors.readingPath error, path

try
data = TOML.parse toml
catch error
errors.parsingTOML error

data

write: (data) ->

result =
if data?
try
TOML.stringify data
catch error
errors.formattingTOML error
else
""

console.log result

if subcommand? && subcommand in ["read", "write"] && path?
call ->
if subcommand == "write"
_TOML.write yield YAML.read path
else
YAML.write yield _TOML.read path

else
console.error "yaml toml: invalid arguments"
console.error "yaml toml [read|write] <path>"
process.exit -1
5 changes: 5 additions & 0 deletions test/test-toml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo:
bar: 7
baz:
- "some string" # TOML does not allow mixed data types in an array
- "hello world"
16 changes: 16 additions & 0 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,19 @@ Amen.describe "YAML CLI", (context) ->
data = JSON.parse yield sh "bin/yaml json write test/test.yaml"
assert.equal "7", data.foo.bar
assert.equal "hello world", data.foo.baz[1]

context.test "toml", (context) ->

context.test "read", ->
YAML = require "js-yaml"
data = YAML.safeLoad yield sh "bin/yaml toml read test/test.toml"
assert.equal "7", data.foo.bar
assert.equal "7", data.somename.partone.one
assert.equal "hello world", data.somename.partone.two[1]


context.test "write", ->
TOML = require "@iarna/toml"
data = TOML.parse yield sh "bin/yaml toml write test/test-toml.yaml"
assert.equal "7", data.foo.bar
assert.equal "hello world", data.foo.baz[1]
6 changes: 6 additions & 0 deletions test/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[foo]
bar = 7

[somename.partone]
one = 7
two = ["some string", "hello world"]