Skip to content

Commit

Permalink
Merge pull request #53 from vitalygashkov/next
Browse files Browse the repository at this point in the history
Fixed Kinopoisk auth, show browser for Kinopoisk auth in debug mode
  • Loading branch information
vitalygashkov authored Dec 6, 2023
2 parents 2f1f2ea + c35d119 commit 538cd62
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "streamyx",
"version": "3.6.31",
"version": "3.6.32",
"author": "Vitaly Gashkov <[email protected]>",
"description": "Command-line video downloader",
"main": "dist/streamyx.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/wive
Submodule wive updated from be9d5c to 46ee48
2 changes: 1 addition & 1 deletion packages/wivenative
2 changes: 1 addition & 1 deletion src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const launchBrowser = async (options: BrowserLaunchArgumentOptions = {})
page = (await browser?.newPage()) ?? null;
} catch (e) {
logger.error((e as Error).message);
executablePath = await prompt('Enter valid Chrome executable path');
executablePath = await prompt.waitForInput('Enter valid Chrome executable path');
}
}
if (executablePath !== chromePath) saveSettings({ chromePath: executablePath });
Expand Down
2 changes: 1 addition & 1 deletion src/providers
62 changes: 49 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,54 @@ import { createInterface } from 'node:readline/promises';
import { stdin, stdout } from 'node:process';
import { delimiter, join } from 'node:path';
import { stat } from 'node:fs/promises';
import EventEmitter from 'node:events';

const prompt = async <T = string>(message: string, type = 'input') => {
const readline = createInterface({ input: stdin, output: stdout });
const question = { message, type };
const isBooleanQuestion = question.type === 'confirm';
const formattedMessage = question.message + (isBooleanQuestion ? ' (y/n)' : '') + ': ';
const answer = await readline.question(formattedMessage);
const result = isBooleanQuestion ? answer === 'y' : answer.trim();
readline.close();
return result as T;
};
type PromptType = 'input' | 'confirm';
type PromptAnswer<T> = T extends 'input' ? string : T extends 'confirm' ? boolean : never;

class Prompt extends EventEmitter {
constructor() {
super();
}

async waitForInput<T extends PromptType = 'input'>(
message: string,
type?: T
): Promise<PromptAnswer<T>> {
const hasPromptListener = !!this.listeners('prompt').length;
const answer = hasPromptListener
? await this.waitForListenerResponse(message, type)
: await this.waitForCliResponse(message, type);
if (type === 'confirm') {
if (typeof answer === 'string')
return (answer?.toLowerCase() === 'y') as PromptAnswer<boolean>;
else return !!answer as PromptAnswer<boolean>;
} else {
return String(answer).trim() as PromptAnswer<string>;
}
}

private async waitForCliResponse(message: string, type: PromptType = 'input') {
const readline = createInterface({ input: stdin, output: stdout });
const question = { message, type };
const isBooleanQuestion = question.type === 'confirm';
const formattedMessage = question.message + (isBooleanQuestion ? ' (y/n)' : '') + ': ';
const answer = await readline.question(formattedMessage);
readline.close();
return answer;
}

private async waitForListenerResponse(message: string, type: PromptType = 'input') {
return new Promise((resolve) => {
this.emit('prompt', message, type);
this.addListener('prompt:response', (response) => {
resolve(response);
});
});
}
}

export const prompt = new Prompt();

const sleep = async (seconds: number) =>
new Promise((resolve) => setTimeout(resolve, seconds * 1000));
Expand Down Expand Up @@ -94,10 +131,10 @@ const validateUrl = async (url: string) => {
const urlObject = new URL(currentUrl);
isValid = !!urlObject;
} else {
currentUrl = await prompt('URL');
currentUrl = await prompt.waitForInput('URL');
}
} catch (e) {
currentUrl = await prompt('URL');
currentUrl = await prompt.waitForInput('URL');
}
} while (!isValid);
return currentUrl;
Expand All @@ -106,7 +143,6 @@ const validateUrl = async (url: string) => {
const parseMainDomain = (url: string) => new URL(url).host.split('.').at(-2) || null;

export {
prompt,
sleep,
bold,
parseNumberRange,
Expand Down

0 comments on commit 538cd62

Please sign in to comment.