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 (exporters.includes("stat.ink")) {
|
||||||
if (!state.statInkApiKey) {
|
if (!state.statInkApiKey) {
|
||||||
this.env.logger.log("stat.ink API key is not set. Please enter below.");
|
const key = (await this.env.prompts.prompt(
|
||||||
const key = (await this.env.readline()).trim();
|
"stat.ink API key is not set. Please enter below.",
|
||||||
|
)).trim();
|
||||||
if (!key) {
|
if (!key) {
|
||||||
this.env.logger.error("API key is required.");
|
this.env.logger.error("API key is required.");
|
||||||
Deno.exit(1);
|
Deno.exit(1);
|
||||||
|
|
@ -263,20 +264,9 @@ export class App {
|
||||||
}
|
}
|
||||||
async run() {
|
async run() {
|
||||||
await this.profile.readState();
|
await this.profile.readState();
|
||||||
const { logger, readline, newFetcher } = this.env;
|
|
||||||
|
|
||||||
if (!this.profile.state.loginState?.sessionToken) {
|
if (!this.profile.state.loginState?.sessionToken) {
|
||||||
const sessionToken = await loginManually({
|
const sessionToken = await loginManually(this.env);
|
||||||
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();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.profile.writeState({
|
await this.profile.writeState({
|
||||||
...this.profile.state,
|
...this.profile.state,
|
||||||
|
|
|
||||||
27
src/env.ts
27
src/env.ts
|
|
@ -1,4 +1,16 @@
|
||||||
import { CookieJar, CookieOptions, wrapFetch } from "../deps.ts";
|
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 = {
|
export type Fetcher = {
|
||||||
get(opts: { url: string; headers?: HeadersInit }): Promise<Response>;
|
get(opts: { url: string; headers?: HeadersInit }): Promise<Response>;
|
||||||
|
|
@ -15,11 +27,26 @@ export type Logger = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Env = {
|
export type Env = {
|
||||||
|
prompts: Prompts;
|
||||||
logger: Logger;
|
logger: Logger;
|
||||||
newFetcher: (opts?: { cookies?: CookieOptions[] }) => Fetcher;
|
newFetcher: (opts?: { cookies?: CookieOptions[] }) => Fetcher;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DEFAULT_ENV: Env = {
|
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: {
|
logger: {
|
||||||
debug: console.debug,
|
debug: console.debug,
|
||||||
log: console.log,
|
log: console.log,
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,7 @@ import { APIError } from "./APIError.ts";
|
||||||
import { Env, Fetcher } from "./env.ts";
|
import { Env, Fetcher } from "./env.ts";
|
||||||
|
|
||||||
export async function loginManually(
|
export async function loginManually(
|
||||||
{ newFetcher, promptLogin }: {
|
{ newFetcher, prompts: { promptLogin } }: Env,
|
||||||
newFetcher: () => Fetcher;
|
|
||||||
promptLogin: (url: string) => Promise<string>;
|
|
||||||
},
|
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const fetch = newFetcher();
|
const fetch = newFetcher();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue