create-new-express-app
Version:
A CLI tool to create a TypeScript Express application with best practices
50 lines (49 loc) • 1.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOnline = getOnline;
const node_child_process_1 = require("node:child_process");
const promises_1 = require("node:dns/promises");
const node_url_1 = __importDefault(require("node:url"));
function getProxy() {
if (process.env.https_proxy) {
return process.env.https_proxy;
}
try {
const httpsProxy = (0, node_child_process_1.execSync)("npm config get https-proxy").toString().trim();
return httpsProxy !== "null" ? httpsProxy : undefined;
}
catch (e) {
return;
}
}
async function getOnline() {
try {
await (0, promises_1.lookup)("registry.yarnpkg.com");
// If DNS lookup succeeds, we are online
return true;
}
catch {
// The DNS lookup failed, but we are still fine as long as a proxy has been set
const proxy = getProxy();
if (!proxy) {
return false;
}
const { hostname } = node_url_1.default.parse(proxy);
if (!hostname) {
// Invalid proxy URL
return false;
}
try {
await (0, promises_1.lookup)(hostname);
// If DNS lookup succeeds for the proxy server, we are online
return true;
}
catch {
// The DNS lookup for the proxy server also failed, so we are offline
return false;
}
}
}