From 0ee5cace757b4105daff0215ee723d5b2bcf2aa2 Mon Sep 17 00:00:00 2001 From: veganbeef Date: Wed, 2 Jul 2025 13:52:59 -0700 Subject: [PATCH] feat: accept ports as args to scripts --- bin/init.js | 2 -- package.json | 4 ++-- scripts/cleanup.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ scripts/dev.js | 35 ++++++++++++++++++++++++++--------- 4 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 scripts/cleanup.js diff --git a/bin/init.js b/bin/init.js index 6766d16..3968b60 100644 --- a/bin/init.js +++ b/bin/init.js @@ -541,7 +541,6 @@ export async function init(projectName = null, autoAcceptDefaults = false) { ); fs.writeFileSync(constantsPath, constantsContent); - console.log('✅ Updated constants.ts with user configuration'); } else { console.log('⚠️ constants.ts not found, skipping constants update'); } @@ -556,7 +555,6 @@ export async function init(projectName = null, autoAcceptDefaults = false) { fs.appendFileSync(envPath, `\nUSE_TUNNEL="${answers.useTunnel}"`); fs.unlinkSync(envExamplePath); - console.log('\nCreated .env.local file from .env.example'); } else { console.log('\n.env.example does not exist, skipping copy and remove operations'); } diff --git a/package.json b/package.json index bce1b14..3666086 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@neynar/create-farcaster-mini-app", - "version": "1.5.2", + "version": "1.5.3", "type": "module", "private": false, "access": "public", @@ -37,7 +37,7 @@ "lint": "next lint", "deploy:vercel": "node scripts/deploy.js", "deploy:raw": "vercel --prod", - "cleanup": "lsof -ti :3000 | xargs kill -9" + "cleanup": "node scripts/cleanup.js" }, "bin": { "@neynar/create-farcaster-mini-app": "./bin/index.js" diff --git a/scripts/cleanup.js b/scripts/cleanup.js new file mode 100644 index 0000000..aed5bca --- /dev/null +++ b/scripts/cleanup.js @@ -0,0 +1,45 @@ +#!/usr/bin/env node + +const { execSync } = require('child_process'); + +// Parse arguments +const args = process.argv.slice(2); +let port = 3000; // default port + +// Look for --port=XXXX, --port XXXX, -p=XXXX, or -p XXXX +args.forEach((arg, index) => { + if (arg.startsWith('--port=')) { + port = arg.split('=')[1]; + } else if (arg === '--port' && args[index + 1]) { + port = args[index + 1]; + } else if (arg.startsWith('-p=')) { + port = arg.split('=')[1]; + } else if (arg === '-p' && args[index + 1]) { + port = args[index + 1]; + } +}); + +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`); + } else { + console.log(`No processes found on port ${port}`); + } +} catch (error) { + if (error.status === 1) { + // lsof returns status 1 when no processes found + console.log(`No processes found on port ${port}`); + } else { + console.error(`Error: ${error.message}`); + process.exit(1); + } +} \ No newline at end of file diff --git a/scripts/dev.js b/scripts/dev.js index 23c2a86..a16a46f 100755 --- a/scripts/dev.js +++ b/scripts/dev.js @@ -15,6 +15,23 @@ let tunnel; let nextDev; let isCleaningUp = false; +// Parse command line arguments for port +const args = process.argv.slice(2); +let port = 3000; // default port + +// Look for --port=XXXX, --port XXXX, -p=XXXX, or -p XXXX +args.forEach((arg, index) => { + if (arg.startsWith('--port=')) { + port = parseInt(arg.split('=')[1]); + } else if (arg === '--port' && args[index + 1]) { + port = parseInt(args[index + 1]); + } else if (arg.startsWith('-p=')) { + port = parseInt(arg.split('=')[1]); + } else if (arg === '-p' && args[index + 1]) { + port = parseInt(args[index + 1]); + } +}); + async function checkPort(port) { return new Promise((resolve) => { const server = createServer(); @@ -67,12 +84,12 @@ async function killProcessOnPort(port) { } async function startDev() { - // Check if port 3000 is already in use - const isPortInUse = await checkPort(3000); + // Check if the specified port is already in use + const isPortInUse = await checkPort(port); if (isPortInUse) { - console.error('Port 3000 is already in use. To find and kill the process using this port:\n\n' + + 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 :3000\n' + + ? `1. Run: netstat -ano | findstr :${port}\n` + '2. Note the PID (Process ID) from the output\n' + '3. Run: taskkill /PID /F\n' : `On macOS/Linux, run:\nnpm run cleanup\n`) + @@ -85,7 +102,7 @@ async function startDev() { if (useTunnel) { // Start localtunnel and get URL - tunnel = await localtunnel({ port: 3000 }); + tunnel = await localtunnel({ port: port }); let ip; try { ip = await fetch('https://ipv4.icanhazip.com').then(res => res.text()).then(ip => ip.trim()); @@ -117,7 +134,7 @@ async function startDev() { 5. Click "Preview" (note that it may take ~10 seconds to load) `); } else { - miniAppUrl = 'http://localhost:3000'; + miniAppUrl = `http://localhost:${port}`; console.log(` 💻 To test your mini app: 1. Open the Warpcast Mini App Developer Tools: https://warpcast.com/~/developers @@ -130,7 +147,7 @@ async function startDev() { // Start next dev with appropriate configuration const nextBin = path.normalize(path.join(projectRoot, 'node_modules', '.bin', 'next')); - nextDev = spawn(nextBin, ['dev'], { + nextDev = spawn(nextBin, ['dev', '-p', port.toString()], { stdio: 'inherit', env: { ...process.env, NEXT_PUBLIC_URL: miniAppUrl, NEXTAUTH_URL: miniAppUrl }, cwd: projectRoot, @@ -174,8 +191,8 @@ async function startDev() { } } - // Force kill any remaining processes on port 3000 - await killProcessOnPort(3000); + // Force kill any remaining processes on the specified port + await killProcessOnPort(port); } catch (error) { console.error('Error during cleanup:', error); } finally {