From e24aaabee3c7019e7997ef209676860c3c71acf8 Mon Sep 17 00:00:00 2001 From: IGOR DE PAULA SILVA Date: Mon, 30 Aug 2021 22:25:32 -0300 Subject: [PATCH 1/3] Unix pipeline option --- index.ts | 74 ++++++++++++++++++++++++++++++++------------ lib/stream_writer.ts | 2 +- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/index.ts b/index.ts index 9ff7839..466559d 100644 --- a/index.ts +++ b/index.ts @@ -3,9 +3,9 @@ import * as yargs from 'yargs'; import * as fs from 'fs'; import * as path from 'path'; -import {StreamWriter, NopWriter, Emitter} from './lib/index'; +import { StreamWriter, NopWriter, Emitter } from './lib/index'; -const argv = yargs.usage('Usage: $0 [options] inputFile rootName') +const argv = yargs.usage('Usage: $0 [options] inputFile rootName\nFrom stdin, outputs interface by default.') .alias('i', 'interface-file') .string('i') .describe('i', 'Specify output file for interfaces') @@ -16,25 +16,59 @@ const argv = yargs.usage('Usage: $0 [options] inputFile rootName') .alias('h', 'help') .argv; -let interfaceWriter = new NopWriter(); -let proxyWriter = interfaceWriter; -if (argv.i && argv.p && path.resolve(argv.i) === path.resolve(argv.p)) { - console.error(`Interfaces and proxies cannot be written to same file.`); - yargs.showHelp(); - process.exit(1); -} -if (argv.i) { - interfaceWriter = new StreamWriter(fs.createWriteStream(argv.i)); +if (process.stdin.isTTY) { + handleShellArguments(); } -if (argv.p) { - proxyWriter = new StreamWriter(fs.createWriteStream(argv.p)); +else { + handlePipedContent(); } -if (argv._.length !== 2) { - console.error(`Please supply an input file with samples in a JSON array, and a symbol to use for the root interface / proxy.`); - yargs.showHelp(); - process.exit(1); + +let interfaceWriter = new NopWriter(); +let proxyWriter = interfaceWriter; + +function handleShellArguments(){ + if (argv.i && argv.p && path.resolve(argv.i) === path.resolve(argv.p)) { + console.error(`Interfaces and proxies cannot be written to same file.`); + yargs.showHelp(); + process.exit(1); + } + if (argv.i) { + interfaceWriter = new StreamWriter(fs.createWriteStream(argv.i)); + } + if (argv.p) { + proxyWriter = new StreamWriter(fs.createWriteStream(argv.p)); + } + if (argv._.length !== 2) { + console.error(`Please supply an input file with samples in a JSON array, and a symbol to use for the root interface / proxy.`); + yargs.showHelp(); + process.exit(1); + } + + const samples = JSON.parse(fs.readFileSync(argv._[0]).toString()); + const e = new Emitter(interfaceWriter, proxyWriter); + e.emit(samples, argv._[1]); } -const samples = JSON.parse(fs.readFileSync(argv._[0]).toString()); -const e = new Emitter(interfaceWriter, proxyWriter); -e.emit(samples, argv._[1]); +function handlePipedContent() { + let data = ''; + process.stdin.on('readable', function() { + const chuck = process.stdin.read(); + if(chuck !== null){ + data += chuck; + } + }); + process.stdin.on('end', function() { + if (!data) { + console.error('No input.'); + process.exit(1); + } + if (argv.p !== undefined) { + proxyWriter = new StreamWriter(process.stdout); + } else { + interfaceWriter = new StreamWriter(process.stdout); + } + const samples = JSON.parse(data); + const e = new Emitter(interfaceWriter, proxyWriter); + e.emit(samples, 'root') + }); +} diff --git a/lib/stream_writer.ts b/lib/stream_writer.ts index c56dc80..5331763 100644 --- a/lib/stream_writer.ts +++ b/lib/stream_writer.ts @@ -10,7 +10,7 @@ export default class StreamWriter extends Writer { this.stream = stream; } public write(s: string): this { - this.stream.write(new Buffer(s, 'utf8')); + this.stream.write(Buffer.from(s, 'utf8')); return this; } public close(cb: () => void): void { From c5c893182998b3c9d398c2e6301b86cd603529f7 Mon Sep 17 00:00:00 2001 From: IGOR DE PAULA SILVA Date: Mon, 30 Aug 2021 22:30:53 -0300 Subject: [PATCH 2/3] Usage note --- index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 466559d..60d8290 100644 --- a/index.ts +++ b/index.ts @@ -5,7 +5,7 @@ import * as path from 'path'; import { StreamWriter, NopWriter, Emitter } from './lib/index'; -const argv = yargs.usage('Usage: $0 [options] inputFile rootName\nFrom stdin, outputs interface by default.') +const argv = yargs.usage('Usage: $0 [options] inputFile rootName\nOr from stdin, outputs interface by default. Outputs proxy with -p option') .alias('i', 'interface-file') .string('i') .describe('i', 'Specify output file for interfaces') From 94e0346951b26d120190f7955b633b60bfd528f9 Mon Sep 17 00:00:00 2001 From: IGOR DE PAULA SILVA Date: Mon, 30 Aug 2021 22:48:31 -0300 Subject: [PATCH 3/3] Fix argument case --- index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index 60d8290..4bab830 100644 --- a/index.ts +++ b/index.ts @@ -16,6 +16,9 @@ const argv = yargs.usage('Usage: $0 [options] inputFile rootName\nOr from stdin, .alias('h', 'help') .argv; +let interfaceWriter = new NopWriter(); +let proxyWriter = interfaceWriter; + if (process.stdin.isTTY) { handleShellArguments(); } @@ -23,9 +26,6 @@ else { handlePipedContent(); } -let interfaceWriter = new NopWriter(); -let proxyWriter = interfaceWriter; - function handleShellArguments(){ if (argv.i && argv.p && path.resolve(argv.i) === path.resolve(argv.p)) { console.error(`Interfaces and proxies cannot be written to same file.`);