|
|
@ -2,14 +2,24 @@ import chalk from 'chalk'; |
|
|
|
import fetch from 'node-fetch'; |
|
|
|
import * as cfg from './cfg'; |
|
|
|
import { stringify as stringifyQuery } from 'querystring'; |
|
|
|
import _emailRegex from 'email-regex'; |
|
|
|
|
|
|
|
function readEmail () { |
|
|
|
const emailRegex = _emailRegex({ exact: true }); |
|
|
|
|
|
|
|
function readEmail ({ invalid = false } = {}) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
process.stdout.write('> Enter your email address: '); |
|
|
|
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) => { |
|
|
|
process.stdin.pause(); |
|
|
|
resolve(d.toString().trim()); |
|
|
|
}).resume(); |
|
|
|
data.push(d); |
|
|
|
if (d.indexOf('\n') > -1) { |
|
|
|
process.stdin.pause(); |
|
|
|
resolve(Buffer.concat(data).toString().trim()); |
|
|
|
} |
|
|
|
}); |
|
|
|
process.stdin.resume(); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
@ -49,8 +59,10 @@ function sleep (ms) { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
async function register (url) { |
|
|
|
const email = await readEmail(); |
|
|
|
async function register (url, { retryEmail = false } = {}) { |
|
|
|
const email = await readEmail({ invalid: retryEmail }); |
|
|
|
if (!emailRegex.test(email)) return register(url, { retryEmail: true }); |
|
|
|
|
|
|
|
const verificationToken = await getVerificationToken(url, email); |
|
|
|
|
|
|
|
console.log(`> Please follow the link sent to ${chalk.bold(email)} to log in.`); |
|
|
|