fix: hide user input when logging in

This commit is contained in:
Anatolii Sieryi
2021-02-25 12:02:58 +02:00
parent 6dd616cd04
commit fc2e6deeaa
3 changed files with 24 additions and 3 deletions

View File

@@ -35,7 +35,8 @@ export async function handlePush (argv: {
const isAuthorized = await client.isAuthorizedWithRedocly();
if (!isAuthorized) {
const clientToken = await promptUser(
green(`\n 🔑 Copy your API key from ${blue('https://app.redoc.ly/profile')} and paste it below`)
green(`\n 🔑 Copy your API key from ${blue('https://app.redoc.ly/profile')} and paste it below`),
true
);
await client.login(clientToken);
}

View File

@@ -185,6 +185,7 @@ yargs
`https://app.${process.env.REDOCLY_DOMAIN || 'redoc.ly'}/profile`,
)} and paste it below`,
),
true
);
const client = new RedoclyClient();
client.login(clientToken);

View File

@@ -6,6 +6,7 @@ import * as yaml from 'js-yaml';
import * as fs from 'fs';
import * as path from 'path';
import * as readline from 'readline';
import { Writable } from 'stream';
import {
BundleOutputFormat,
Config,
@@ -119,17 +120,35 @@ export function saveBundle(filename: string, output: string) {
fs.writeFileSync(filename, output);
}
export async function promptUser(query: string): Promise<string> {
export async function promptUser(query: string, hideUserInput = false): Promise<string> {
return new Promise((resolve) => {
let output: Writable = process.stdout;
let isOutputMuted = false;
if (hideUserInput) {
output = new Writable({
write: (chunk, encoding, callback) => {
if (!isOutputMuted) {
process.stdout.write(chunk, encoding);
}
callback();
},
});
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
output,
terminal: true,
historySize: hideUserInput ? 0 : 30,
});
rl.question(`${query}:\n\n `, (answer) => {
rl.close();
resolve(answer);
});
isOutputMuted = hideUserInput;
});
}