Skip to content

Commit

Permalink
More PR review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Jun 17, 2024
1 parent fcde6cb commit 797104f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 48 deletions.
22 changes: 18 additions & 4 deletions templates/cli/lib/commands/run.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,28 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
const taken = await isPortTaken(port);

if(taken) {
log(`Port ${port} is already in use by another process.`);
port = null;
error(`Port ${port} is already in use by another process.`);
return;
}
}

if(!port) {
const answers = await inquirer.prompt(questionsRunFunctions[1]);
port = answers.port;
let portFound = fale;
port = 3000;
while(port < 3100) {
const taken = await isPortTaken(port);
if(!taken) {
portFound = true;
break;
}

port++;
}

if(!portFound) {
error('Could not auto-suggest an available port. Please configure port with `appwrite run --port YOUR_PORT` command.');
return;
}
}

// Configuration: Engine
Expand Down
45 changes: 1 addition & 44 deletions templates/cli/lib/questions.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -791,50 +791,7 @@ const questionsRunFunctions = [
})
return choices;
}
},
{
type: "input",
name: "port",
message: 'Which port would you like function to listen on?',
default: async () => {
let port = 3000;
while(port < 3100) {
const taken = await isPortTaken(port);
if(!taken) {
return port;
}

port++;
}

return 3000;
},
validate: function(value) {
const done = this.async();

(async () => {
if (typeof value !== 'number' && isNaN(+value)) {
throw Error(`Port needs to be a valid integer between 1024 and 65536.`);
}

value = +value;

if(value < 1024 || value > 65536) {
throw Error(`Port needs to be a valid integer between 1024 and 65536.`);
}

const taken = await isPortTaken(value);

if(taken) {
throw Error(`Port ${value} is in use by another process. Pick a different one.`);
}
})().then(() => {
done(null, true);
}).catch((err) => {
done(err.message);
});
},
},
}
];

module.exports = {
Expand Down

0 comments on commit 797104f

Please sign in to comment.