Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

#33 migrate to inquirer #46

Merged
merged 5 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 72 additions & 102 deletions modules/auth.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const apiRequest = require('./req')
const log = require('./log')
const prompt = require('prompt')
const promptUtil = require('./promptUtil')
const inquirer = require('inquirer')

// TODO: use a faster route to verify the token
function tokenValid (token) {
return apiRequest.get('instances/', {
token
},
{ skipResponseProcessing: true }
)
}, {
skipResponseProcessing: true
})
}

function authenticate (email, password) {
Expand All @@ -21,95 +20,77 @@ function authenticate (email, password) {
})
}

function signupApi (email, password, password_confirmation, newsletter) { // eslint-disable-line
function signupApi (email, password, passwordConfirmation, newsletter) {
return apiRequest.post('account/register', {
account: {
email,
password,
password_confirmation,
password_confirmation: passwordConfirmation,
newsletter
}
},
{ token: '' }
)
}, {
token: ''
})
}

function login () {
async function login () {
const schema = [{
type: 'input',
message: 'email:',
name: 'email'
},
{
type: 'password',
message: 'Password:',
name: 'password'
}
]
const result = await inquirer.prompt(schema)
return new Promise((resolve, reject) => {
const schema = {
properties: {
email: {
required: true
},
password: {
hidden: true
}
}
}

prompt.start()

prompt.get(schema, function (err, result) {
if (err) {
reject(err)
} else {
authenticate(result.email, result.password).then((token) => {
resolve(token)
}).catch(err => {
reject(err)
})
}
authenticate(result.email, result.password).then((token) => {
resolve(token)
}).catch(err => {
reject(err)
})
})
}

function wantsNewsletter () {
return new Promise((resolve, reject) => {
const schema = {
properties: {
wantsNewsletter: {
description: 'Subscribe to the newsletter ([y]es or [n]o) ?',
pattern: /^[y,n]$/,
message: 'Invalid input, please enter either Y or N.',
required: true,
default: 'y'
}
}
}

prompt.start()

prompt.get(schema, function (err, result) {
if (err || !result) {
return reject(err)
} else {
resolve(result.wantsNewsletter)
}
})
})
async function wantsNewsletter () {
const schema = [{
type: 'input',
message: 'Subscribe to the newsletter ([y]es or [n]o) ?',
name: 'wantsNewsletter',
choices: ['y', 'n'],
default: 'y'
}]
const result = await inquirer.prompt(schema)
return result.wantsNewsletter === 'y' ? 1 : 0
}

async function signup () {
const schema = {
properties: {
email: {
required: true
},
password: {
hidden: true
},
password_confirmation: {
hidden: true
}
}
const schema = [{
type: 'input',
message: 'email:',
name: 'email'
},
{
type: 'password',
message: 'Password:',
name: 'password'
},
{
type: 'password',
message: 'Repeat Password:',
name: 'password_confirmation'
}
]

let user = null

while (user === null) {
try {
const result = await promptUtil.promisifyPrompt(schema)
const newsletter = (await wantsNewsletter()) === 'y'
const result = await inquirer.prompt(schema)
const newsletter = await wantsNewsletter()
user = await signupApi(result.email, result.password, result.password_confirmation, newsletter)

if (user) {
Expand All @@ -118,7 +99,6 @@ async function signup () {
} catch (err) {
log.err(err)
user = null

if (!err.error) {
return null
}
Expand All @@ -128,45 +108,35 @@ async function signup () {
return user.token
}

function loginOrSignup () {
return new Promise((resolve, reject) => {
const schema = {
properties: {
loginOrSignup: {
description: 'Would you like to [l]ogin or [r]egister a new account?',
pattern: /^[l,r]$/,
message: 'Invalid input, please enter either l to [l]ogin or r to [r]egister.',
required: true,
default: 'r'
}
}
}

prompt.start()

prompt.get(schema, function (err, result) {
if (err || !result) {
return reject(err)
}

if (result.loginOrSignup === 'l') {
async function loginOrSignup () {
const schema = [{
type: 'input',
message: 'Would you like to [l]ogin or [r]egister a new account?',
name: 'loginOrSignup',
choices: ['l', 'r'],
default: 'r'
}]
const result = await inquirer.prompt(schema)
return await new Promise((resolve, reject) => {
switch (result.loginOrSignup) {
case 'l':
login().then((token) => {
resolve(token)
}).catch((err) => {
console.log(err)
reject(err)
})
} else if (result.loginOrSignup === 'r') {
break
case 'r':
signup().then((token) => {
resolve(token)
}).catch((err) => {
console.log(err)
reject(err)
})
} else {

}
})
break
default:
resolve({ error: 'invalid input: [r]egister or [l]ogin only' })
break
}
})
}

Expand Down
69 changes: 24 additions & 45 deletions modules/instance.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const apiRequest = require('./req')
const log = require('./log')
const promptUtil = require('./promptUtil')
const inquirer = require('inquirer')
const instanceRequest = require('./instanceRequest')
const instanceOp = require('./instance_operation')
const modLocations = require('./locations')
Expand Down Expand Up @@ -49,18 +49,6 @@ async function selectExistingOrCreate (env) {
defaultSitename = sites[0]
}

const schema = {
properties: {
sitename: {
description: 'Type your subdomain sitename (Example: my-site)' +
instanceType === 'server' ? ' OR custom domain (mysite.com)' : '',
message: 'Invalid input, please enter a sitename or custom domain.',
required: true,
default: defaultSitename
}
}
}

if (sites.length > 0) {
console.log('Existing sites:')

Expand All @@ -75,7 +63,14 @@ async function selectExistingOrCreate (env) {
}
}

const result = await promptUtil.promisifyPrompt(schema)
const schema = [{
type: 'input',
message: 'Type your subdomain sitename (Example: my-site)' + instanceType === 'server' ? ' OR custom domain (mysite.com)' : '',
name: 'sitename',
default: defaultSitename
}]

const result = await inquirer.prompt(schema)

const siteExists = await getWebsite(result.sitename, env)

Expand All @@ -84,7 +79,6 @@ async function selectExistingOrCreate (env) {
return result.sitename
} else {
log.out('creating website...')
// try to create it!
const siteCreated = await createInstance({
sitename: result.sitename,
instanceType
Expand All @@ -107,24 +101,19 @@ async function selectLocation (env, allLocations) {
while (!selectedLocation) {
const defaultLocation = allLocations[0].id

const schema = {
properties: {
location: {
description: 'Select a location (type the id)',
message: 'Invalid input, please enter a valid location.',
required: true,
default: defaultLocation
}
}
}
const schema = [{
type: 'input',
message: 'Select a location (type the id)',
name: 'location',
default: defaultLocation
}]

if (allLocations.length > 0) {
console.log('-----------------------------------------')
console.log(allLocations)
console.log('Select a location')
}

const result = await promptUtil.promisifyPrompt(schema)
const result = await inquirer.prompt(schema)

const locationValid = allLocations.find(l => [l.id, l.str_id].includes(result.location))

Expand All @@ -133,7 +122,6 @@ async function selectLocation (env, allLocations) {
} else {
log.out('adding location...')

// try to create it!
const resultAddLocation =
await instanceOp('addLocation', env, {
location_str_id: result.location
Expand All @@ -143,9 +131,7 @@ async function selectLocation (env, allLocations) {
log.out(resultAddLocation)
}

selectedLocation = resultAddLocation.result === 'success'
? result.location
: null
selectedLocation = resultAddLocation.result === 'success' ? result.location : null
}
}

Expand All @@ -158,33 +144,26 @@ async function selectPlan (env, locationId, allPlans) {
while (selectedPlan == null) {
const defaultPlan = allPlans[0].id

const schema = {
properties: {
plan: {
description: 'Select a plan (type the id)',
message: 'Invalid input, please enter a valid plan.',
required: true,
default: defaultPlan
}
}
}
const schema = [{
type: 'input',
message: 'Select a plan (type the id)',
name: 'plan',
default: defaultPlan
}]

if (allPlans.length > 0) {
console.log('-----------------------------------------')
console.log(allPlans)
console.log('Select a plan')
}

const result = await promptUtil.promisifyPrompt(schema)

const result = await inquirer.prompt(schema)
const planValid = allPlans.find(l => l.id === result.plan)

if (!planValid) {
log.out('Invalid plan')
} else {
log.out('setting plan...')

// try to create it!
await instanceOp('set-plan', env, {
location_str_id: locationId,
plan: result.plan
Expand Down
18 changes: 0 additions & 18 deletions modules/promptUtil.js

This file was deleted.

Loading