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 1d776a9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 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
7 changes: 3 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export async function cli(
doc: { type: 'boolean', short: 'd' },
help: { type: 'boolean', short: 'h' },
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' },
yaml: { type: 'string', default: '1.2' }
yaml: { type: 'string', default: '1.2' },
indent: { type: 'string', short: 'i' }
}
})
} catch (error) {
Expand Down Expand Up @@ -190,8 +190,7 @@ 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))
console.log(JSON.stringify(opt.single ? data[0] : data, null, Number(opt.indent)))
}
done()
})
Expand Down
8 changes: 4 additions & 4 deletions tests/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,23 @@ const skip = Number(major) < 20
['[{"hello":"world"},42]']
)
})
describe('--pretty', () => {
describe('--indent', () => {
ok(
'basic',
'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]']
)
})
Expand Down

0 comments on commit 1d776a9

Please sign in to comment.