From ecdf0dd4da36875e3bae86ee308c212f608d93f8 Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Fri, 18 Nov 2022 20:27:23 +0800 Subject: [PATCH] refactor: add prompts to Env --- src/app.ts | 18 ++++-------------- src/env.ts | 27 +++++++++++++++++++++++++++ src/iksm.ts | 5 +---- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/app.ts b/src/app.ts index 6024c9c..b36049b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -65,8 +65,9 @@ export class App { if (exporters.includes("stat.ink")) { if (!state.statInkApiKey) { - this.env.logger.log("stat.ink API key is not set. Please enter below."); - const key = (await this.env.readline()).trim(); + const key = (await this.env.prompts.prompt( + "stat.ink API key is not set. Please enter below.", + )).trim(); if (!key) { this.env.logger.error("API key is required."); Deno.exit(1); @@ -263,20 +264,9 @@ export class App { } async run() { await this.profile.readState(); - const { logger, readline, newFetcher } = this.env; if (!this.profile.state.loginState?.sessionToken) { - const sessionToken = await loginManually({ - newFetcher, - promptLogin: async (url: string) => { - logger.log("Navigate to this URL in your browser:"); - logger.log(url); - logger.log( - 'Log in, right click the "Select this account" button, copy the link address, and paste it below:', - ); - return await readline(); - }, - }); + const sessionToken = await loginManually(this.env); await this.profile.writeState({ ...this.profile.state, diff --git a/src/env.ts b/src/env.ts index e9eb914..db7f25c 100644 --- a/src/env.ts +++ b/src/env.ts @@ -1,4 +1,16 @@ import { CookieJar, CookieOptions, wrapFetch } from "../deps.ts"; +import { readline } from "./utils.ts"; + +export type Prompts = { + /** + * Prompt the user to enter the npf url. + */ + promptLogin: (url: string) => Promise; + /** + * Prompt the user to enter the string. + */ + prompt: (tips: string) => Promise; +}; export type Fetcher = { get(opts: { url: string; headers?: HeadersInit }): Promise; @@ -15,11 +27,26 @@ export type Logger = { }; export type Env = { + prompts: Prompts; logger: Logger; newFetcher: (opts?: { cookies?: CookieOptions[] }) => Fetcher; }; export const DEFAULT_ENV: Env = { + prompts: { + promptLogin: async (url: string) => { + console.log("Navigate to this URL in your browser:"); + console.log(url); + console.log( + 'Log in, right click the "Select this account" button, copy the link address, and paste it below:', + ); + return await readline(); + }, + prompt: async (tips: string) => { + console.log(tips); + return await readline(); + }, + }, logger: { debug: console.debug, log: console.log, diff --git a/src/iksm.ts b/src/iksm.ts index f2cf282..27d2793 100644 --- a/src/iksm.ts +++ b/src/iksm.ts @@ -9,10 +9,7 @@ import { APIError } from "./APIError.ts"; import { Env, Fetcher } from "./env.ts"; export async function loginManually( - { newFetcher, promptLogin }: { - newFetcher: () => Fetcher; - promptLogin: (url: string) => Promise; - }, + { newFetcher, prompts: { promptLogin } }: Env, ): Promise { const fetch = newFetcher();