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

View File

@ -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"

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 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 <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 {