feat: accept ports as args to scripts

This commit is contained in:
veganbeef 2025-07-02 13:52:59 -07:00
parent 4fbcbdba6f
commit 0ee5cace75
No known key found for this signature in database
4 changed files with 73 additions and 13 deletions

View File

@ -541,7 +541,6 @@ export async function init(projectName = null, autoAcceptDefaults = false) {
); );
fs.writeFileSync(constantsPath, constantsContent); fs.writeFileSync(constantsPath, constantsContent);
console.log('✅ Updated constants.ts with user configuration');
} else { } else {
console.log('⚠️ constants.ts not found, skipping constants update'); 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.appendFileSync(envPath, `\nUSE_TUNNEL="${answers.useTunnel}"`);
fs.unlinkSync(envExamplePath); fs.unlinkSync(envExamplePath);
console.log('\nCreated .env.local file from .env.example');
} else { } else {
console.log('\n.env.example does not exist, skipping copy and remove operations'); console.log('\n.env.example does not exist, skipping copy and remove operations');
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@neynar/create-farcaster-mini-app", "name": "@neynar/create-farcaster-mini-app",
"version": "1.5.2", "version": "1.5.3",
"type": "module", "type": "module",
"private": false, "private": false,
"access": "public", "access": "public",
@ -37,7 +37,7 @@
"lint": "next lint", "lint": "next lint",
"deploy:vercel": "node scripts/deploy.js", "deploy:vercel": "node scripts/deploy.js",
"deploy:raw": "vercel --prod", "deploy:raw": "vercel --prod",
"cleanup": "lsof -ti :3000 | xargs kill -9" "cleanup": "node scripts/cleanup.js"
}, },
"bin": { "bin": {
"@neynar/create-farcaster-mini-app": "./bin/index.js" "@neynar/create-farcaster-mini-app": "./bin/index.js"

45
scripts/cleanup.js Normal file
View File

@ -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);
}
}

View File

@ -15,6 +15,23 @@ let tunnel;
let nextDev; let nextDev;
let isCleaningUp = false; 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) { async function checkPort(port) {
return new Promise((resolve) => { return new Promise((resolve) => {
const server = createServer(); const server = createServer();
@ -67,12 +84,12 @@ async function killProcessOnPort(port) {
} }
async function startDev() { async function startDev() {
// Check if port 3000 is already in use // Check if the specified port is already in use
const isPortInUse = await checkPort(3000); const isPortInUse = await checkPort(port);
if (isPortInUse) { 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' (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' + '2. Note the PID (Process ID) from the output\n' +
'3. Run: taskkill /PID <PID> /F\n' '3. Run: taskkill /PID <PID> /F\n'
: `On macOS/Linux, run:\nnpm run cleanup\n`) + : `On macOS/Linux, run:\nnpm run cleanup\n`) +
@ -85,7 +102,7 @@ async function startDev() {
if (useTunnel) { if (useTunnel) {
// Start localtunnel and get URL // Start localtunnel and get URL
tunnel = await localtunnel({ port: 3000 }); tunnel = await localtunnel({ port: port });
let ip; let ip;
try { 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());
@ -117,7 +134,7 @@ async function startDev() {
5. Click "Preview" (note that it may take ~10 seconds to load) 5. Click "Preview" (note that it may take ~10 seconds to load)
`); `);
} else { } else {
miniAppUrl = 'http://localhost:3000'; miniAppUrl = `http://localhost:${port}`;
console.log(` console.log(`
💻 To test your mini app: 💻 To test your mini app:
1. Open the Warpcast Mini App Developer Tools: https://warpcast.com/~/developers 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 // 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'], { nextDev = spawn(nextBin, ['dev', '-p', port.toString()], {
stdio: 'inherit', stdio: 'inherit',
env: { ...process.env, NEXT_PUBLIC_URL: miniAppUrl, NEXTAUTH_URL: miniAppUrl }, env: { ...process.env, NEXT_PUBLIC_URL: miniAppUrl, NEXTAUTH_URL: miniAppUrl },
cwd: projectRoot, cwd: projectRoot,
@ -174,8 +191,8 @@ async function startDev() {
} }
} }
// Force kill any remaining processes on port 3000 // Force kill any remaining processes on the specified port
await killProcessOnPort(3000); await killProcessOnPort(port);
} catch (error) { } catch (error) {
console.error('Error during cleanup:', error); console.error('Error during cleanup:', error);
} finally { } finally {