Skip to content

Commit

Permalink
feat: implement cache/clean flags
Browse files Browse the repository at this point in the history
  • Loading branch information
bgenia committed Nov 29, 2023
1 parent 573ef49 commit 4e03fd3
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions src/cli/commands/cache/clean/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
import { command } from "cmd-ts"
import { command, flag } from "cmd-ts"
import { rm } from "node:fs/promises"
import { cacheDirectory } from "@/cache"
import {
cacheDirectory,
globalCacheDirectory,
localCacheDirectory,
} from "@/cache"

async function cleanCache(directory: string) {
console.log(`Cleaning cache from '${directory}' . . .`)

await rm(directory, { force: true, recursive: true })

console.log("Complete.")
}

export const cleanCommand = command({
name: "clean",
description: "Clean cache",
args: {},
async handler() {
console.log(`Cleaning cache from '${cacheDirectory}' . . .`)
args: {
local: flag({
long: "local",
short: "l",
description: "Clean local cache (ignores configuration)",
defaultValue: () => false,
}),
global: flag({
long: "global",
short: "g",
description: "Clean global cache (ignores configuration)",
defaultValue: () => false,
}),
all: flag({
long: "all",
short: "a",
description: "Clean both local & global cache (ignores configuration)",
defaultValue: () => false,
}),
},
async handler(argv) {
if (argv.local || argv.all) {
await cleanCache(localCacheDirectory)
}

await rm(cacheDirectory, { force: true, recursive: true })
if (argv.global || argv.all) {
await cleanCache(globalCacheDirectory)
}

console.log("Complete.")
if (!argv.global && !argv.local && !argv.all) {
await cleanCache(cacheDirectory)
}
},
})

0 comments on commit 4e03fd3

Please sign in to comment.