@quasarbright/projection
Version:
A static site generator that creates a beautiful, interactive gallery to showcase your coding projects. Features search, filtering, tags, responsive design, and an admin UI.
116 lines • 4.29 kB
JavaScript
;
/**
* Utility for finding available ports
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PortFinder = void 0;
const net = __importStar(require("net"));
/**
* PortFinder provides utilities for finding available ports
*/
class PortFinder {
/**
* Check if a specific port is available
*/
static async isPortAvailable(port) {
return new Promise((resolve) => {
const server = net.createServer();
server.once('error', (err) => {
if (err.code === 'EADDRINUSE') {
resolve(false);
}
else {
resolve(false);
}
});
server.once('listening', () => {
server.close();
resolve(true);
});
server.listen(port);
});
}
/**
* Find an available port starting from the given port
*
* @param startPort - Port to start searching from
* @param maxAttempts - Maximum number of ports to try (default: 10)
* @returns The first available port found
* @throws Error if no available port is found within maxAttempts
*/
static async findAvailablePort(startPort, maxAttempts = 10) {
for (let i = 0; i < maxAttempts; i++) {
const port = startPort + i;
const available = await this.isPortAvailable(port);
if (available) {
return port;
}
}
throw new Error(`Could not find an available port. Tried ports ${startPort} to ${startPort + maxAttempts - 1}.`);
}
/**
* Find a port with fallback behavior
*
* If userSuppliedPort is true and the port is not available, throws an error.
* If userSuppliedPort is false and the port is not available, tries to find the next available port.
*
* @param port - The desired port number
* @param userSuppliedPort - Whether the port was explicitly supplied by the user
* @returns PortFinderResult with the available port and whether it was the requested one
* @throws Error if userSuppliedPort is true and the port is not available
*/
static async findPortWithFallback(port, userSuppliedPort) {
const available = await this.isPortAvailable(port);
if (available) {
return {
port,
wasRequested: true
};
}
// Port is not available
if (userSuppliedPort) {
throw new Error(`Port ${port} is already in use. Please choose a different port using --port flag.`);
}
// Try to find next available port
const nextPort = await this.findAvailablePort(port + 1);
return {
port: nextPort,
wasRequested: false
};
}
}
exports.PortFinder = PortFinder;
//# sourceMappingURL=port-finder.js.map