Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: warn, exit and error utils from oclif command class #3028

Closed
Closed
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
4 changes: 2 additions & 2 deletions src/commands/addons/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { prepareAddonCommand, ADDON_VALIDATION } = require('../../utils/addons/prepare')
const Command = require('../../utils/command')
const { log } = require('../../utils/command-helpers')
const { log, exit } = require('../../utils/command-helpers')
const openBrowser = require('../../utils/open-browser')

class AddonsAuthCommand extends Command {
Expand All @@ -25,7 +25,7 @@ class AddonsAuthCommand extends Command {
log(addon.auth_url)
log()
await openBrowser({ url: addon.auth_url })
this.exit()
exit()
}
}
AddonsAuthCommand.aliases = ['addon:auth']
Expand Down
6 changes: 2 additions & 4 deletions src/commands/addons/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const generatePrompts = require('../../utils/addons/prompts')
const render = require('../../utils/addons/render')
const { requiredConfigValues, missingConfigValues, updateConfigValues } = require('../../utils/addons/validation')
const Command = require('../../utils/command')
const { log } = require('../../utils/command-helpers')
const { log, error } = require('../../utils/command-helpers')
const { parseRawFlags } = require('../../utils/parse-raw-flags')

class AddonsConfigCommand extends Command {
Expand Down Expand Up @@ -61,7 +61,6 @@ class AddonsConfigCommand extends Command {
siteId,
instanceId: addon.id,
api,
error: this.error,
})
return false
}
Expand Down Expand Up @@ -132,13 +131,12 @@ class AddonsConfigCommand extends Command {
siteId,
instanceId: addon.id,
api,
error: this.error,
})
}
}
}

const update = async function ({ addonName, currentConfig, newConfig, siteId, instanceId, api, error }) {
const update = async function ({ addonName, currentConfig, newConfig, siteId, instanceId, api }) {
const codeDiff = diffValues(currentConfig, newConfig)
if (!codeDiff) {
log('No changes, exiting early')
Expand Down
10 changes: 5 additions & 5 deletions src/commands/addons/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const generatePrompts = require('../../utils/addons/prompts')
const render = require('../../utils/addons/render')
const { requiredConfigValues, missingConfigValues, updateConfigValues } = require('../../utils/addons/validation')
const Command = require('../../utils/command')
const { log } = require('../../utils/command-helpers')
const { log, error } = require('../../utils/command-helpers')
const { parseRawFlags } = require('../../utils/parse-raw-flags')

const createAddon = async ({ api, siteId, addonName, config, siteData, error }) => {
const createAddon = async ({ api, siteId, addonName, config, siteData }) => {
try {
const response = await api.createServiceInstance({
siteId,
Expand Down Expand Up @@ -38,7 +38,7 @@ class AddonsCreateCommand extends Command {
validation: ADDON_VALIDATION.NOT_EXISTS,
})

const { error, netlify } = this
const { netlify } = this
const { api, site } = netlify
const siteId = site.id

Expand Down Expand Up @@ -71,7 +71,7 @@ class AddonsCreateCommand extends Command {
return false
}

await createAddon({ api, siteId, addonName, config: newConfig, siteData, error })
await createAddon({ api, siteId, addonName, config: newConfig, siteData })

return false
}
Expand Down Expand Up @@ -104,7 +104,7 @@ class AddonsCreateCommand extends Command {
}
}

await createAddon({ api, siteId, addonName, config: configValues, siteData, error })
await createAddon({ api, siteId, addonName, config: configValues, siteData })
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/commands/addons/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const inquirer = require('inquirer')

const { prepareAddonCommand, ADDON_VALIDATION } = require('../../utils/addons/prepare')
const Command = require('../../utils/command')
const { log } = require('../../utils/command-helpers')
const { log, exit, error } = require('../../utils/command-helpers')
const { parseRawFlags } = require('../../utils/parse-raw-flags')

class AddonsDeleteCommand extends Command {
Expand All @@ -26,7 +26,7 @@ class AddonsDeleteCommand extends Command {
default: false,
})
if (!wantsToDelete) {
this.exit()
exit()
}
}

Expand All @@ -37,8 +37,8 @@ class AddonsDeleteCommand extends Command {
instanceId: addon.id,
})
log(`Addon "${addonName}" deleted`)
} catch (error) {
this.error(error.message)
} catch (error_) {
error(error_.message)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/commands/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { methods } = require('netlify')

const { isEmptyCommand } = require('../utils/check-command-inputs')
const Command = require('../utils/command')
const { log, logJson } = require('../utils/command-helpers')
const { log, logJson, error, exit } = require('../utils/command-helpers')

class APICommand extends Command {
async run() {
Expand All @@ -25,15 +25,15 @@ class APICommand extends Command {
log()
log('Above is a list of available API methods')
log(`To run a method use "${chalk.cyanBright('netlify api methodName')}"`)
this.exit()
exit()
}

if (!apiMethod) {
this.error(`You must provide an API method. Run "netlify api --list" to see available methods`)
error(`You must provide an API method. Run "netlify api --list" to see available methods`)
}

if (!api[apiMethod] || typeof api[apiMethod] !== 'function') {
this.error(`"${apiMethod}"" is not a valid api method. Run "netlify api --list" to see available methods`)
error(`"${apiMethod}"" is not a valid api method. Run "netlify api --list" to see available methods`)
}

let payload
Expand All @@ -45,8 +45,8 @@ class APICommand extends Command {
try {
const apiResponse = await api[apiMethod](payload)
logJson(apiResponse)
} catch (error) {
this.error(error)
} catch (error_) {
error(error_)
}
}
}
Expand Down
22 changes: 10 additions & 12 deletions src/commands/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { flags: flagsLib } = require('@oclif/command')

const { getBuildOptions, runBuild } = require('../../lib/build')
const Command = require('../../utils/command')
const { getToken } = require('../../utils/command-helpers')
const { error, exit, getToken } = require('../../utils/command-helpers')

class BuildCommand extends Command {
// Run Netlify Build
Expand All @@ -19,23 +19,21 @@ class BuildCommand extends Command {
token,
flags,
})

if (!flags.offline) {
this.checkOptions(options)
checkOptions(options)
}

const { exitCode } = await runBuild(options)
this.exit(exitCode)
exit(exitCode)
}
}

checkOptions({ cachedConfig: { siteInfo = {} }, token }) {
if (!siteInfo.id) {
this.error('Could not find the site ID. Please run netlify link.', { exit: 1 })
}
const checkOptions = ({ cachedConfig: { siteInfo = {} }, token }) => {
if (!siteInfo.id) {
error('Could not find the site ID. Please run netlify link.', { exit: 1 })
}

if (!token) {
this.error('Could not find the access token. Please run netlify login.', { exit: 1 })
}
if (!token) {
error('Could not find the access token. Please run netlify login.', { exit: 1 })
}
}

Expand Down
Loading