From fba5d95aebf13f45cec19967986a4b23e4625c3c Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 18 Oct 2020 09:55:09 +0200 Subject: [PATCH] Add fileType 'stdout' to print JSON-results on stdout for easy unix-style integration of this tool (cherry picked from commit c97950cc3813acc51923d3b8eb8878d4b0a01f41) --- bin/cli.js | 4 ++-- lib/Amazon.js | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index f99ef62..687f5d9 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -80,8 +80,8 @@ require('yargs') }, filetype: { default: 'csv', - choices: ['csv', 'json', 'all', ''], - describe: "Type of the output file where the data will be saved. 'all' - save data to the 'json' and 'csv' files", + choices: ['csv', 'json', 'all', 'stdout', ''], + describe: "Type of the output file where the data will be saved. 'stdout' - print json to stdout. 'all' - save data to the 'json' and 'csv' files", }, sort: { default: false, diff --git a/lib/Amazon.js b/lib/Amazon.js index d272d74..75901de 100644 --- a/lib/Amazon.js +++ b/lib/Amazon.js @@ -243,7 +243,7 @@ class AmazonScraper { if (this.cli) { spinner.stop(); } - if (this.fileType && this.cli) { + if (this.fileType && this.cli && this.fileType !== "stdout") { console.log(`Result was saved to: ${this.fileName}`); } return { @@ -310,6 +310,9 @@ class AmazonScraper { async saveResultToFile() { if (this.collector.length) { switch (this.fileType) { + case 'stdout': + console.log(JSON.stringify(this.collector)); + break; case 'json': await fromCallback((cb) => writeFile(`${this.fileName}.json`, JSON.stringify(this.collector), cb)); break;