oxtimely
Version:
A CLI tool to print the current computer's hostname
247 lines (217 loc) โข 7.51 kB
JavaScript
import figlet from "figlet";
import https from "https";
import os from "os";
import process from "process";
import chalk from "chalk";
import chalkAnimation from "chalk-animation";
import ora from "ora";
import gradient from "gradient-string";
import boxen from "boxen";
import { Command } from "commander";
import fs from "fs";
import path from "path";
import { homedir } from "os";
const CONFIG_PATH = path.join(homedir(), ".oxtimelyrc");
const program = new Command();
program
.option("--name <name>", "Your name", undefined)
.option("--off", "Disable startup feature")
.option("--reload", "Enable startup feature")
.option("--welcome", "Display welcome message")
.option("show-prompt", "Show prompt for shell init")
.parse(process.argv);
const options = program.opts();
// Show help if no options and no welcome
if (!process.argv.slice(2).length) {
console.log(gradient.morning("\n๐ Oxtimely Command Guide:"));
console.log(`
${chalk.cyan("oxtimely --name <your-name>")} โ Set your name and enable auto-start
${chalk.cyan("oxtimely --off")} โ Disable oxtimely auto startup
${chalk.cyan("oxtimely --reload")} โ Re-enable oxtimely on terminal start
${chalk.cyan("oxtimely")} โ Show system info (if enabled)
${chalk.cyan("oxtimely --welcome")} โ Force show welcome screen (used in startup script)
${chalk.gray("โ๏ธ Pro tip:")} Just use ${chalk.cyan("oxtimely --name Harsh")} once and enjoy auto magic!
`);
process.exit(0);
}
function updateConfig(name, enabled) {
const config = `name=${name}\nenabled=${enabled}`;
fs.writeFileSync(CONFIG_PATH, config);
}
function readConfig() {
if (!fs.existsSync(CONFIG_PATH)) return { name: "Friend", enabled: true };
const content = fs.readFileSync(CONFIG_PATH, "utf-8");
const lines = content.split("\n");
const name = lines.find(l => l.startsWith("name="))?.split("=")[1] || "Friend";
const enabled = lines.find(l => l.startsWith("enabled="))?.split("=")[1] === "true";
return { name, enabled };
}
function getShellProfilePath() {
const shell = process.env.SHELL;
const homeDir = os.homedir();
if (shell?.includes("zsh")) return path.join(homeDir, ".zshrc");
if (shell?.includes("bash")) return path.join(homeDir, ".bashrc");
if (shell?.includes("fish")) return path.join(homeDir, ".config/fish/config.fish");
return null;
}
function enableAutoPrompt() {
const shellPath = getShellProfilePath();
if (!shellPath) {
console.log("โ Unable to detect shell config file.");
return;
}
const command = "oxtimely --welcome";
const marker = "# >>> oxtimely auto-prompt >>>";
const content = fs.readFileSync(shellPath, "utf8");
if (!content.includes(marker)) {
const appendBlock = `
${marker}
if command -v oxtimely &> /dev/null; then
${command}
fi
# <<< oxtimely auto-prompt <<<
`;
fs.appendFileSync(shellPath, appendBlock);
console.log(`โ
oxtimely prompt enabled in ${path.basename(shellPath)}. Restart your terminal to see it!`);
}
}
function disableAutoPrompt() {
const shellPath = getShellProfilePath();
if (!shellPath) return;
const content = fs.readFileSync(shellPath, "utf8");
const newContent = content.replace(/# >>> oxtimely auto-prompt >>>[\s\S]*?# <<< oxtimely auto-prompt <<</, "");
fs.writeFileSync(shellPath, newContent);
console.log(`โ oxtimely prompt disabled in ${path.basename(shellPath)}.`);
}
if (options.off) {
const cfg = readConfig();
updateConfig(cfg.name, false);
disableAutoPrompt();
console.log(chalk.red("๐ OxTimely startup disabled"));
process.exit(0);
}
if (options.reload) {
const cfg = readConfig();
updateConfig(cfg.name, true);
enableAutoPrompt();
console.log(chalk.green("๐ OxTimely startup enabled"));
process.exit(0);
}
if (options.name) {
updateConfig(options.name, true);
enableAutoPrompt();
console.log(chalk.green("โ
Name set and auto startup enabled."));
process.exit(0);
}
let USER_NAME;
const cfg = readConfig();
USER_NAME = cfg.name;
if (!cfg.enabled && !options.welcome) {
process.exit(0);
}
function fetchPublicIP(callback) {
https.get("https://api.ipify.org?format=json", (res) => {
let ipData = "";
res.on("data", chunk => ipData += chunk);
res.on("end", () => {
try {
const ip = JSON.parse(ipData).ip;
callback(null, ip);
} catch (err) {
callback("IP fetch error");
}
});
}).on("error", () => callback("IP fetch error"));
}
function getCurrentTime() {
return new Date().toLocaleString("en-IN", { timeZone: "Asia/Kolkata" });
}
function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === "IPv4" && !iface.internal) {
return iface.address;
}
}
}
return "Unknown";
}
function getCPUDetails() {
const cpus = os.cpus();
const model = cpus[0]?.model || "Unknown";
const cores = cpus.length;
return { model, cores };
}
function getProcessDetails() {
return {
pid: process.pid,
memory: (process.memoryUsage().rss / 1024 / 1024).toFixed(2) + " MB",
uptime: (process.uptime() / 60).toFixed(2) + " mins",
nodeVersion: process.version,
platform: process.platform,
};
}
function fetchRandomQuote() {
const spinner = ora("๐งโโ๏ธ Fetching Zen Quote...").start();
https.get("https://zenquotes.io/api/random", (res) => {
let data = "";
res.on("data", chunk => data += chunk);
res.on("end", () => {
spinner.stop();
try {
const parsed = JSON.parse(data);
if (Array.isArray(parsed) && parsed.length > 0) {
const { q: quote, a: author } = parsed[0];
console.log(
`\n${chalk.gray(`[๐ก Quote @ ${new Date().toLocaleTimeString()}]`)}\n` +
chalk.greenBright(`"${quote.trim()}"`) + "\n" +
chalk.yellow(`โ ${author}\n`)
);
} else {
throw new Error("Unexpected response format.");
}
} catch (e) {
console.error(chalk.red("โ Failed to parse quote data."));
}
});
}).on("error", (err) => {
spinner.fail("โ Failed to fetch quote: " + err.message);
});
}
figlet(USER_NAME.toUpperCase(), async function (err, asciiName) {
if (err) return console.log("Something went wrong...");
const animation = chalkAnimation.rainbow(asciiName);
const now = getCurrentTime();
const localIP = getLocalIP();
const cpu = getCPUDetails();
const proc = getProcessDetails();
await new Promise(res => setTimeout(res, 2000));
animation.stop();
fetchPublicIP((err, publicIP) => {
if (err) publicIP = "Unknown";
const info = `
${gradient.atlas(`๐ง Hello ${USER_NAME}!`)}
${chalk.cyan("๐
Date & Time:")} ${now}
${chalk.cyan("๐ Public IP:")} ${publicIP}
${chalk.cyan("๐ Local IP:")} ${localIP}
${chalk.blue("๐ฅ๏ธ CPU:")} ${cpu.model} (${cpu.cores} cores)
${chalk.blue("โ๏ธ PID:")} ${proc.pid}
${chalk.blue("๐ฆ Memory:")} ${proc.memory}
${chalk.blue("โฑ๏ธ Uptime:")} ${proc.uptime}
${chalk.blue("๐งช Node:")} ${proc.nodeVersion}
${chalk.blue("๐ป Platform:")} ${proc.platform}
`;
console.log(
boxen(info, {
padding: 1,
borderStyle: "round",
borderColor: "magentaBright",
title: "๐ System Info",
titleAlignment: "center",
})
);
fetchRandomQuote();
});
});