Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 52 additions & 18 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\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')
Expand All @@ -18,23 +18,57 @@ const argv = yargs.usage('Usage: $0 [options] inputFile rootName')

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);

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')
});
}
2 changes: 1 addition & 1 deletion lib/stream_writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down