Skip to content
Merged
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
1 change: 1 addition & 0 deletions docs/09_cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Options:
--help, -h Show this message.
--json, -j Output JSON.
--indent 2 Output pretty-printed data, indented by the given number of spaces.
--merge, -m Enable support for "<<" merge keys.

Additional options for bare "yaml" command:
--doc, -d Output pretty-printed JS Document objects.
Expand Down
4 changes: 3 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Options:
--help, -h Show this message.
--json, -j Output JSON.
--indent 2 Output pretty-printed data, indented by the given number of spaces.
--merge, -m Enable support for "<<" merge keys.

Additional options for bare "yaml" command:
--doc, -d Output pretty-printed JS Document objects.
Expand Down Expand Up @@ -59,6 +60,7 @@ export async function cli(
doc: { type: 'boolean', short: 'd' },
help: { type: 'boolean', short: 'h' },
indent: { type: 'string', short: 'i' },
merge: { type: 'boolean', short: 'm' },
json: { type: 'boolean', short: 'j' },
single: { type: 'boolean', short: '1' },
strict: { type: 'boolean', short: 's' },
Expand Down Expand Up @@ -127,7 +129,7 @@ export async function cli(
const lineCounter = new LineCounter()
const parser = new Parser(lineCounter.addNewLine)
// @ts-expect-error Version is validated at runtime
const composer = new Composer({ version: opt.yaml })
const composer = new Composer({ version: opt.yaml, merge: opt.merge })
const visitor: visitor | null = opt.visit
? (await import(resolve(opt.visit))).default
: null
Expand Down
26 changes: 26 additions & 0 deletions tests/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,32 @@ const skip = Number(major) < 20
]
)
})
describe('--merge', () => {
ok(
'can be set',
'hello:\n world: 2\nfoo:\n world: 2',
['--merge', '--json'],
['[{"hello":{"world":2},"foo":{"world":2}}]']
)
ok(
'basic',
'hello: &a\n world: 2\nfoo:\n <<: *a',
['--merge', '--json'],
['[{"hello":{"world":2},"foo":{"world":2}}]']
)
ok(
'also enabled with --yaml=1.1',
'hello: &a\n world: 2\nfoo:\n <<: *a',
['--yaml=1.1', '--json'],
['[{"hello":{"world":2},"foo":{"world":2}}]']
)
ok(
'not enabled by default',
'hello: &a\n world: 2\nfoo:\n <<: *a',
['--json'],
['[{"hello":{"world":2},"foo":{"<<":{"world":2}}}]']
)
})
describe('--doc', () => {
ok('basic', 'hello: world', ['--doc'], [{ contents: { items: [{}] } }])
ok(
Expand Down