Skip to content

Commit

Permalink
refactor: Migrate --pretty CLI option to --indent
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbayley committed Jul 1, 2024
1 parent f7fb67f commit 1373797
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/09_cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Usage:
Options:
--help, -h Show this message.
--json, -j Output JSON.
--pretty, -p Output pretty-printed JSON.
--indent 2 Output pretty-printed data, indented by the given number of spaces.

Additional options for bare "yaml" command:
--doc, -d Output pretty-printed JS Document objects.
Expand Down
14 changes: 8 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Usage:
Options:
--help, -h Show this message.
--json, -j Output JSON.
--pretty, -p Output pretty-printed JSON.
--indent 2 Output pretty-printed data, indented by the given number of spaces.
Additional options for bare "yaml" command:
--doc, -d Output pretty-printed JS Document objects.
Expand Down Expand Up @@ -56,8 +56,8 @@ export async function cli(
options: {
doc: { type: 'boolean', short: 'd' },
help: { type: 'boolean', short: 'h' },
indent: { type: 'string', short: 'i' },
json: { type: 'boolean', short: 'j' },
pretty: { type: 'boolean', short: 'p' },
single: { type: 'boolean', short: '1' },
strict: { type: 'boolean', short: 's' },
visit: { type: 'string', short: 'v' },
Expand All @@ -73,6 +73,8 @@ export async function cli(
values: opt
} = args

let indent = Number(opt.indent)

stdin.setEncoding('utf-8')

// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
Expand All @@ -94,7 +96,7 @@ export async function cli(
})
stdin.on('end', () => {
for (const tok of lexer.lex('', false)) add(tok)
if (opt.json) console.log(JSON.stringify(data))
if (opt.json) console.log(JSON.stringify(data, null, indent))
done()
})
break
Expand All @@ -112,7 +114,7 @@ export async function cli(
})
stdin.on('end', () => {
for (const tok of parser.parse('', false)) add(tok)
if (opt.json) console.log(JSON.stringify(data))
if (opt.json) console.log(JSON.stringify(data, null, indent))
done()
})
break
Expand Down Expand Up @@ -161,7 +163,8 @@ export async function cli(
} else {
if (reqDocEnd) console.log('...')
try {
const str = String(doc)
indent ||= 2
const str = doc.toString({ indent })
console.log(str.endsWith('\n') ? str.slice(0, -1) : str)
} catch (error) {
done(error as Error)
Expand Down Expand Up @@ -190,7 +193,6 @@ export async function cli(
)
}
if (mode !== 'valid' && opt.json) {
const indent = opt.pretty ? 2 : 0
console.log(JSON.stringify(opt.single ? data[0] : data, null, indent))
}
done()
Expand Down
76 changes: 72 additions & 4 deletions tests/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,93 @@ const skip = Number(major) < 20
['[{"hello":"world"},42]']
)
})
describe('--pretty', () => {
describe('--indent', () => {
ok(
'basic',
'hello:\n world: 2',
['--indent', '3'],
['hello:\n world: 2']
)
ok(
'--json',
'hello: world',
['--json', '--pretty'],
['--json', '--indent', '2'],
['[\n {\n "hello": "world"\n }\n]']
)
ok(
'--single',
'hello: world',
['--json', '--pretty', '--single'],
['--json', '--indent', '2', '--single'],
['{\n "hello": "world"\n}']
)
ok(
'multiple',
'hello: world\n---\n42',
['--json', '--pretty'],
['--json', '--indent', '2'],
['[\n {\n "hello": "world"\n },\n 42\n]']
)
ok(
'Lexer',
'hello: world',
['lex', '--json', '--indent', '2'],
['[\n "\\u0002",\n "\\u001f",\n "hello",\n ":",\n " ",\n "\\u001f",\n "world"\n]']
)
ok(
'CST parser',
'hello: world',
['cst', '--json', '--indent', '2'],
[JSON.stringify([
{
type: 'document',
offset: 0,
start: [],
value: {
type: 'block-map',
offset: 0,
indent: 0,
items: [
{
start: [],
key: {
type: 'scalar',
offset: 0,
indent: 0,
source: 'hello'
},
sep: [
{
type: 'map-value-ind',
offset: 5,
indent: 0,
source: ':'
},
{
type: "space",
offset: 6,
indent: 0,
source: ' '
}
],
value: {
type: 'scalar',
offset: 7,
indent: 0,
source: 'world',
end: [
{
type: 'newline',
offset: 12,
indent: 0,
source: '\n'
}
]
}
}
]
}
}
], null, 2)]
)
})
describe('--doc', () => {
ok('basic', 'hello: world', ['--doc'], [{ contents: { items: [{}] } }])
Expand Down

0 comments on commit 1373797

Please sign in to comment.