From fa3f5b108afbf38e75fa394b9db6838d5aa2eae5 Mon Sep 17 00:00:00 2001 From: Daniel Bayley Date: Sun, 30 Jun 2024 14:41:45 +0100 Subject: [PATCH 1/4] feat: Add --pretty option to CLI tool --- docs/09_cli.md | 1 + src/cli.ts | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/09_cli.md b/docs/09_cli.md index 123cf991..9315ee5a 100644 --- a/docs/09_cli.md +++ b/docs/09_cli.md @@ -16,6 +16,7 @@ Usage: Options: --help, -h Show this message. --json, -j Output JSON. + --pretty, -p Output pretty-printed JSON. Additional options for bare "yaml" command: --doc, -d Output pretty-printed JS Document objects. diff --git a/src/cli.ts b/src/cli.ts index a05ede81..f627fac2 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -24,6 +24,7 @@ Usage: Options: --help, -h Show this message. --json, -j Output JSON. + --pretty, -p Output pretty-printed JSON. Additional options for bare "yaml" command: --doc, -d Output pretty-printed JS Document objects. @@ -56,6 +57,7 @@ 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' }, @@ -188,7 +190,8 @@ export async function cli( ) } if (mode !== 'valid' && opt.json) { - console.log(JSON.stringify(opt.single ? data[0] : data)) + const indent = opt.pretty ? 2 : 0 + console.log(JSON.stringify(opt.single ? data[0] : data, null, indent)) } done() }) From f7fb67fa2e6bb19a9618ca94021ffad3d1dca949 Mon Sep 17 00:00:00 2001 From: Daniel Bayley Date: Sun, 30 Jun 2024 14:42:16 +0100 Subject: [PATCH 2/4] test: Add tests for --pretty CLI option --- tests/cli.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/cli.ts b/tests/cli.ts index f0da5172..c5152010 100644 --- a/tests/cli.ts +++ b/tests/cli.ts @@ -114,6 +114,26 @@ const skip = Number(major) < 20 ['[{"hello":"world"},42]'] ) }) + describe('--pretty', () => { + ok( + 'basic', + 'hello: world', + ['--json', '--pretty'], + ['[\n {\n "hello": "world"\n }\n]'] + ) + ok( + '--single', + 'hello: world', + ['--json', '--pretty', '--single'], + ['{\n "hello": "world"\n}'] + ) + ok( + 'multiple', + 'hello: world\n---\n42', + ['--json', '--pretty'], + ['[\n {\n "hello": "world"\n },\n 42\n]'] + ) + }) describe('--doc', () => { ok('basic', 'hello: world', ['--doc'], [{ contents: { items: [{}] } }]) ok( From 13737972026b5b9147b916560d0438c82be33b0e Mon Sep 17 00:00:00 2001 From: Daniel Bayley Date: Mon, 1 Jul 2024 14:14:44 +0100 Subject: [PATCH 3/4] refactor: Migrate --pretty CLI option to --indent --- docs/09_cli.md | 2 +- src/cli.ts | 14 ++++++---- tests/cli.ts | 76 +++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/docs/09_cli.md b/docs/09_cli.md index 9315ee5a..0788bcb1 100644 --- a/docs/09_cli.md +++ b/docs/09_cli.md @@ -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. diff --git a/src/cli.ts b/src/cli.ts index f627fac2..9d3529d2 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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. @@ -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' }, @@ -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 @@ -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 @@ -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 @@ -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) @@ -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() diff --git a/tests/cli.ts b/tests/cli.ts index c5152010..7c400c69 100644 --- a/tests/cli.ts +++ b/tests/cli.ts @@ -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: [{}] } }]) From a633de3b5f354ad1eaec001a741e177d4e47b212 Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Sun, 7 Jul 2024 11:42:45 +0300 Subject: [PATCH 4/4] Add missing newline --- tests/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cli.ts b/tests/cli.ts index 7c400c69..f8aaf7bb 100644 --- a/tests/cli.ts +++ b/tests/cli.ts @@ -147,7 +147,7 @@ const skip = Number(major) < 20 ) ok( 'CST parser', - 'hello: world', + 'hello: world\n', ['cst', '--json', '--indent', '2'], [JSON.stringify([ {