Skip to content

refactor(deploy): improve types to avoid extraneous runtime check #7220

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 8 additions & 15 deletions src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,21 +811,21 @@ export const deploy = async (options: DeployOptionValues, command: BaseCommand)

await command.authenticate(options.auth)

let siteId = site.id || options.site

let initialSiteData: SiteInfo | undefined
let newSiteData!: SiteInfo
if (siteId && !isEmpty(siteInfo)) {

const hasSiteData = (site.id || options.site) && !isEmpty(siteInfo)

if (hasSiteData) {
initialSiteData = siteInfo
siteId = initialSiteData.id
} else {
log("This folder isn't linked to a site yet")
const NEW_SITE = '+ Create & configure a new site'
const EXISTING_SITE = 'Link this directory to an existing site'

const initializeOpts = [EXISTING_SITE, NEW_SITE]
const initializeOpts = [EXISTING_SITE, NEW_SITE] as const

const { initChoice } = await inquirer.prompt([
const { initChoice } = await inquirer.prompt<{ initChoice: typeof initializeOpts[number] }>([
{
type: 'list',
name: 'initChoice',
Expand All @@ -837,22 +837,15 @@ export const deploy = async (options: DeployOptionValues, command: BaseCommand)
if (initChoice === NEW_SITE) {
newSiteData = await sitesCreate({}, command)
site.id = newSiteData.id
siteId = site.id
} else if (initChoice === EXISTING_SITE) {
newSiteData = await link({}, command)
site.id = newSiteData?.id
siteId = site.id
site.id = newSiteData.id
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would refactor this to a switch, which will trigger exhaustive switch rules (@typescript-eslint/switch-exhaustiveness-check and I swear there's a built-in tsconfig option that I can't remember now...?) if we ever regress in the future.

}

if (!siteId) {
return logAndThrowError(
"Unable to determine which site to deploy to. Make sure you've run 'netlify link' or that you're specifying your desired site using the '--site' option.",
)
}

// This is the best I could come up with to make TS happy with the complexities above.
const siteData = initialSiteData ?? newSiteData
const siteId = siteData.id

if (options.trigger) {
return triggerDeploy({ api, options, siteData, siteId })
Expand Down
Loading