Skip to content

Commit 4938182

Browse files
authored
refactor(deploy): improve types to avoid extraneous runtime check (#7220)
* refactor: improve types to avoid extraneous runtime check This code is messy, but by improving the typess and avoiding some reassignments we can let TS understand it better and avoid an extraneous runtime check. * refactor: use switch-case in deploy init choice
1 parent 2aa2e44 commit 4938182

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/commands/deploy/deploy.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -811,21 +811,21 @@ export const deploy = async (options: DeployOptionValues, command: BaseCommand)
811811

812812
await command.authenticate(options.auth)
813813

814-
let siteId = site.id || options.site
815-
816814
let initialSiteData: SiteInfo | undefined
817815
let newSiteData!: SiteInfo
818-
if (siteId && !isEmpty(siteInfo)) {
816+
817+
const hasSiteData = (site.id || options.site) && !isEmpty(siteInfo)
818+
819+
if (hasSiteData) {
819820
initialSiteData = siteInfo
820-
siteId = initialSiteData.id
821821
} else {
822822
log("This folder isn't linked to a site yet")
823823
const NEW_SITE = '+ Create & configure a new site'
824824
const EXISTING_SITE = 'Link this directory to an existing site'
825825

826-
const initializeOpts = [EXISTING_SITE, NEW_SITE]
826+
const initializeOpts = [EXISTING_SITE, NEW_SITE] as const
827827

828-
const { initChoice } = await inquirer.prompt([
828+
const { initChoice } = await inquirer.prompt<{ initChoice: typeof initializeOpts[number] }>([
829829
{
830830
type: 'list',
831831
name: 'initChoice',
@@ -834,25 +834,21 @@ export const deploy = async (options: DeployOptionValues, command: BaseCommand)
834834
},
835835
])
836836
// create site or search for one
837-
if (initChoice === NEW_SITE) {
838-
newSiteData = await sitesCreate({}, command)
839-
site.id = newSiteData.id
840-
siteId = site.id
841-
} else if (initChoice === EXISTING_SITE) {
842-
newSiteData = await link({}, command)
843-
site.id = newSiteData?.id
844-
siteId = site.id
837+
switch (initChoice) {
838+
case NEW_SITE:
839+
newSiteData = await sitesCreate({}, command)
840+
site.id = newSiteData.id
841+
break
842+
case EXISTING_SITE:
843+
newSiteData = await link({}, command)
844+
site.id = newSiteData.id
845+
break
845846
}
846847
}
847848

848-
if (!siteId) {
849-
return logAndThrowError(
850-
"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.",
851-
)
852-
}
853-
854849
// This is the best I could come up with to make TS happy with the complexities above.
855850
const siteData = initialSiteData ?? newSiteData
851+
const siteId = siteData.id
856852

857853
if (options.trigger) {
858854
return triggerDeploy({ api, options, siteData, siteId })

0 commit comments

Comments
 (0)