From 1dbce278d8bc1e918fae1682c646dc061ceafce3 Mon Sep 17 00:00:00 2001 From: Philippe Serhal Date: Thu, 17 Apr 2025 09:18:18 -0400 Subject: [PATCH 1/2] 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. --- src/commands/deploy/deploy.ts | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/commands/deploy/deploy.ts b/src/commands/deploy/deploy.ts index 4b928aa554c..84d89bd4df0 100644 --- a/src/commands/deploy/deploy.ts +++ b/src/commands/deploy/deploy.ts @@ -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', @@ -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 } } - 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 }) From a37e183b7ecd0623442472076f8d1e68aeb97706 Mon Sep 17 00:00:00 2001 From: Philippe Serhal Date: Wed, 23 Apr 2025 14:08:56 -0400 Subject: [PATCH 2/2] refactor: use switch-case in deploy init choice --- src/commands/deploy/deploy.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/commands/deploy/deploy.ts b/src/commands/deploy/deploy.ts index 84d89bd4df0..0a68805cae3 100644 --- a/src/commands/deploy/deploy.ts +++ b/src/commands/deploy/deploy.ts @@ -834,12 +834,15 @@ export const deploy = async (options: DeployOptionValues, command: BaseCommand) }, ]) // create site or search for one - if (initChoice === NEW_SITE) { - newSiteData = await sitesCreate({}, command) - site.id = newSiteData.id - } else if (initChoice === EXISTING_SITE) { - newSiteData = await link({}, command) - site.id = newSiteData.id + switch (initChoice) { + case NEW_SITE: + newSiteData = await sitesCreate({}, command) + site.id = newSiteData.id + break + case EXISTING_SITE: + newSiteData = await link({}, command) + site.id = newSiteData.id + break } }