Skip to content

Commit

Permalink
add working site:delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidWells committed Jul 23, 2019
1 parent 2440f7b commit 24ccc8b
Showing 1 changed file with 91 additions and 28 deletions.
119 changes: 91 additions & 28 deletions src/commands/sites/delete.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,112 @@
const { Command, flags } = require('@oclif/command')
// const {parseRawFlags} = require('../../utils/parse-raw-flags')
const inquirer = require('inquirer')
const chalk = require('chalk')
const Command = require('@netlify/cli-utils')
const { flags } = require('@oclif/command')
const { parseRawFlags } = require('../../utils/parse-raw-flags')

class SitesDeleteCommand extends Command {
async run() {
const { args } = this.parse(SitesDeleteCommand)

this.log(`delete a site id:`, args.siteID)
this.log(`Implementation coming soon`)
const { args, flags, raw } = this.parse(SitesDeleteCommand)
const { api, site } = this.netlify
const { siteId } = args
const cwdSiteId = site.id

// 1. Prompt user for verification
await this.authenticate(flags.auth)

let siteData
try {
siteData = await api.getSite({ siteId })
} catch (err) {
if (err.status === 404) {
this.warn(`No site with id ${siteId} found. Please verify the siteId & try again.`)
this.exit()
}
}

if (!siteData) {
this.warn(`Unable to process site`)
this.exit()
}

const { force, f } = parseRawFlags(raw)
const noForce = !force && !f

/* Verify the user wants to delete the site */
if (noForce) {
this.log(`${chalk.redBright('Warning')}: You are about to permanently delete "${chalk.bold(siteData.name)}"`)
this.log(` Verify this siteID "${cwdSiteId}" supplied is correct and proceed.`)
this.log(' To skip this prompt, pass a --force flag to the delete command')
this.log()
this.log(`${chalk.bold('Be careful here. There is no undo!')}`)
this.log()
const { wantsToDelete } = await inquirer.prompt({
type: 'confirm',
name: 'wantsToDelete',
message: `WARNING: Are you sure you want to delete the "${siteData.name}" site?`,
default: false
})
this.log()
if (!wantsToDelete) {
this.exit()
}
}

// const {force, f} = parseRawFlags(raw)
// if (!force || !f) {
// const inquirer = require('inquirer')
// const {wantsToDelete} = await inquirer.prompt({
// type: 'confirm',
// name: 'wantsToDelete',
// message: `Are you sure you want to delete the ${addonName} add-on? (to skip this prompt, pass a --force flag)`,
// default: false
// })
// if (!wantsToDelete) this.exit()
// }

// 2. delete site

// 3. --force flag to skip prompts
/* Validation logic if siteId passed in does not match current site ID */
if (noForce && (cwdSiteId && (cwdSiteId !== siteId))) {
this.log(`${chalk.redBright('Warning')}: The siteId supplied does not match the current working directory siteId`)
this.log()
this.log(`Supplied: "${siteId}"`)
this.log(`Current Site: "${cwdSiteId}"`)
this.log()
this.log(`Verify this siteID "${cwdSiteId}" supplied is correct and proceed.`)
this.log('To skip this prompt, pass a --force flag to the delete command')
const { wantsToDelete } = await inquirer.prompt({
type: 'confirm',
name: 'wantsToDelete',
message: `Verify & Proceed with deletion of site "${siteId}"?`,
default: false
})
if (!wantsToDelete) {
this.exit()
}
}

this.log(`Deleting site "${siteId}"...`)

try {
await api.deleteSite({ site_id: siteId })
} catch (error) {
if (error.status === 404) {
this.warn(`No site with id ${siteId} found. Please verify the siteId & try again.`)
this.exit()
} else {
this.error(`Delete Site error: ${error.status}: ${error.message}`)
}
}
this.log(`Site "${siteId}" successfully deleted!`)
}
}

SitesDeleteCommand.description = `delete a site`

SitesDeleteCommand.args = [
{
name: 'siteID',
name: 'siteId',
required: true,
description: 'Site ID to delete'
description: 'Site ID to delete. `netlify delete 1234-5678-890`'
}
]

SitesDeleteCommand.flags = {
force: flags.boolean({ char: 'f', description: 'delete without prompting (useful for CI)' })
force: flags.boolean({
char: 'f',
description: 'delete without prompting (useful for CI)'
})
}

SitesDeleteCommand.examples = ['netlify site:delete 123-432621211']

// TODO implement logic
SitesDeleteCommand.hidden = true
SitesDeleteCommand.examples = [
'netlify site:delete 123-432621211'
]

module.exports = SitesDeleteCommand

0 comments on commit 24ccc8b

Please sign in to comment.