Skip to content

Commit

Permalink
feat: get the shortest vercel production domain (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Chełminiak <[email protected]>
  • Loading branch information
KarolinaKopacz and maneike authored Nov 20, 2024
1 parent 01174bc commit d4878e3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
16 changes: 8 additions & 8 deletions packages/core/installMachine/index.ts
Original file line number Diff line number Diff line change
@@ -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 }) => {
Expand Down
14 changes: 10 additions & 4 deletions packages/core/installMachine/installSteps/vercel/deploy.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { spawn } from 'child_process';

export const getShortestVercelAlias = (deploymentUrl: string): Promise<string | null> => {
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);
});
});
};

0 comments on commit d4878e3

Please sign in to comment.