diff --git a/lib/login.js b/lib/login.js index 9a79d9b..d48f240 100644 --- a/lib/login.js +++ b/lib/login.js @@ -3,23 +3,7 @@ import fetch from 'node-fetch'; import * as cfg from './cfg'; import { stringify as stringifyQuery } from 'querystring'; import { validate } from 'email-validator'; - -function readEmail ({ invalid = false } = {}) { - return new Promise((resolve, reject) => { - const decorate = invalid ? chalk.red : (v) => v; - const prompt = decorate('> Enter your email address: '); - process.stdout.write(prompt); - const data = []; - process.stdin.on('data', (d) => { - data.push(d); - if (d.indexOf('\n') > -1) { - process.stdin.pause(); - resolve(Buffer.concat(data).toString().trim()); - } - }); - process.stdin.resume(); - }); -} +import readEmail from 'email-prompt'; async function getVerificationToken (url, email) { const data = JSON.stringify({ email }); @@ -59,6 +43,8 @@ function sleep (ms) { async function register (url, { retryEmail = false } = {}) { const email = await readEmail({ invalid: retryEmail }); + process.stdout.write('\n'); + if (!validate(email)) return register(url, { retryEmail: true }); const verificationToken = await getVerificationToken(url, email); diff --git a/package.json b/package.json index 2690e45..3530e88 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "split-array": "1.0.1", "text-table": "0.2.0", "spdy": "3.3.3", - "email-validator": "1.0.4" + "email-validator": "1.0.4", + "email-prompt": "0.1.3" }, "devDependencies": { "alpha-sort": "1.0.2",