Skip to content

Commit 0f29ce6

Browse files
authored
feat: Add --merge option to CLI tool (#611)
1 parent e00cab9 commit 0f29ce6

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

docs/09_cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Options:
1717
--help, -h Show this message.
1818
--json, -j Output JSON.
1919
--indent 2 Output pretty-printed data, indented by the given number of spaces.
20+
--merge, -m Enable support for "<<" merge keys.
2021

2122
Additional options for bare "yaml" command:
2223
--doc, -d Output pretty-printed JS Document objects.

src/cli.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Options:
2727
--help, -h Show this message.
2828
--json, -j Output JSON.
2929
--indent 2 Output pretty-printed data, indented by the given number of spaces.
30+
--merge, -m Enable support for "<<" merge keys.
3031
3132
Additional options for bare "yaml" command:
3233
--doc, -d Output pretty-printed JS Document objects.
@@ -59,6 +60,7 @@ export async function cli(
5960
doc: { type: 'boolean', short: 'd' },
6061
help: { type: 'boolean', short: 'h' },
6162
indent: { type: 'string', short: 'i' },
63+
merge: { type: 'boolean', short: 'm' },
6264
json: { type: 'boolean', short: 'j' },
6365
single: { type: 'boolean', short: '1' },
6466
strict: { type: 'boolean', short: 's' },
@@ -127,7 +129,7 @@ export async function cli(
127129
const lineCounter = new LineCounter()
128130
const parser = new Parser(lineCounter.addNewLine)
129131
// @ts-expect-error Version is validated at runtime
130-
const composer = new Composer({ version: opt.yaml })
132+
const composer = new Composer({ version: opt.yaml, merge: opt.merge })
131133
const visitor: visitor | null = opt.visit
132134
? (await import(resolve(opt.visit))).default
133135
: null

tests/cli.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,32 @@ const skip = Number(major) < 20
208208
]
209209
)
210210
})
211+
describe('--merge', () => {
212+
ok(
213+
'can be set',
214+
'hello:\n world: 2\nfoo:\n world: 2',
215+
['--merge', '--json'],
216+
['[{"hello":{"world":2},"foo":{"world":2}}]']
217+
)
218+
ok(
219+
'basic',
220+
'hello: &a\n world: 2\nfoo:\n <<: *a',
221+
['--merge', '--json'],
222+
['[{"hello":{"world":2},"foo":{"world":2}}]']
223+
)
224+
ok(
225+
'also enabled with --yaml=1.1',
226+
'hello: &a\n world: 2\nfoo:\n <<: *a',
227+
['--yaml=1.1', '--json'],
228+
['[{"hello":{"world":2},"foo":{"world":2}}]']
229+
)
230+
ok(
231+
'not enabled by default',
232+
'hello: &a\n world: 2\nfoo:\n <<: *a',
233+
['--json'],
234+
['[{"hello":{"world":2},"foo":{"<<":{"world":2}}}]']
235+
)
236+
})
211237
describe('--doc', () => {
212238
ok('basic', 'hello: world', ['--doc'], [{ contents: { items: [{}] } }])
213239
ok(

0 commit comments

Comments
 (0)