formatting

This commit is contained in:
Shreyaschorge
2025-07-07 14:10:47 +05:30
parent f42a5f8d33
commit 193dffe03a
64 changed files with 6398 additions and 985 deletions

View File

@@ -26,9 +26,9 @@ async function lookupFidByCustodyAddress(custodyAddress, apiKey) {
`https://api.neynar.com/v2/farcaster/user/bulk-by-address?addresses=${lowerCasedCustodyAddress}&address_types=custody_address`,
{
headers: {
'accept': 'application/json',
'x-api-key': 'FARCASTER_V2_FRAMES_DEMO'
}
accept: 'application/json',
'x-api-key': 'FARCASTER_V2_FRAMES_DEMO',
},
}
);
@@ -37,7 +37,10 @@ async function lookupFidByCustodyAddress(custodyAddress, apiKey) {
}
const data = await response.json();
if (!data[lowerCasedCustodyAddress]?.length || !data[lowerCasedCustodyAddress][0].custody_address) {
if (
!data[lowerCasedCustodyAddress]?.length ||
!data[lowerCasedCustodyAddress][0].custody_address
) {
throw new Error('No FID found for this custody address');
}
@@ -51,19 +54,22 @@ async function loadEnvLocal() {
{
type: 'confirm',
name: 'loadLocal',
message: 'Found .env.local, likely created by the install script - would you like to load its values?',
default: false
}
message:
'Found .env.local, likely created by the install script - would you like to load its values?',
default: false,
},
]);
if (loadLocal) {
console.log('Loading values from .env.local...');
const localEnv = dotenv.parse(fs.readFileSync('.env.local'));
// Copy all values except SEED_PHRASE to .env
const envContent = fs.existsSync('.env') ? fs.readFileSync('.env', 'utf8') + '\n' : '';
const envContent = fs.existsSync('.env')
? fs.readFileSync('.env', 'utf8') + '\n'
: '';
let newEnvContent = envContent;
for (const [key, value] of Object.entries(localEnv)) {
if (key !== 'SEED_PHRASE') {
// Update process.env
@@ -74,7 +80,7 @@ async function loadEnvLocal() {
}
}
}
// Write updated content to .env
fs.writeFileSync('.env', newEnvContent);
console.log('✅ Values from .env.local have been written to .env');
@@ -102,9 +108,13 @@ const projectRoot = path.join(__dirname, '..');
async function validateDomain(domain) {
// Remove http:// or https:// if present
const cleanDomain = domain.replace(/^https?:\/\//, '');
// Basic domain validation
if (!cleanDomain.match(/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/)) {
if (
!cleanDomain.match(
/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/
)
) {
throw new Error('Invalid domain format');
}
@@ -120,8 +130,8 @@ async function queryNeynarApp(apiKey) {
`https://api.neynar.com/portal/app_by_api_key`,
{
headers: {
'x-api-key': apiKey
}
'x-api-key': apiKey,
},
}
);
const data = await response.json();
@@ -142,24 +152,36 @@ async function validateSeedPhrase(seedPhrase) {
}
}
async function generateFarcasterMetadata(domain, fid, accountAddress, seedPhrase, webhookUrl) {
async function generateFarcasterMetadata(
domain,
fid,
accountAddress,
seedPhrase,
webhookUrl
) {
const header = {
type: 'custody',
key: accountAddress,
fid,
};
const encodedHeader = Buffer.from(JSON.stringify(header), 'utf-8').toString('base64');
const encodedHeader = Buffer.from(JSON.stringify(header), 'utf-8').toString(
'base64'
);
const payload = {
domain
domain,
};
const encodedPayload = Buffer.from(JSON.stringify(payload), 'utf-8').toString('base64url');
const encodedPayload = Buffer.from(JSON.stringify(payload), 'utf-8').toString(
'base64url'
);
const account = mnemonicToAccount(seedPhrase);
const signature = await account.signMessage({
message: `${encodedHeader}.${encodedPayload}`
const signature = await account.signMessage({
message: `${encodedHeader}.${encodedPayload}`,
});
const encodedSignature = Buffer.from(signature, 'utf-8').toString('base64url');
const encodedSignature = Buffer.from(signature, 'utf-8').toString(
'base64url'
);
const tags = process.env.NEXT_PUBLIC_MINI_APP_TAGS?.split(',');
@@ -167,17 +189,17 @@ async function generateFarcasterMetadata(domain, fid, accountAddress, seedPhrase
accountAssociation: {
header: encodedHeader,
payload: encodedPayload,
signature: encodedSignature
signature: encodedSignature,
},
frame: {
version: "1",
version: '1',
name: process.env.NEXT_PUBLIC_MINI_APP_NAME,
iconUrl: `https://${domain}/icon.png`,
homeUrl: `https://${domain}`,
imageUrl: `https://${domain}/api/opengraph-image`,
buttonTitle: process.env.NEXT_PUBLIC_MINI_APP_BUTTON_TEXT,
splashImageUrl: `https://${domain}/splash.png`,
splashBackgroundColor: "#f7f7f7",
splashBackgroundColor: '#f7f7f7',
webhookUrl,
description: process.env.NEXT_PUBLIC_MINI_APP_DESCRIPTION,
primaryCategory: process.env.NEXT_PUBLIC_MINI_APP_PRIMARY_CATEGORY,
@@ -190,7 +212,7 @@ async function main() {
try {
console.log('\n📝 Checking environment variables...');
console.log('Loading values from .env...');
// Load .env.local if user wants to
await loadEnvLocal();
@@ -199,16 +221,17 @@ async function main() {
{
type: 'input',
name: 'domain',
message: 'Enter the domain where your mini app will be deployed (e.g., example.com):',
validate: async (input) => {
message:
'Enter the domain where your mini app will be deployed (e.g., example.com):',
validate: async input => {
try {
await validateDomain(input);
return true;
} catch (error) {
return error.message;
}
}
}
},
},
]);
// Get frame name from user
@@ -218,13 +241,13 @@ async function main() {
name: 'frameName',
message: 'Enter the name for your mini app (e.g., My Cool Mini App):',
default: process.env.NEXT_PUBLIC_MINI_APP_NAME,
validate: (input) => {
validate: input => {
if (input.trim() === '') {
return 'Mini app name cannot be empty';
}
return true;
}
}
},
},
]);
// Get button text from user
@@ -233,14 +256,15 @@ async function main() {
type: 'input',
name: 'buttonText',
message: 'Enter the text for your mini app button:',
default: process.env.NEXT_PUBLIC_MINI_APP_BUTTON_TEXT || 'Launch Mini App',
validate: (input) => {
default:
process.env.NEXT_PUBLIC_MINI_APP_BUTTON_TEXT || 'Launch Mini App',
validate: input => {
if (input.trim() === '') {
return 'Button text cannot be empty';
}
return true;
}
}
},
},
]);
// Get Neynar configuration
@@ -254,9 +278,10 @@ async function main() {
{
type: 'password',
name: 'neynarApiKey',
message: 'Enter your Neynar API key (optional - leave blank to skip):',
default: null
}
message:
'Enter your Neynar API key (optional - leave blank to skip):',
default: null,
},
]);
neynarApiKey = inputNeynarApiKey;
} else {
@@ -284,14 +309,16 @@ async function main() {
}
// If we get here, the API key was invalid
console.log('\n⚠ Could not find Neynar app information. The API key may be incorrect.');
console.log(
'\n⚠ Could not find Neynar app information. The API key may be incorrect.'
);
const { retry } = await inquirer.prompt([
{
type: 'confirm',
name: 'retry',
message: 'Would you like to try a different API key?',
default: true
}
default: true,
},
]);
// Reset for retry
@@ -311,18 +338,19 @@ async function main() {
{
type: 'password',
name: 'seedPhrase',
message: 'Your farcaster custody account seed phrase is required to create a signature proving this app was created by you.\n' +
`⚠️ ${yellow}${italic}seed phrase is only used to sign the mini app manifest, then discarded${reset} ⚠️\n` +
'Seed phrase:',
validate: async (input) => {
message:
'Your farcaster custody account seed phrase is required to create a signature proving this app was created by you.\n' +
`⚠️ ${yellow}${italic}seed phrase is only used to sign the mini app manifest, then discarded${reset} ⚠️\n` +
'Seed phrase:',
validate: async input => {
try {
await validateSeedPhrase(input);
return true;
} catch (error) {
return error.message;
}
}
}
},
},
]);
seedPhrase = inputSeedPhrase;
} else {
@@ -333,22 +361,36 @@ async function main() {
const accountAddress = await validateSeedPhrase(seedPhrase);
console.log('✅ Generated account address from seed phrase');
const fid = await lookupFidByCustodyAddress(accountAddress, neynarApiKey ?? 'FARCASTER_V2_FRAMES_DEMO');
const fid = await lookupFidByCustodyAddress(
accountAddress,
neynarApiKey ?? 'FARCASTER_V2_FRAMES_DEMO'
);
// Generate and sign manifest
console.log('\n🔨 Generating mini app manifest...');
// Determine webhook URL based on environment variables
const webhookUrl = neynarApiKey && neynarClientId
? `https://api.neynar.com/f/app/${neynarClientId}/event`
: `${domain}/api/webhook`;
const metadata = await generateFarcasterMetadata(domain, fid, accountAddress, seedPhrase, webhookUrl);
console.log('\n✅ Mini app manifest generated' + (seedPhrase ? ' and signed' : ''));
// Determine webhook URL based on environment variables
const webhookUrl =
neynarApiKey && neynarClientId
? `https://api.neynar.com/f/app/${neynarClientId}/event`
: `${domain}/api/webhook`;
const metadata = await generateFarcasterMetadata(
domain,
fid,
accountAddress,
seedPhrase,
webhookUrl
);
console.log(
'\n✅ Mini app manifest generated' + (seedPhrase ? ' and signed' : '')
);
// Read existing .env file or create new one
const envPath = path.join(projectRoot, '.env');
let envContent = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf8') : '';
let envContent = fs.existsSync(envPath)
? fs.readFileSync(envPath, 'utf8')
: '';
// Add or update environment variables
const newEnvVars = [
@@ -366,10 +408,10 @@ async function main() {
`NEXT_PUBLIC_ANALYTICS_ENABLED="${process.env.NEXT_PUBLIC_ANALYTICS_ENABLED || 'false'}"`,
// Neynar configuration (if it exists in current env)
...(process.env.NEYNAR_API_KEY ?
[`NEYNAR_API_KEY="${process.env.NEYNAR_API_KEY}"`] : []),
...(neynarClientId ?
[`NEYNAR_CLIENT_ID="${neynarClientId}"`] : []),
...(process.env.NEYNAR_API_KEY
? [`NEYNAR_API_KEY="${process.env.NEYNAR_API_KEY}"`]
: []),
...(neynarClientId ? [`NEYNAR_CLIENT_ID="${neynarClientId}"`] : []),
// FID (if it exists in current env)
...(process.env.FID ? [`FID="${process.env.FID}"`] : []),
@@ -406,16 +448,21 @@ async function main() {
// Run next build
console.log('\nBuilding Next.js application...');
const nextBin = path.normalize(path.join(projectRoot, 'node_modules', '.bin', 'next'));
execSync(`"${nextBin}" build`, {
cwd: projectRoot,
const nextBin = path.normalize(
path.join(projectRoot, 'node_modules', '.bin', 'next')
);
execSync(`"${nextBin}" build`, {
cwd: projectRoot,
stdio: 'inherit',
shell: process.platform === 'win32'
shell: process.platform === 'win32',
});
console.log('\n✨ Build complete! Your mini app is ready for deployment. 🪐');
console.log('📝 Make sure to configure the environment variables from .env in your hosting provider');
console.log(
'\n✨ Build complete! Your mini app is ready for deployment. 🪐'
);
console.log(
'📝 Make sure to configure the environment variables from .env in your hosting provider'
);
} catch (error) {
console.error('\n❌ Error:', error.message);
process.exit(1);

View File

@@ -21,13 +21,13 @@ args.forEach((arg, index) => {
try {
console.log(`Checking for processes on port ${port}...`);
// Find processes using the port
const pids = execSync(`lsof -ti :${port}`, { encoding: 'utf8' }).trim();
if (pids) {
console.log(`Found processes: ${pids.replace(/\n/g, ', ')}`);
// Kill the processes
execSync(`kill -9 ${pids.replace(/\n/g, ' ')}`);
console.log(`✓ Processes on port ${port} have been terminated`);
@@ -42,4 +42,4 @@ try {
console.error(`Error: ${error.message}`);
process.exit(1);
}
}
}

View File

@@ -34,9 +34,9 @@ async function lookupFidByCustodyAddress(custodyAddress, apiKey) {
`https://api.neynar.com/v2/farcaster/user/bulk-by-address?addresses=${lowerCasedCustodyAddress}&address_types=custody_address`,
{
headers: {
'accept': 'application/json',
'x-api-key': apiKey
}
accept: 'application/json',
'x-api-key': apiKey,
},
}
);
@@ -45,32 +45,47 @@ async function lookupFidByCustodyAddress(custodyAddress, apiKey) {
}
const data = await response.json();
if (!data[lowerCasedCustodyAddress]?.length || !data[lowerCasedCustodyAddress][0].custody_address) {
if (
!data[lowerCasedCustodyAddress]?.length ||
!data[lowerCasedCustodyAddress][0].custody_address
) {
throw new Error('No FID found for this custody address');
}
return data[lowerCasedCustodyAddress][0].fid;
}
async function generateFarcasterMetadata(domain, fid, accountAddress, seedPhrase, webhookUrl) {
async function generateFarcasterMetadata(
domain,
fid,
accountAddress,
seedPhrase,
webhookUrl
) {
const trimmedDomain = domain.trim();
const header = {
type: 'custody',
key: accountAddress,
fid,
};
const encodedHeader = Buffer.from(JSON.stringify(header), 'utf-8').toString('base64');
const encodedHeader = Buffer.from(JSON.stringify(header), 'utf-8').toString(
'base64'
);
const payload = {
domain: trimmedDomain
domain: trimmedDomain,
};
const encodedPayload = Buffer.from(JSON.stringify(payload), 'utf-8').toString('base64url');
const encodedPayload = Buffer.from(JSON.stringify(payload), 'utf-8').toString(
'base64url'
);
const account = mnemonicToAccount(seedPhrase);
const signature = await account.signMessage({
message: `${encodedHeader}.${encodedPayload}`
const signature = await account.signMessage({
message: `${encodedHeader}.${encodedPayload}`,
});
const encodedSignature = Buffer.from(signature, 'utf-8').toString('base64url');
const encodedSignature = Buffer.from(signature, 'utf-8').toString(
'base64url'
);
const tags = process.env.NEXT_PUBLIC_MINI_APP_TAGS?.split(',');
@@ -78,17 +93,17 @@ async function generateFarcasterMetadata(domain, fid, accountAddress, seedPhrase
accountAssociation: {
header: encodedHeader,
payload: encodedPayload,
signature: encodedSignature
signature: encodedSignature,
},
frame: {
version: "1",
version: '1',
name: process.env.NEXT_PUBLIC_MINI_APP_NAME,
iconUrl: `https://${trimmedDomain}/icon.png`,
homeUrl: `https://${trimmedDomain}`,
imageUrl: `https://${trimmedDomain}/api/opengraph-image`,
buttonTitle: process.env.NEXT_PUBLIC_MINI_APP_BUTTON_TEXT,
splashImageUrl: `https://${trimmedDomain}/splash.png`,
splashBackgroundColor: "#f7f7f7",
splashBackgroundColor: '#f7f7f7',
webhookUrl: webhookUrl?.trim(),
description: process.env.NEXT_PUBLIC_MINI_APP_DESCRIPTION,
primaryCategory: process.env.NEXT_PUBLIC_MINI_APP_PRIMARY_CATEGORY,
@@ -104,15 +119,16 @@ async function loadEnvLocal() {
{
type: 'confirm',
name: 'loadLocal',
message: 'Found .env.local - would you like to load its values in addition to .env values? (except for SEED_PHRASE, values will be written to .env)',
default: true
}
message:
'Found .env.local - would you like to load its values in addition to .env values? (except for SEED_PHRASE, values will be written to .env)',
default: true,
},
]);
if (loadLocal) {
console.log('Loading values from .env.local...');
const localEnv = dotenv.parse(fs.readFileSync('.env.local'));
const allowedVars = [
'SEED_PHRASE',
'NEXT_PUBLIC_MINI_APP_NAME',
@@ -122,12 +138,14 @@ async function loadEnvLocal() {
'NEXT_PUBLIC_MINI_APP_BUTTON_TEXT',
'NEXT_PUBLIC_ANALYTICS_ENABLED',
'NEYNAR_API_KEY',
'NEYNAR_CLIENT_ID'
'NEYNAR_CLIENT_ID',
];
const envContent = fs.existsSync('.env') ? fs.readFileSync('.env', 'utf8') + '\n' : '';
const envContent = fs.existsSync('.env')
? fs.readFileSync('.env', 'utf8') + '\n'
: '';
let newEnvContent = envContent;
for (const [key, value] of Object.entries(localEnv)) {
if (allowedVars.includes(key)) {
process.env[key] = value;
@@ -136,7 +154,7 @@ async function loadEnvLocal() {
}
}
}
fs.writeFileSync('.env', newEnvContent);
console.log('✅ Values from .env.local have been written to .env');
}
@@ -149,7 +167,7 @@ async function loadEnvLocal() {
async function checkRequiredEnvVars() {
console.log('\n📝 Checking environment variables...');
console.log('Loading values from .env...');
await loadEnvLocal();
const requiredVars = [
@@ -157,20 +175,23 @@ async function checkRequiredEnvVars() {
name: 'NEXT_PUBLIC_MINI_APP_NAME',
message: 'Enter the name for your frame (e.g., My Cool Mini App):',
default: process.env.NEXT_PUBLIC_MINI_APP_NAME,
validate: input => input.trim() !== '' || 'Mini app name cannot be empty'
validate: input => input.trim() !== '' || 'Mini app name cannot be empty',
},
{
name: 'NEXT_PUBLIC_MINI_APP_BUTTON_TEXT',
message: 'Enter the text for your frame button:',
default: process.env.NEXT_PUBLIC_MINI_APP_BUTTON_TEXT ?? 'Launch Mini App',
validate: input => input.trim() !== '' || 'Button text cannot be empty'
}
default:
process.env.NEXT_PUBLIC_MINI_APP_BUTTON_TEXT ?? 'Launch Mini App',
validate: input => input.trim() !== '' || 'Button text cannot be empty',
},
];
const missingVars = requiredVars.filter(varConfig => !process.env[varConfig.name]);
const missingVars = requiredVars.filter(
varConfig => !process.env[varConfig.name]
);
if (missingVars.length > 0) {
console.log('\n⚠ Some required information is missing. Let\'s set it up:');
console.log("\n⚠ Some required information is missing. Let's set it up:");
for (const varConfig of missingVars) {
const { value } = await inquirer.prompt([
{
@@ -178,17 +199,22 @@ async function checkRequiredEnvVars() {
name: 'value',
message: varConfig.message,
default: varConfig.default,
validate: varConfig.validate
}
validate: varConfig.validate,
},
]);
process.env[varConfig.name] = value;
const envContent = fs.existsSync('.env') ? fs.readFileSync('.env', 'utf8') : '';
const envContent = fs.existsSync('.env')
? fs.readFileSync('.env', 'utf8')
: '';
if (!envContent.includes(`${varConfig.name}=`)) {
const newLine = envContent ? '\n' : '';
fs.appendFileSync('.env', `${newLine}${varConfig.name}="${value.trim()}"`);
fs.appendFileSync(
'.env',
`${newLine}${varConfig.name}="${value.trim()}"`
);
}
}
}
@@ -201,21 +227,23 @@ async function checkRequiredEnvVars() {
{
type: 'password',
name: 'seedPhrase',
message: 'Enter your Farcaster custody account seed phrase to sign the mini app manifest\n(optional -- leave blank to create an unsigned mini app)\n\nSeed phrase:',
default: null
}
message:
'Enter your Farcaster custody account seed phrase to sign the mini app manifest\n(optional -- leave blank to create an unsigned mini app)\n\nSeed phrase:',
default: null,
},
]);
if (seedPhrase) {
process.env.SEED_PHRASE = seedPhrase;
const { storeSeedPhrase } = await inquirer.prompt([
{
type: 'confirm',
name: 'storeSeedPhrase',
message: 'Would you like to store this seed phrase in .env.local for future use?',
default: false
}
message:
'Would you like to store this seed phrase in .env.local for future use?',
default: false,
},
]);
if (storeSeedPhrase) {
@@ -230,9 +258,9 @@ async function checkRequiredEnvVars() {
async function getGitRemote() {
try {
const remoteUrl = execSync('git remote get-url origin', {
const remoteUrl = execSync('git remote get-url origin', {
cwd: projectRoot,
encoding: 'utf8'
encoding: 'utf8',
}).trim();
return remoteUrl;
} catch (error) {
@@ -242,9 +270,9 @@ async function getGitRemote() {
async function checkVercelCLI() {
try {
execSync('vercel --version', {
execSync('vercel --version', {
stdio: 'ignore',
shell: process.platform === 'win32'
shell: process.platform === 'win32',
});
return true;
} catch (error) {
@@ -254,9 +282,9 @@ async function checkVercelCLI() {
async function installVercelCLI() {
console.log('Installing Vercel CLI...');
execSync('npm install -g vercel', {
execSync('npm install -g vercel', {
stdio: 'inherit',
shell: process.platform === 'win32'
shell: process.platform === 'win32',
});
}
@@ -271,25 +299,27 @@ async function getVercelToken() {
} catch (error) {
console.warn('Could not read Vercel token from config file');
}
// Try environment variable
if (process.env.VERCEL_TOKEN) {
return process.env.VERCEL_TOKEN;
}
// Try to extract from vercel whoami
try {
const whoamiOutput = execSync('vercel whoami', {
const whoamiOutput = execSync('vercel whoami', {
encoding: 'utf8',
stdio: 'pipe'
stdio: 'pipe',
});
// If we can get whoami, we're logged in, but we need the actual token
// The token isn't directly exposed, so we'll need to use CLI for some operations
console.log('✅ Verified Vercel CLI authentication');
return null; // We'll fall back to CLI operations
} catch (error) {
throw new Error('Not logged in to Vercel CLI. Please run this script again to login.');
throw new Error(
'Not logged in to Vercel CLI. Please run this script again to login.'
);
}
}
@@ -303,21 +333,25 @@ async function loginToVercel() {
console.log('2. Authorize GitHub access');
console.log('3. Complete the Vercel account setup in your browser');
console.log('4. Return here once your Vercel account is created\n');
console.log('\nNote: you may need to cancel this script with ctrl+c and run it again if creating a new vercel account');
console.log(
'\nNote: you may need to cancel this script with ctrl+c and run it again if creating a new vercel account'
);
const child = spawn('vercel', ['login'], {
stdio: 'inherit'
stdio: 'inherit',
});
await new Promise((resolve, reject) => {
child.on('close', (code) => {
child.on('close', code => {
resolve();
});
});
console.log('\n📱 Waiting for login to complete...');
console.log('If you\'re creating a new account, please complete the Vercel account setup in your browser first.');
console.log(
"If you're creating a new account, please complete the Vercel account setup in your browser first."
);
for (let i = 0; i < 150; i++) {
try {
execSync('vercel whoami', { stdio: 'ignore' });
@@ -349,11 +383,11 @@ async function setVercelEnvVarSDK(vercelClient, projectId, key, value) {
// Get existing environment variables
const existingVars = await vercelClient.projects.getEnvironmentVariables({
idOrName: projectId
idOrName: projectId,
});
const existingVar = existingVars.envs?.find(env =>
env.key === key && env.target?.includes('production')
const existingVar = existingVars.envs?.find(
env => env.key === key && env.target?.includes('production')
);
if (existingVar) {
@@ -363,8 +397,8 @@ async function setVercelEnvVarSDK(vercelClient, projectId, key, value) {
id: existingVar.id,
requestBody: {
value: processedValue,
target: ['production']
}
target: ['production'],
},
});
console.log(`✅ Updated environment variable: ${key}`);
} else {
@@ -375,15 +409,18 @@ async function setVercelEnvVarSDK(vercelClient, projectId, key, value) {
key: key,
value: processedValue,
type: 'encrypted',
target: ['production']
}
target: ['production'],
},
});
console.log(`✅ Created environment variable: ${key}`);
}
return true;
} catch (error) {
console.warn(`⚠️ Warning: Failed to set environment variable ${key}:`, error.message);
console.warn(
`⚠️ Warning: Failed to set environment variable ${key}:`,
error.message
);
return false;
}
}
@@ -395,7 +432,7 @@ async function setVercelEnvVarCLI(key, value, projectRoot) {
execSync(`vercel env rm ${key} production -y`, {
cwd: projectRoot,
stdio: 'ignore',
env: process.env
env: process.env,
});
} catch (error) {
// Ignore errors from removal
@@ -424,7 +461,7 @@ async function setVercelEnvVarCLI(key, value, projectRoot) {
cwd: projectRoot,
stdio: 'pipe', // Changed from 'inherit' to avoid interactive prompts
shell: true,
env: process.env
env: process.env,
});
fs.unlinkSync(tempFilePath);
@@ -435,72 +472,95 @@ async function setVercelEnvVarCLI(key, value, projectRoot) {
if (fs.existsSync(tempFilePath)) {
fs.unlinkSync(tempFilePath);
}
console.warn(`⚠️ Warning: Failed to set environment variable ${key}:`, error.message);
console.warn(
`⚠️ Warning: Failed to set environment variable ${key}:`,
error.message
);
return false;
}
}
async function setEnvironmentVariables(vercelClient, projectId, envVars, projectRoot) {
async function setEnvironmentVariables(
vercelClient,
projectId,
envVars,
projectRoot
) {
console.log('\n📝 Setting up environment variables...');
const results = [];
for (const [key, value] of Object.entries(envVars)) {
if (!value) continue;
let success = false;
// Try SDK approach first if we have a Vercel client
if (vercelClient && projectId) {
success = await setVercelEnvVarSDK(vercelClient, projectId, key, value);
}
// Fallback to CLI approach
if (!success) {
success = await setVercelEnvVarCLI(key, value, projectRoot);
}
results.push({ key, success });
}
// Report results
const failed = results.filter(r => !r.success);
if (failed.length > 0) {
console.warn(`\n⚠️ Failed to set ${failed.length} environment variables:`);
failed.forEach(r => console.warn(` - ${r.key}`));
console.warn('\nYou may need to set these manually in the Vercel dashboard.');
console.warn(
'\nYou may need to set these manually in the Vercel dashboard.'
);
}
return results;
}
async function deployToVercel(useGitHub = false) {
try {
console.log('\n🚀 Deploying to Vercel...');
// Ensure vercel.json exists
const vercelConfigPath = path.join(projectRoot, 'vercel.json');
if (!fs.existsSync(vercelConfigPath)) {
console.log('📝 Creating vercel.json configuration...');
fs.writeFileSync(vercelConfigPath, JSON.stringify({
buildCommand: "next build",
framework: "nextjs"
}, null, 2));
fs.writeFileSync(
vercelConfigPath,
JSON.stringify(
{
buildCommand: 'next build',
framework: 'nextjs',
},
null,
2
)
);
}
// Set up Vercel project
console.log('\n📦 Setting up Vercel project...');
console.log('An initial deployment is required to get an assigned domain that can be used in the mini app manifest\n');
console.log('\n⚠ Note: choosing a longer, more unique project name will help avoid conflicts with other existing domains\n');
execSync('vercel', {
console.log(
'An initial deployment is required to get an assigned domain that can be used in the mini app manifest\n'
);
console.log(
'\n⚠ Note: choosing a longer, more unique project name will help avoid conflicts with other existing domains\n'
);
execSync('vercel', {
cwd: projectRoot,
stdio: 'inherit',
shell: process.platform === 'win32'
shell: process.platform === 'win32',
});
// Load project info
const projectJson = JSON.parse(fs.readFileSync('.vercel/project.json', 'utf8'));
const projectJson = JSON.parse(
fs.readFileSync('.vercel/project.json', 'utf8')
);
const projectId = projectJson.projectId;
// Get Vercel token and initialize SDK client
@@ -509,12 +569,14 @@ async function deployToVercel(useGitHub = false) {
const token = await getVercelToken();
if (token) {
vercelClient = new Vercel({
bearerToken: token
bearerToken: token,
});
console.log('✅ Initialized Vercel SDK client');
}
} catch (error) {
console.warn('⚠️ Could not initialize Vercel SDK, falling back to CLI operations');
console.warn(
'⚠️ Could not initialize Vercel SDK, falling back to CLI operations'
);
}
// Get project details
@@ -525,22 +587,27 @@ async function deployToVercel(useGitHub = false) {
if (vercelClient) {
try {
const project = await vercelClient.projects.get({
idOrName: projectId
idOrName: projectId,
});
projectName = project.name;
domain = `${projectName}.vercel.app`;
console.log('🌐 Using project name for domain:', domain);
} catch (error) {
console.warn('⚠️ Could not get project details via SDK, using CLI fallback');
console.warn(
'⚠️ Could not get project details via SDK, using CLI fallback'
);
}
}
// Fallback to CLI method if SDK failed
if (!domain) {
const inspectOutput = execSync(`vercel project inspect ${projectId} 2>&1`, {
cwd: projectRoot,
encoding: 'utf8'
});
const inspectOutput = execSync(
`vercel project inspect ${projectId} 2>&1`,
{
cwd: projectRoot,
encoding: 'utf8',
}
);
const nameMatch = inspectOutput.match(/Name\s+([^\n]+)/);
if (nameMatch) {
@@ -554,7 +621,9 @@ async function deployToVercel(useGitHub = false) {
domain = `${projectName}.vercel.app`;
console.log('🌐 Using project name for domain:', domain);
} else {
throw new Error('Could not determine project name from inspection output');
throw new Error(
'Could not determine project name from inspection output'
);
}
}
}
@@ -565,110 +634,146 @@ async function deployToVercel(useGitHub = false) {
if (process.env.SEED_PHRASE) {
console.log('\n🔨 Generating mini app metadata...');
const accountAddress = await validateSeedPhrase(process.env.SEED_PHRASE);
fid = await lookupFidByCustodyAddress(accountAddress, process.env.NEYNAR_API_KEY ?? 'FARCASTER_V2_FRAMES_DEMO');
const webhookUrl = process.env.NEYNAR_API_KEY && process.env.NEYNAR_CLIENT_ID
? `https://api.neynar.com/f/app/${process.env.NEYNAR_CLIENT_ID}/event`
: `https://${domain}/api/webhook`;
fid = await lookupFidByCustodyAddress(
accountAddress,
process.env.NEYNAR_API_KEY ?? 'FARCASTER_V2_FRAMES_DEMO'
);
miniAppMetadata = await generateFarcasterMetadata(domain, fid, accountAddress, process.env.SEED_PHRASE, webhookUrl);
const webhookUrl =
process.env.NEYNAR_API_KEY && process.env.NEYNAR_CLIENT_ID
? `https://api.neynar.com/f/app/${process.env.NEYNAR_CLIENT_ID}/event`
: `https://${domain}/api/webhook`;
miniAppMetadata = await generateFarcasterMetadata(
domain,
fid,
accountAddress,
process.env.SEED_PHRASE,
webhookUrl
);
console.log('✅ Mini app metadata generated and signed');
}
// Prepare environment variables
const nextAuthSecret = process.env.NEXTAUTH_SECRET || crypto.randomBytes(32).toString('hex');
const nextAuthSecret =
process.env.NEXTAUTH_SECRET || crypto.randomBytes(32).toString('hex');
const vercelEnv = {
NEXTAUTH_SECRET: nextAuthSecret,
AUTH_SECRET: nextAuthSecret,
NEXTAUTH_URL: `https://${domain}`,
NEXT_PUBLIC_URL: `https://${domain}`,
...(process.env.NEYNAR_API_KEY && { NEYNAR_API_KEY: process.env.NEYNAR_API_KEY }),
...(process.env.NEYNAR_CLIENT_ID && { NEYNAR_CLIENT_ID: process.env.NEYNAR_CLIENT_ID }),
...(process.env.NEYNAR_API_KEY && {
NEYNAR_API_KEY: process.env.NEYNAR_API_KEY,
}),
...(process.env.NEYNAR_CLIENT_ID && {
NEYNAR_CLIENT_ID: process.env.NEYNAR_CLIENT_ID,
}),
...(miniAppMetadata && { MINI_APP_METADATA: miniAppMetadata }),
...Object.fromEntries(
Object.entries(process.env)
.filter(([key]) => key.startsWith('NEXT_PUBLIC_'))
)
Object.entries(process.env).filter(([key]) =>
key.startsWith('NEXT_PUBLIC_')
)
),
};
// Set environment variables
await setEnvironmentVariables(vercelClient, projectId, vercelEnv, projectRoot);
await setEnvironmentVariables(
vercelClient,
projectId,
vercelEnv,
projectRoot
);
// Deploy the project
if (useGitHub) {
console.log('\nSetting up GitHub integration...');
execSync('vercel link', {
execSync('vercel link', {
cwd: projectRoot,
stdio: 'inherit',
env: process.env
env: process.env,
});
console.log('\n📦 Deploying with GitHub integration...');
} else {
console.log('\n📦 Deploying local code directly...');
}
execSync('vercel deploy --prod', {
execSync('vercel deploy --prod', {
cwd: projectRoot,
stdio: 'inherit',
env: process.env
env: process.env,
});
// Verify actual domain after deployment
console.log('\n🔍 Verifying deployment domain...');
let actualDomain = domain;
if (vercelClient) {
try {
const deployments = await vercelClient.deployments.list({
projectId: projectId,
limit: 1
limit: 1,
});
if (deployments.deployments?.[0]?.url) {
actualDomain = deployments.deployments[0].url;
console.log('🌐 Verified actual domain:', actualDomain);
}
} catch (error) {
console.warn('⚠️ Could not verify domain via SDK, using assumed domain');
console.warn(
'⚠️ Could not verify domain via SDK, using assumed domain'
);
}
}
// Update environment variables if domain changed
if (actualDomain !== domain) {
console.log('🔄 Updating environment variables with correct domain...');
const webhookUrl = process.env.NEYNAR_API_KEY && process.env.NEYNAR_CLIENT_ID
? `https://api.neynar.com/f/app/${process.env.NEYNAR_CLIENT_ID}/event`
: `https://${actualDomain}/api/webhook`;
const webhookUrl =
process.env.NEYNAR_API_KEY && process.env.NEYNAR_CLIENT_ID
? `https://api.neynar.com/f/app/${process.env.NEYNAR_CLIENT_ID}/event`
: `https://${actualDomain}/api/webhook`;
const updatedEnv = {
NEXTAUTH_URL: `https://${actualDomain}`,
NEXT_PUBLIC_URL: `https://${actualDomain}`
NEXT_PUBLIC_URL: `https://${actualDomain}`,
};
if (miniAppMetadata) {
const updatedMetadata = await generateFarcasterMetadata(actualDomain, fid, await validateSeedPhrase(process.env.SEED_PHRASE), process.env.SEED_PHRASE, webhookUrl);
const updatedMetadata = await generateFarcasterMetadata(
actualDomain,
fid,
await validateSeedPhrase(process.env.SEED_PHRASE),
process.env.SEED_PHRASE,
webhookUrl
);
updatedEnv.MINI_APP_METADATA = updatedMetadata;
}
await setEnvironmentVariables(vercelClient, projectId, updatedEnv, projectRoot);
await setEnvironmentVariables(
vercelClient,
projectId,
updatedEnv,
projectRoot
);
console.log('\n📦 Redeploying with correct domain...');
execSync('vercel deploy --prod', {
execSync('vercel deploy --prod', {
cwd: projectRoot,
stdio: 'inherit',
env: process.env
env: process.env,
});
domain = actualDomain;
}
console.log('\n✨ Deployment complete! Your mini app is now live at:');
console.log(`🌐 https://${domain}`);
console.log('\n📝 You can manage your project at https://vercel.com/dashboard');
console.log(
'\n📝 You can manage your project at https://vercel.com/dashboard'
);
} catch (error) {
console.error('\n❌ Deployment failed:', error.message);
process.exit(1);
@@ -678,7 +783,9 @@ async function deployToVercel(useGitHub = false) {
async function main() {
try {
console.log('🚀 Vercel Mini App Deployment (SDK Edition)');
console.log('This script will deploy your mini app to Vercel using the Vercel SDK.');
console.log(
'This script will deploy your mini app to Vercel using the Vercel SDK.'
);
console.log('\nThe script will:');
console.log('1. Check for required environment variables');
console.log('2. Set up a Vercel project (new or existing)');
@@ -690,9 +797,9 @@ async function main() {
await import('@vercel/sdk');
} catch (error) {
console.log('📦 Installing @vercel/sdk...');
execSync('npm install @vercel/sdk', {
execSync('npm install @vercel/sdk', {
cwd: projectRoot,
stdio: 'inherit'
stdio: 'inherit',
});
console.log('✅ @vercel/sdk installed successfully');
}
@@ -709,8 +816,8 @@ async function main() {
type: 'confirm',
name: 'useGitHubDeploy',
message: 'Would you like to deploy from the GitHub repository?',
default: true
}
default: true,
},
]);
useGitHub = useGitHubDeploy;
} else {
@@ -722,10 +829,10 @@ async function main() {
message: 'What would you like to do?',
choices: [
{ name: 'Deploy local code directly', value: 'deploy' },
{ name: 'Set up GitHub repository first', value: 'setup' }
{ name: 'Set up GitHub repository first', value: 'setup' },
],
default: 'deploy'
}
default: 'deploy',
},
]);
if (action === 'setup') {
@@ -739,22 +846,21 @@ async function main() {
}
}
if (!await checkVercelCLI()) {
if (!(await checkVercelCLI())) {
console.log('Vercel CLI not found. Installing...');
await installVercelCLI();
}
if (!await loginToVercel()) {
if (!(await loginToVercel())) {
console.error('\n❌ Failed to log in to Vercel. Please try again.');
process.exit(1);
}
await deployToVercel(useGitHub);
} catch (error) {
console.error('\n❌ Error:', error.message);
process.exit(1);
}
}
main();
main();

View File

@@ -33,18 +33,18 @@ args.forEach((arg, index) => {
});
async function checkPort(port) {
return new Promise((resolve) => {
return new Promise(resolve => {
const server = createServer();
server.once('error', () => {
resolve(true); // Port is in use
});
server.once('listening', () => {
server.close();
resolve(false); // Port is free
});
server.listen(port);
});
}
@@ -54,29 +54,32 @@ async function killProcessOnPort(port) {
if (process.platform === 'win32') {
// Windows: Use netstat to find the process
const netstat = spawn('netstat', ['-ano', '|', 'findstr', `:${port}`]);
netstat.stdout.on('data', (data) => {
netstat.stdout.on('data', data => {
const match = data.toString().match(/\s+(\d+)$/);
if (match) {
const pid = match[1];
spawn('taskkill', ['/F', '/PID', pid]);
}
});
await new Promise((resolve) => netstat.on('close', resolve));
await new Promise(resolve => netstat.on('close', resolve));
} else {
// Unix-like systems: Use lsof
const lsof = spawn('lsof', ['-ti', `:${port}`]);
lsof.stdout.on('data', (data) => {
data.toString().split('\n').forEach(pid => {
if (pid) {
try {
process.kill(parseInt(pid), 'SIGKILL');
} catch (e) {
if (e.code !== 'ESRCH') throw e;
lsof.stdout.on('data', data => {
data
.toString()
.split('\n')
.forEach(pid => {
if (pid) {
try {
process.kill(parseInt(pid), 'SIGKILL');
} catch (e) {
if (e.code !== 'ESRCH') throw e;
}
}
}
});
});
});
await new Promise((resolve) => lsof.on('close', resolve));
await new Promise(resolve => lsof.on('close', resolve));
}
} catch (e) {
// Ignore errors if no process found
@@ -87,13 +90,15 @@ async function startDev() {
// Check if the specified port is already in use
const isPortInUse = await checkPort(port);
if (isPortInUse) {
console.error(`Port ${port} is already in use. To find and kill the process using this port:\n\n` +
(process.platform === 'win32'
? `1. Run: netstat -ano | findstr :${port}\n` +
'2. Note the PID (Process ID) from the output\n' +
'3. Run: taskkill /PID <PID> /F\n'
: `On macOS/Linux, run:\nnpm run cleanup\n`) +
'\nThen try running this command again.');
console.error(
`Port ${port} is already in use. To find and kill the process using this port:\n\n` +
(process.platform === 'win32'
? `1. Run: netstat -ano | findstr :${port}\n` +
'2. Note the PID (Process ID) from the output\n' +
'3. Run: taskkill /PID <PID> /F\n'
: `On macOS/Linux, run:\nnpm run cleanup\n`) +
'\nThen try running this command again.'
);
process.exit(1);
}
@@ -105,7 +110,9 @@ async function startDev() {
tunnel = await localtunnel({ port: port });
let ip;
try {
ip = await fetch('https://ipv4.icanhazip.com').then(res => res.text()).then(ip => ip.trim());
ip = await fetch('https://ipv4.icanhazip.com')
.then(res => res.text())
.then(ip => ip.trim());
} catch (error) {
console.error('Error getting IP address:', error);
}
@@ -143,15 +150,21 @@ async function startDev() {
4. Click "Preview" to test your mini app (note that it may take ~5 seconds to load the first time)
`);
}
// Start next dev with appropriate configuration
const nextBin = path.normalize(path.join(projectRoot, 'node_modules', '.bin', 'next'));
const nextBin = path.normalize(
path.join(projectRoot, 'node_modules', '.bin', 'next')
);
nextDev = spawn(nextBin, ['dev', '-p', port.toString()], {
stdio: 'inherit',
env: { ...process.env, NEXT_PUBLIC_URL: miniAppUrl, NEXTAUTH_URL: miniAppUrl },
env: {
...process.env,
NEXT_PUBLIC_URL: miniAppUrl,
NEXTAUTH_URL: miniAppUrl,
},
cwd: projectRoot,
shell: process.platform === 'win32' // Add shell option for Windows
shell: process.platform === 'win32', // Add shell option for Windows
});
// Handle cleanup
@@ -181,7 +194,7 @@ async function startDev() {
console.log('Note: Next.js process already terminated');
}
}
if (tunnel) {
try {
await tunnel.close();
@@ -209,4 +222,4 @@ async function startDev() {
}
}
startDev().catch(console.error);
startDev().catch(console.error);