mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-12-06 01:12:34 -05:00
feat: accept ports as args to scripts
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user