UNPKG

@reliverse/rse

Version:

@reliverse/rse is your all-in-one companion for bootstrapping and improving any kind of projects (especially web apps built with frameworks like Next.js) — whether you're kicking off something new or upgrading an existing app. It is also a little AI-power

151 lines (150 loc) 4.5 kB
import { relinka } from "@reliverse/relinka"; import { defineCommand, multiselectPrompt, selectPrompt } from "@reliverse/rempts"; import { execa } from "execa"; import os from "os"; const popularGlobalNpmPackages = [ "typescript", "eslint", "biome", "@reliverse/relidler" ]; const popularDesktopApps = { darwin: ["obsidian", "vscode", "notion", "docker"], win32: ["obsidian", "vscode", "notion", "docker"], linux: ["obsidian", "vscode", "notion", "docker"] }; export default defineCommand({ meta: { name: "install", description: "Install global NPM packages or desktop apps" }, args: { // The user can optionally specify one or more items to install items: { type: "positional", required: false, array: true, description: "Apps or packages to install" } }, run: async ({ args }) => { const items = [args.items]; if (items && items.length > 0) { return await handleDirectInstall(items); } relinka("info", "\n\u25C6 Select Installation Type"); const installType = await selectPrompt({ title: "installType", content: "What do you want to install?", options: [ { value: "cli apps", label: "CLI apps" }, { value: "desktop apps", label: "Desktop apps" } ] }); if (installType === "cli apps") { await handleCliApps(); } else { await handleDesktopApps(); } } }); async function handleDirectInstall(items) { relinka("info", "\nDirect install of:", items.join(", ")); const { npmPackages, desktopApps } = separateApps(items); if (npmPackages.length > 0) { relinka("info", `Installing global NPM packages... ${npmPackages}`); await installGlobalNpmPackages(npmPackages); } if (desktopApps.length > 0) { relinka("info", `Installing desktop apps... ${desktopApps}`); await installDesktopApps(desktopApps); } } async function handleCliApps() { const selected = await multiselectPrompt({ title: "selected", content: "Select CLI apps to install (space to toggle)", options: popularGlobalNpmPackages.map((pkg) => ({ value: pkg, label: pkg })) }); if (!selected || selected.length === 0) { relinka("warn", "No CLI apps selected."); return; } await installGlobalNpmPackages(selected); } async function handleDesktopApps() { const platform = os.platform(); const apps = popularDesktopApps[platform] || []; if (apps.length === 0) { relinka("error", `No known desktop apps for ${platform}.`); return; } const selected = await multiselectPrompt({ title: "selected", content: "Select desktop apps to install (space to toggle)", options: apps.map((app) => ({ value: app, label: app })) }); if (!selected || selected.length === 0) { relinka("warn", "No desktop apps selected."); return; } await installDesktopApps(selected); } function separateApps(items) { const npmPackages = []; const desktopApps = []; for (const item of items) { if (popularGlobalNpmPackages.includes(item.toLowerCase()) || isLikelyNpmPackage(item)) { npmPackages.push(item); } else { desktopApps.push(item); } } return { npmPackages, desktopApps }; } function isLikelyNpmPackage(item) { if (item.includes(".") === false || item.startsWith("@")) { return true; } return false; } async function installGlobalNpmPackages(packages) { try { await execa("npm", ["install", "-g", ...packages], { stdio: "inherit" }); relinka( "success", `Global NPM install finished for: ${packages.join(", ")}` ); } catch (error) { relinka("error", "Failed to install NPM packages:", String(error)); } } async function installDesktopApps(apps) { const platform = os.platform(); for (const app of apps) { try { if (platform === "darwin") { relinka("info", `Installing ${app} via Homebrew...`); await execa("brew", ["install", app], { stdio: "inherit" }); } else if (platform === "win32") { relinka("info", `Installing ${app} via Chocolatey/winget...`); await execa("choco", ["install", app, "-y"], { stdio: "inherit" }); } else { relinka("info", `Installing ${app} via apt-get...`); await execa("sudo", ["apt-get", "install", "-y", app], { stdio: "inherit" }); } relinka("success", `Installed: ${app}`); } catch (error) { relinka("error", `Failed to install ${app}`, String(error)); } } }