From 51e3ff958516f0672921aae6d8bbbef5dcfc13c9 Mon Sep 17 00:00:00 2001 From: EviSong Date: Sun, 6 Jun 2021 16:42:10 +0800 Subject: [PATCH 1/2] [bin] wrap + unwrap cli support non-TTY (stdin) --- bin/unwrap.js | 47 ++++++++++++++++++++++++++++++----------------- bin/wrap.js | 45 +++++++++++++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 33 deletions(-) diff --git a/bin/unwrap.js b/bin/unwrap.js index e5174c0..4b24ca5 100644 --- a/bin/unwrap.js +++ b/bin/unwrap.js @@ -3,24 +3,37 @@ 'use strict'; const attr = require('../lib/dynamodb-data-types').AttributeValue; -const content = process.argv[2]; -if (!content) { - throw new Error('Input data required.') +if (process.stdin.isTTY) { + unwrap(process.argv[2]); +} else { + let data = ""; + process.stdin.on('data', function(chunk) { + data += chunk; + }); + process.stdin.on('end', function() { + unwrap(data); + }); } -let payload = ''; -try { - payload = JSON.parse(content); -} -catch (err) { - payload = content -} -const output = attr.unwrap(payload); +function unwrap(content) { + if (!content) { + throw new Error('Input data required.') + } -if (typeof output === 'object') { - process.stdout.write(JSON.stringify(output) + '\n'); -} -else { - process.stdout.write(output + '\n'); -} + let payload = ''; + try { + payload = JSON.parse(content); + } + catch (err) { + payload = content + } + const output = attr.unwrap(payload); + + if (typeof output === 'object') { + process.stdout.write(JSON.stringify(output) + '\n'); + } + else { + process.stdout.write(output + '\n'); + } +} \ No newline at end of file diff --git a/bin/wrap.js b/bin/wrap.js index 25f25a7..2082888 100644 --- a/bin/wrap.js +++ b/bin/wrap.js @@ -3,24 +3,37 @@ 'use strict'; const attr = require('../lib/dynamodb-data-types').AttributeValue; -const content = process.argv[2]; -if (!content) { - throw new Error('Input data required.') +if (process.stdin.isTTY) { + wrap(process.argv[2]); +} else { + let data = ""; + process.stdin.on('data', function(chunk) { + data += chunk; + }); + process.stdin.on('end', function() { + wrap(data); + }); } -let payload = ''; -try { - payload = JSON.parse(content); -} -catch (err) { - payload = content -} -const output = attr.wrap(payload); +function wrap(content) { + if (!content) { + throw new Error('Input data required.') + } -if (typeof output === 'object') { - process.stdout.write(JSON.stringify(output) + '\n'); -} -else { - process.stdout.write(output + '\n'); + let payload = ''; + try { + payload = JSON.parse(content); + } + catch (err) { + payload = content + } + const output = attr.wrap(payload); + + if (typeof output === 'object') { + process.stdout.write(JSON.stringify(output) + '\n'); + } + else { + process.stdout.write(output + '\n'); + } } From db9efb48e633d2b4ef43615df3f54401bc54c3e6 Mon Sep 17 00:00:00 2001 From: EviSong Date: Sun, 6 Jun 2021 16:50:53 +0800 Subject: [PATCH 2/2] Updated `Use in cli` section --- README.md | 4 ++++ bin/unwrap.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a943298..914cfaf 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,10 @@ Use with the cli for quick utility dynamo-dt-attr-wrap '{"hello":"world"}' dynamo-dt-attr-unwrap '{"hello": {"S": "world"}}' +Pipe is also supported + + echo '{"hello":"world"}' | dynamo-dt-attr-wrap + echo '{"hello": {"S": "world"}}' | dynamo-dt-attr-unwrap ### Use in the browser diff --git a/bin/unwrap.js b/bin/unwrap.js index 4b24ca5..7bd9215 100644 --- a/bin/unwrap.js +++ b/bin/unwrap.js @@ -36,4 +36,4 @@ function unwrap(content) { else { process.stdout.write(output + '\n'); } -} \ No newline at end of file +}