refactor: add prompts to Env
parent
94e6ed33ba
commit
ecdf0dd4da
18
src/app.ts
18
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,
|
||||
|
|
|
|||
27
src/env.ts
27
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<string>;
|
||||
/**
|
||||
* Prompt the user to enter the string.
|
||||
*/
|
||||
prompt: (tips: string) => Promise<string>;
|
||||
};
|
||||
|
||||
export type Fetcher = {
|
||||
get(opts: { url: string; headers?: HeadersInit }): Promise<Response>;
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<string>;
|
||||
},
|
||||
{ newFetcher, prompts: { promptLogin } }: Env,
|
||||
): Promise<string> {
|
||||
const fetch = newFetcher();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue