Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

feat: listen at a dynamically generated port #221

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,39 @@ app.get('/revanced.apk', function (req, res) {
res.download(file);
});

const open = async () => {
const open = async (PORT) => {
if (require('os').platform === 'android') {
await require('util').promisify(
require('child_process').exec('termux-open-url http://localhost:8080')
require('child_process').exec(`termux-open-url http://localhost:${PORT}`)
);
} else require('open')('http://localhost:8080');
} else require('open')(`http://localhost:${PORT}`);
};

server.listen(8080, () => {
console.log('The webserver is now running!');
try {
console.log('Opening the app in the default browser...');
open('http://localhost:8080');
console.log('Done. Check if a browser window has opened');
} catch (e) {
console.log(
'Failed. Open up http://localhost:8080 manually in your browser.'
);
}
});
const listen = (PORT) => {
server.listen(PORT, () => {
console.log('The webserver is now running!');
try {
console.log('Opening the app in the default browser...');
open(PORT);
console.log('Done. Check if a browser window has opened');
} catch (e) {
console.log(
`Failed. Open up http://localhost:${PORT} manually in your browser.`
);
}
});
};

require('portfinder')
.getPortPromise()
.then((port) => {
console.log(`[builder] Using port ${port}`);
listen(port);
})
.catch((err) => {
console.error(`[builder] Unable to determine free ports. Reason:\n${err}`);
listen(8080);
});

process.on('uncaughtException', (reason) => {
console.log(
Expand Down
99 changes: 91 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"node-fetch-progress": "^1.0.2",
"open": "^8.4.0",
"path": "^0.12.7",
"portfinder": "^1.0.29",
"ws": "^8.8.1"
},
"devDependencies": {
Expand Down