From d4878e3ed4683b04576b6cbe66103671c51935ce Mon Sep 17 00:00:00 2001 From: Karolina Kopacz Date: Wed, 20 Nov 2024 13:48:36 +0100 Subject: [PATCH] feat: get the shortest vercel production domain (#31) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Martin Chełminiak --- packages/core/installMachine/index.ts | 16 ++++----- .../installSteps/vercel/deploy.ts | 14 +++++--- .../vercel/utils/getShortestVercelAlias.ts | 33 +++++++++++++++++++ 3 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 packages/core/installMachine/installSteps/vercel/utils/getShortestVercelAlias.ts diff --git a/packages/core/installMachine/index.ts b/packages/core/installMachine/index.ts index d06f70b..57ca77e 100644 --- a/packages/core/installMachine/index.ts +++ b/packages/core/installMachine/index.ts @@ -1,22 +1,22 @@ -import { createMachine, fromPromise, ActorLogic, AnyEventObject, PromiseSnapshot, createActor, and, not } from 'xstate'; +import { ActorLogic, AnyEventObject, PromiseSnapshot, and, createActor, createMachine, fromPromise, not } from 'xstate'; +import { InstallMachineContext, StepsCompleted } from '../types'; +import { saveStateToRcFile } from '../utils/rcFileManager'; +import { prepareDrink } from './installSteps/bar/prepareDrink'; +import { createDocFiles } from './installSteps/docs/create'; import { initializeRepository } from './installSteps/github/install'; +import { pushToGitHub } from './installSteps/github/repositoryManager'; +import { modifyHomepage } from './installSteps/homepage/install'; import { preparePayload } from './installSteps/payload/install'; import { prettify } from './installSteps/prettier/prettify'; import { connectSupabaseProject } from './installSteps/supabase/connectProject'; import { createSupabaseProject } from './installSteps/supabase/createProject'; import { installSupabase } from './installSteps/supabase/install'; +import { installTailwind } from './installSteps/tailwind/install'; import { createTurboRepo } from './installSteps/turbo/create'; import { deployVercelProject } from './installSteps/vercel/deploy'; import { linkVercelProject } from './installSteps/vercel/link'; import { updateVercelProjectSettings } from './installSteps/vercel/updateProjectSettings'; -import { prepareDrink } from './installSteps/bar/prepareDrink'; -import { createDocFiles } from './installSteps/docs/create'; -import { pushToGitHub } from './installSteps/github/repositoryManager'; -import { InstallMachineContext, StepsCompleted } from '../types'; -import { saveStateToRcFile } from '../utils/rcFileManager'; -import { installTailwind } from './installSteps/tailwind/install'; -import { modifyHomepage } from './installSteps/homepage/install'; const isStepCompleted = (stepName: keyof StepsCompleted) => { return ({ context }: { context: InstallMachineContext; event: AnyEventObject }) => { diff --git a/packages/core/installMachine/installSteps/vercel/deploy.ts b/packages/core/installMachine/installSteps/vercel/deploy.ts index 6bead4d..fda997c 100644 --- a/packages/core/installMachine/installSteps/vercel/deploy.ts +++ b/packages/core/installMachine/installSteps/vercel/deploy.ts @@ -1,6 +1,7 @@ import { execSync } from 'child_process'; import { execAsync } from '../../../utils/execAsync'; import { logger } from '../../../utils/logger'; +import { getShortestVercelAlias } from './utils/getShortestVercelAlias'; export const deployVercelProject = async () => { await logger.withSpinner('vercel', 'Connecting Vercel to Git...', async (spinner) => { @@ -17,15 +18,20 @@ export const deployVercelProject = async () => { logger.log('vercel', 'Creating production deployment...'); - const productionUrl = execSync('vercel --prod', { + const productionUrl = execSync('npx vercel --prod', { stdio: ['inherit', 'pipe', 'inherit'], encoding: 'utf8', }); - if (productionUrl) { - logger.log('vercel', `You can access your production deployment at: \x1b[36m${productionUrl}\x1b[0m`); - } else { + const shortestVercelAlias = await getShortestVercelAlias(productionUrl); + + if (!productionUrl) { logger.log('vercel', 'Failed to create production deployment.'); return; } + + if (shortestVercelAlias) { + logger.log('vercel', `You can access your production deployment at: \x1b[36m${shortestVercelAlias}\x1b[0m`); + return; + } }; diff --git a/packages/core/installMachine/installSteps/vercel/utils/getShortestVercelAlias.ts b/packages/core/installMachine/installSteps/vercel/utils/getShortestVercelAlias.ts new file mode 100644 index 0000000..c410343 --- /dev/null +++ b/packages/core/installMachine/installSteps/vercel/utils/getShortestVercelAlias.ts @@ -0,0 +1,33 @@ +import { spawn } from 'child_process'; + +export const getShortestVercelAlias = (deploymentUrl: string): Promise => { + return new Promise((resolve) => { + const vercel = spawn('vercel', ['inspect', deploymentUrl]); + + let urls: string[] = []; + + vercel.stderr.on('data', (data) => { + const output = data.toString(); + + if (output.includes('.vercel.app')) { + const matches = output.match(/https:\/\/[\w-]+\.vercel\.app/g); + if (matches) { + urls = [...urls, ...matches]; + } + } + }); + + vercel.on('close', (code) => { + if (code !== 0 || urls.length === 0) { + resolve(null); + } else { + const shortestUrl = urls.reduce((shortest, current) => (current.length < shortest.length ? current : shortest)); + resolve(shortestUrl); + } + }); + + vercel.on('error', () => { + resolve(null); + }); + }); +};