@eightshone/sshman
Version:
A simple cli ssh manager
39 lines • 1.59 kB
JavaScript
import { execSync } from "child_process";
function getGlobalInstalledVersion(pkg, manager) {
var _a, _b;
try {
let cmd = "";
switch (manager) {
case "pnpm":
cmd = `pnpm ls -g ${pkg} --json`;
break;
case "yarn":
cmd = `yarn global list --json`;
const output = execSync(cmd, {
stdio: ["pipe", "pipe", "ignore"], // stdin, stdout, stderr => ignore any warnings that could be printed to the terminal
}).toString();
const lines = output.split("\n");
for (const line of lines) {
const json = JSON.parse(line);
if (json.type === "info" && json.data.includes(pkg)) {
const match = json.data.match(/@eightshone\/sshman@([\d.]+)/);
return (match === null || match === void 0 ? void 0 : match[1]) || null;
}
}
return null;
default:
cmd = `npm ls -g ${pkg} --json`;
break;
}
const jsonOutput = execSync(cmd, {
stdio: ["pipe", "pipe", "ignore"],
}).toString();
const parsed = JSON.parse(jsonOutput);
return ((_b = (_a = parsed.dependencies) === null || _a === void 0 ? void 0 : _a[pkg]) === null || _b === void 0 ? void 0 : _b.version) || null;
}
catch (_c) {
return null;
}
}
export default getGlobalInstalledVersion;
//# sourceMappingURL=getGlobalInstalledVersion.js.map