Skip to content

Commit

Permalink
write to file if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Jul 8, 2024
1 parent bcd9535 commit 0ad8004
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/commands/unipept/unipept_subcommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command, Option } from "commander";
import { createReadStream, readFileSync } from "fs";
import { createReadStream, createWriteStream, readFileSync } from "fs";
import { createInterface } from "node:readline";
import { Interface } from "readline";
import { Formatter } from "../../formatters/formatter.js";
Expand All @@ -14,10 +14,11 @@ export abstract class UnipeptSubcommand {
user_agent: string;
host = "https://api.unipept.ugent.be";
url?: string;
selectedFields?: RegExp[];
fasta: boolean;
formatter?: Formatter;
outputStream: NodeJS.WritableStream = process.stdout;
firstBatch = true;
selectedFields?: RegExp[];
fasta: boolean;

constructor(name: string) {
this.name = name;
Expand Down Expand Up @@ -53,6 +54,9 @@ export abstract class UnipeptSubcommand {
this.host = this.getHost();
this.url = `${this.host}/api/v2/${this.name}.json`;
this.formatter = FormatterFactory.getFormatter(this.options.format);
if (this.options.output) {
this.outputStream = createWriteStream(this.options.output);
}

let slice = [];

Expand Down Expand Up @@ -81,10 +85,10 @@ export abstract class UnipeptSubcommand {

if (this.firstBatch) {
this.firstBatch = false;
process.stdout.write(this.formatter.header(result, this.fasta));
this.outputStream.write(this.formatter.header(result, this.fasta));
}

process.stdout.write(this.formatter.format(result, this.fasta));
this.outputStream.write(this.formatter.format(result, this.fasta));
}

private constructRequestBody(slice: string[]): URLSearchParams {
Expand Down

0 comments on commit 0ad8004

Please sign in to comment.