worktree-tool
Version:
A command-line tool for managing Git worktrees with integrated tmux/shell session management
40 lines • 1.31 kB
JavaScript
import net from "net";
export class PortManager {
parseRange(range) {
const match = /^(\d+)-(\d+)$/.exec(range);
if (!match?.[1] || !match[2]) {
throw new Error(`Invalid port range format: ${range}`);
}
return {
start: parseInt(match[1]),
end: parseInt(match[2]),
};
}
async isPortAvailable(port) {
return new Promise((resolve) => {
const server = net.createServer();
server.once("error", () => {
resolve(false);
});
server.once("listening", () => {
server.close();
resolve(true);
});
server.listen(port);
});
}
async findAvailablePorts(start, end, count) {
const available = [];
for (let port = start; port <= end && available.length < count; port++) {
if (await this.isPortAvailable(port)) {
available.push(port);
}
}
if (available.length < count) {
throw new Error(`Could not find ${String(count)} available ports in range ${String(start)}-${String(end)}`);
}
return available;
}
}
export const portManager = new PortManager();
//# sourceMappingURL=port-manager.js.map