Skip to content

Commit

Permalink
Merge pull request #45 from netlify/inquirer-prompt-link
Browse files Browse the repository at this point in the history
Add inquirer prompts
  • Loading branch information
bcomnes authored Jul 31, 2018
2 parents b9e3ff2 + b2c7bd5 commit 05c97db
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $ npm install -g netlify-cli
$ netlify-cli COMMAND
running command...
$ netlify-cli (-v|--version|version)
netlify-cli/0.0.0 darwin-x64 node-v10.6.0
netlify-cli/0.0.0 darwin-x64 node-v10.7.0
$ netlify-cli --help [COMMAND]
USAGE
$ netlify-cli COMMAND
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"flush-write-stream": "^1.0.3",
"folder-walker": "^3.1.0",
"hasha": "^3.0.0",
"inquirer": "^6.0.0",
"is-docker": "^1.1.0",
"listr": "^0.14.1",
"lodash.camelcase": "^4.3.0",
"lodash.flatten": "^4.4.0",
"lodash.get": "^4.4.2",
Expand Down
91 changes: 82 additions & 9 deletions src/commands/link/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Command = require('../../base')
const { flags } = require('@oclif/command')
const renderShortDesc = require('../../utils/renderShortDescription')
const { CLIError } = require('@oclif/errors')
const inquirer = require('inquirer')

class LinkCommand extends Command {
async run() {
Expand All @@ -10,7 +10,7 @@ class LinkCommand extends Command {
const siteId = this.site.get('siteId')

if (siteId && !flags.force) {
const site = await this.netlify.getSite(siteId)
const site = await this.netlify.getSite({ siteId })
this.log(`Site already linked to ${site.name}`)
this.log(`Link: ${site.admin_url}`)
return this.exit()
Expand All @@ -19,10 +19,10 @@ class LinkCommand extends Command {
if (flags.id) {
let site
try {
site = await this.netlify.getSite(flags.id)
site = await this.netlify.getSite({ site_id: flags.id })
} catch (e) {
if (e.status === 404) throw new CLIError(`Site id ${flags.id} not found`)
else throw new CLIError(e)
if (e.status === 404) this.error(new Error(`Site id ${flags.id} not found`))
else this.error(e)
}
this.site.set('siteId', site.id)
this.log(`Linked to ${site.name} in ${this.site.path}`)
Expand All @@ -37,20 +37,93 @@ class LinkCommand extends Command {
filter: 'all'
})
} catch (e) {
if (e.status === 404) throw new CLIError(`Site id ${flags.id} not found`)
else throw new CLIError(e)
if (e.status === 404) this.error(new Error(`${flags.name} not found`))
else this.error(e)
}

if (results.length === 0) {
throw new CLIError(`No sites found named ${flags.name}`)
this.error(new Error(`No sites found named ${flags.name}`))
}
const site = results[0]
this.site.set('siteId', site.id)
this.log(`Linked to ${site.name} in ${this.site.path}`)
return this.exit()
}

this.log(`TODO: Add inquisitor interactive mode here. Use flags instead`)
const { linkType } = await inquirer.prompt([
{
type: 'list',
name: 'linkType',
message: 'How do you want to link this folder to a site?',
choices: ['Site Name', 'Site ID']
}
])

switch (linkType) {
case 'Site Name': {
const { siteName } = await inquirer.prompt([
{
type: 'input',
name: 'siteName',
message: 'What is the name of the site?'
}
])
let sites
try {
sites = await this.netlify.listSites({
name: siteName,
filter: 'all'
})
} catch (e) {
if (e.status === 404) this.error(new Error(`${flags.name} not found`))
else this.error(e)
}

if (sites.length === 0) {
this.error(new Error(`No sites found named ${flags.name}`))
}
let site
if (sites.length > 1) {
const { siteName } = await inquirer.prompt([
{
type: 'list',
name: 'name',
paginated: true,
choices: sites.map(site => site.name)
}
])
site = sites.find(site => (site.name = siteName))
if (!site) this.error('No site selected')
} else {
site = sites[0]
}
this.site.set('siteId', site.id)
this.log(`Linked to ${site.name} in ${this.site.path}`)
this.exit()
break
}
case 'Site ID': {
const { siteId } = await inquirer.prompt([
{
type: 'input',
name: 'siteId',
message: 'What is the site-id of the site?'
}
])

let site
try {
site = await this.netlify.getSite({ siteId })
} catch (e) {
if (e.status === 404) this.error(new Error(`Site id ${siteId} not found`))
else this.error(e)
}
this.site.set('siteId', site.id)
this.log(`Linked to ${site.name} in ${this.site.path}`)
this.exit()
break
}
}
}
}

Expand Down

0 comments on commit 05c97db

Please sign in to comment.