Add output to file option
This commit is contained in:
parent
b1c7de7e8c
commit
f315361278
7 changed files with 43 additions and 25 deletions
30
src/cli.ts
30
src/cli.ts
|
@ -2,6 +2,8 @@ import { Command, Option } from "commander";
|
|||
import randomBunny from "./index";
|
||||
import ICliOptions from "./contracts/ICliOptions";
|
||||
import { exit } from "process";
|
||||
import OutputHelper from "./helpers/outputHelper";
|
||||
import { writeFileSync } from "fs";
|
||||
|
||||
const program = new Command();
|
||||
|
||||
|
@ -12,6 +14,7 @@ program
|
|||
.option('-s, --subreddit <subreddit>', 'The subreddit to search', 'rabbits')
|
||||
.option('-j, --json', 'Output as JSON')
|
||||
.option('-q, --query-metadata', 'Include query metadata in result')
|
||||
.option('-o <file>', 'Output to file')
|
||||
.addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top']));
|
||||
|
||||
program.parse();
|
||||
|
@ -21,31 +24,14 @@ const options: ICliOptions = program.opts();
|
|||
randomBunny(options.subreddit, options.sort)
|
||||
.then((response) => {
|
||||
if (response.IsSuccess) {
|
||||
const result = response.Result!;
|
||||
const output = OutputHelper.GenerateOutput(response, options);
|
||||
|
||||
const outputLines: string[] = [];
|
||||
|
||||
if (options.json) {
|
||||
console.log(JSON.stringify(result));
|
||||
return;
|
||||
if (options.o) {
|
||||
writeFileSync(options.o, output);
|
||||
} else {
|
||||
console.log(output);
|
||||
}
|
||||
|
||||
outputLines.push(`Archived = ${result.Archived}`);
|
||||
outputLines.push(`Downvotes = ${result.Downs}`);
|
||||
outputLines.push(`Hidden = ${result.Hidden}`);
|
||||
outputLines.push(`Permalink = ${result.Permalink}`);
|
||||
outputLines.push(`Subreddit = ${result.Subreddit}`);
|
||||
outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`);
|
||||
outputLines.push(`Title = ${result.Title}`);
|
||||
outputLines.push(`Upvotes = ${result.Ups}`);
|
||||
outputLines.push(`Url = ${result.Url}`);
|
||||
|
||||
if (options.queryMetadata != null) {
|
||||
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
|
||||
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
|
||||
}
|
||||
|
||||
console.log(outputLines.join("\n"));
|
||||
exit(0);
|
||||
} else {
|
||||
const error = response.Error!;
|
||||
|
|
|
@ -2,5 +2,6 @@ export default interface ICliOptions {
|
|||
subreddit: string,
|
||||
json?: boolean,
|
||||
sort: "new" | "hot" | "top",
|
||||
o?: string,
|
||||
queryMetadata?: boolean,
|
||||
}
|
31
src/helpers/outputHelper.ts
Normal file
31
src/helpers/outputHelper.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
import ICliOptions from "../contracts/ICliOptions";
|
||||
import IReturnResult from "../contracts/IReturnResult";
|
||||
|
||||
export default class OutputHelper {
|
||||
public static GenerateOutput(response: IReturnResult, options: ICliOptions): string {
|
||||
const result = response.Result!;
|
||||
|
||||
const outputLines: string[] = [];
|
||||
|
||||
if (options.json) {
|
||||
return JSON.stringify(result);
|
||||
}
|
||||
|
||||
outputLines.push(`Archived = ${result.Archived}`);
|
||||
outputLines.push(`Downvotes = ${result.Downs}`);
|
||||
outputLines.push(`Hidden = ${result.Hidden}`);
|
||||
outputLines.push(`Permalink = ${result.Permalink}`);
|
||||
outputLines.push(`Subreddit = ${result.Subreddit}`);
|
||||
outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`);
|
||||
outputLines.push(`Title = ${result.Title}`);
|
||||
outputLines.push(`Upvotes = ${result.Ups}`);
|
||||
outputLines.push(`Url = ${result.Url}`);
|
||||
|
||||
if (options.queryMetadata != null) {
|
||||
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
|
||||
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
|
||||
}
|
||||
|
||||
return outputLines.join("\n");
|
||||
}
|
||||
}
|
|
@ -5,7 +5,7 @@ import { List } from 'linqts';
|
|||
import IFetchResult from "./contracts/IFetchResult";
|
||||
import { ErrorCode } from "./constants/ErrorCode";
|
||||
import ErrorMessages from "./constants/ErrorMessages";
|
||||
import ImageHelper from "./imageHelper";
|
||||
import ImageHelper from "./helpers/imageHelper";
|
||||
|
||||
export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot'): Promise<IReturnResult> {
|
||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import ImageHelper from "../src/imageHelper";
|
||||
import ImageHelper from "../../src/helpers/imageHelper";
|
||||
import fetch from "got-cjs";
|
||||
|
||||
jest.mock('got-cjs');
|
|
@ -1,6 +1,6 @@
|
|||
import { ErrorCode } from "../src/constants/ErrorCode";
|
||||
import ErrorMessages from "../src/constants/ErrorMessages";
|
||||
import ImageHelper from "../src/imageHelper";
|
||||
import ImageHelper from "../src/helpers/imageHelper";
|
||||
import randomBunny from "../src/index";
|
||||
import fetch from "got-cjs";
|
||||
|
||||
|
|
Loading…
Reference in a new issue