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: replace inherited log, exit utils in commands.(build|env|lm|link|unlink) #3367

Merged
merged 4 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 13 additions & 13 deletions src/commands/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ 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')

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

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

class BuildCommand extends Command {
// Run Netlify Build
Expand All @@ -21,21 +31,11 @@ class BuildCommand extends Command {
})

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

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

checkOptions({ cachedConfig: { siteInfo = {} }, token }) {
if (!siteInfo.id) {
this.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 })
}
exit(exitCode)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/env/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const dotenv = require('dotenv')
const isEmpty = require('lodash/isEmpty')

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

class EnvImportCommand extends Command {
async run() {
Expand Down Expand Up @@ -34,7 +34,7 @@ class EnvImportCommand extends Command {
importedEnv = dotenv.parse(envFileContents)
} catch (error) {
log(error.message)
this.exit(1)
exit(1)
}

if (isEmpty(importedEnv)) {
Expand Down
30 changes: 15 additions & 15 deletions src/commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const chalk = require('chalk')

const { listSites } = require('../lib/api')
const Command = require('../utils/command')
const { log } = require('../utils/command-helpers')
const { log, error, exit } = require('../utils/command-helpers')
const ensureNetlifyIgnore = require('../utils/gitignore')
const linkPrompt = require('../utils/link/link-by-prompt')
const { track } = require('../utils/telemetry')
Expand All @@ -26,7 +26,7 @@ class LinkCommand extends Command {
let siteData
try {
siteData = await api.getSite({ siteId })
} catch (error) {
} catch {
// silent api error
}

Expand All @@ -37,7 +37,7 @@ class LinkCommand extends Command {
if (siteId && !siteData) {
console.log(`"${siteId}" was not found in your Netlify account.`)
console.log(`Please double check your siteID and which account you are logged into via \`netlify status\`.`)
return this.exit()
return exit()
}

// If already linked to site. exit and prompt for unlink
Expand All @@ -46,17 +46,17 @@ class LinkCommand extends Command {
log(`Admin url: ${siteData.admin_url}`)
log()
log(`To unlink this site, run: ${chalk.cyanBright('netlify unlink')}`)
return this.exit()
return exit()
}

if (flags.id) {
try {
siteData = await api.getSite({ site_id: flags.id })
} catch (error) {
if (error.status === 404) {
this.error(new Error(`Site id ${flags.id} not found`))
} catch (error_) {
if (error_.status === 404) {
error(new Error(`Site id ${flags.id} not found`))
} else {
this.error(error)
error(error_)
}
}

Expand All @@ -70,7 +70,7 @@ class LinkCommand extends Command {
kind: 'byId',
})

return this.exit()
return exit()
}

if (flags.name) {
Expand All @@ -83,16 +83,16 @@ class LinkCommand extends Command {
filter: 'all',
},
})
} catch (error) {
if (error.status === 404) {
this.error(new Error(`${flags.name} not found`))
} catch (error_) {
if (error_.status === 404) {
error(new Error(`${flags.name} not found`))
} else {
this.error(error)
error(error_)
}
}

if (results.length === 0) {
this.error(new Error(`No sites found named ${flags.name}`))
error(new Error(`No sites found named ${flags.name}`))
}
const [firstSiteData] = results
state.set('siteId', firstSiteData.id)
Expand All @@ -105,7 +105,7 @@ class LinkCommand extends Command {
kind: 'byName',
})

return this.exit()
return exit()
}

siteData = await linkPrompt(this, flags)
Expand Down
9 changes: 5 additions & 4 deletions src/commands/lm/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const execa = require('execa')
const Listr = require('listr')

const Command = require('../../utils/command')
const { error } = require('../../utils/command-helpers')
const { installPlatform } = require('../../utils/lm/install')
const { checkHelperVersion } = require('../../utils/lm/requirements')
const { printBanner } = require('../../utils/lm/ui')
Expand All @@ -14,7 +15,7 @@ const installHelperIfMissing = async function ({ force }) {
if (!version) {
installHelper = true
}
} catch (error) {
} catch {
installHelper = true
}

Expand All @@ -37,9 +38,9 @@ const provisionService = async function (siteId, api) {
addon: addonName,
body: {},
})
} catch (error) {
} catch (error_) {
// error is JSONHTTPError
throw new Error(error.json.error)
throw new Error(error_.json.error)
}
}

Expand All @@ -62,7 +63,7 @@ class LmSetupCommand extends Command {
try {
helperInstalled = await installHelperIfMissing({ force: flags['force-install'] })
} catch (error_) {
this.error(error_)
error(error_)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/unlink.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Command = require('../utils/command')
const { log } = require('../utils/command-helpers')
const { log, exit } = require('../utils/command-helpers')
const { track } = require('../utils/telemetry')

class UnlinkCommand extends Command {
Expand All @@ -9,7 +9,7 @@ class UnlinkCommand extends Command {

if (!siteId) {
log(`Folder is not linked to a Netlify site. Run 'netlify link' to link it`)
return this.exit()
return exit()
}

let siteData = {}
Expand Down