mirror of
https://github.com/sst/opencode.git
synced 2025-12-23 10:11:41 +00:00
refactor: extract shared utilities to cli/util.ts
This commit is contained in:
parent
0cc6c70277
commit
60c167d342
3 changed files with 50 additions and 82 deletions
|
|
@ -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<number> {
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<number> {
|
||||
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
|
||||
}
|
||||
|
|
|
|||
41
packages/opencode/src/cli/util.ts
Normal file
41
packages/opencode/src/cli/util.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import fs from "fs/promises"
|
||||
import path from "path"
|
||||
import os from "os"
|
||||
|
||||
export async function getDirectorySize(dir: string): Promise<number> {
|
||||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue