diff --git a/packages/opencode/src/cli/cmd/cache.ts b/packages/opencode/src/cli/cmd/cache.ts index 4a95facdd..7985353ef 100644 --- a/packages/opencode/src/cli/cmd/cache.ts +++ b/packages/opencode/src/cli/cmd/cache.ts @@ -1,11 +1,10 @@ import type { Argv } from "yargs" import { cmd } from "./cmd" import * as prompts from "@clack/prompts" -import { UI } from "../ui" import { Global } from "../../global" +import { getDirectorySize, formatSize, shortenPath } from "../util" import fs from "fs/promises" import path from "path" -import os from "os" const CacheCleanCommand = cmd({ command: "clean", @@ -35,9 +34,8 @@ const CacheCleanCommand = cmd({ } const size = await getDirectorySize(Global.Path.cache) - const sizeStr = formatSize(size) - prompts.log.info(`Cache: ${shortenPath(Global.Path.cache)} (${sizeStr})`) + prompts.log.info(`Cache: ${shortenPath(Global.Path.cache)} (${formatSize(size)})`) if (args.dryRun) { prompts.log.warn("Dry run - no changes made") @@ -111,45 +109,11 @@ const CacheInfoCommand = cmd({ export const CacheCommand = cmd({ command: "cache", - describe: "manage cache", - builder: (yargs) => yargs.command(CacheCleanCommand).command(CacheInfoCommand).demandCommand(), + describe: "manage plugin and package cache", + builder: (yargs) => + yargs + .command(CacheCleanCommand) + .command(CacheInfoCommand) + .demandCommand(1, "Please specify a subcommand: clean or info"), async handler() {}, }) - -async function getDirectorySize(dir: string): Promise { - let total = 0 - - const walk = async (current: string) => { - const entries = await fs.readdir(current, { withFileTypes: true }).catch(() => []) - - for (const entry of entries) { - const full = path.join(current, entry.name) - if (entry.isDirectory()) { - await walk(full) - continue - } - if (entry.isFile()) { - const stat = await fs.stat(full).catch(() => null) - if (stat) total += stat.size - } - } - } - - await walk(dir) - return total -} - -function formatSize(bytes: number): string { - if (bytes < 1024) return `${bytes} B` - if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB` - if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB` - return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB` -} - -function shortenPath(p: string): string { - const home = os.homedir() - if (p.startsWith(home)) { - return p.replace(home, "~") - } - return p -} diff --git a/packages/opencode/src/cli/cmd/uninstall.ts b/packages/opencode/src/cli/cmd/uninstall.ts index 62210d575..0bcf548a8 100644 --- a/packages/opencode/src/cli/cmd/uninstall.ts +++ b/packages/opencode/src/cli/cmd/uninstall.ts @@ -3,6 +3,7 @@ import { UI } from "../ui" import * as prompts from "@clack/prompts" import { Installation } from "../../installation" import { Global } from "../../global" +import { getDirectorySize, formatSize, shortenPath } from "../util" import { $ } from "bun" import fs from "fs/promises" import path from "path" @@ -304,41 +305,3 @@ async function cleanShellConfig(file: string) { const output = filtered.join("\n") + "\n" await Bun.write(file, output) } - -async function getDirectorySize(dir: string): Promise { - let total = 0 - - const walk = async (current: string) => { - const entries = await fs.readdir(current, { withFileTypes: true }).catch(() => []) - - for (const entry of entries) { - const full = path.join(current, entry.name) - if (entry.isDirectory()) { - await walk(full) - continue - } - if (entry.isFile()) { - const stat = await fs.stat(full).catch(() => null) - if (stat) total += stat.size - } - } - } - - await walk(dir) - return total -} - -function formatSize(bytes: number): string { - if (bytes < 1024) return `${bytes} B` - if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB` - if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB` - return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB` -} - -function shortenPath(p: string): string { - const home = os.homedir() - if (p.startsWith(home)) { - return p.replace(home, "~") - } - return p -} diff --git a/packages/opencode/src/cli/util.ts b/packages/opencode/src/cli/util.ts new file mode 100644 index 000000000..b5940e6e4 --- /dev/null +++ b/packages/opencode/src/cli/util.ts @@ -0,0 +1,41 @@ +import fs from "fs/promises" +import path from "path" +import os from "os" + +export async function getDirectorySize(dir: string): Promise { + let total = 0 + + const walk = async (current: string) => { + const entries = await fs.readdir(current, { withFileTypes: true }).catch(() => []) + + for (const entry of entries) { + const full = path.join(current, entry.name) + if (entry.isDirectory()) { + await walk(full) + continue + } + if (entry.isFile()) { + const stat = await fs.stat(full).catch(() => null) + if (stat) total += stat.size + } + } + } + + await walk(dir) + return total +} + +export function formatSize(bytes: number): string { + if (bytes < 1024) return `${bytes} B` + if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB` + if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB` + return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB` +} + +export function shortenPath(p: string): string { + const home = os.homedir() + if (p.startsWith(home)) { + return p.replace(home, "~") + } + return p +}