add templates subcommand

This commit is contained in:
Dan Finch 2026-05-03 12:00:34 +02:00
parent 56524450dc
commit 2602b67938
3 changed files with 66 additions and 2 deletions

View file

@ -3,6 +3,7 @@ import { resolve } from "path"
import { check } from "../src/check"
import { sync } from "../src/sync"
import { promote } from "../src/promote"
import { templates } from "../src/templates"
const [command, ...rawArgs] = process.argv.slice(2)
const hasYes = rawArgs.includes("--yes")
@ -19,7 +20,10 @@ switch (command) {
case "promote":
await promote(targetDir, hasYes)
break
case "templates":
templates(rawArgs.includes("--full"))
break
default:
console.error("Usage: regime <check|sync|promote> [path] [--yes]")
console.error("Usage: regime <check|sync|promote|templates> [path] [--yes] [--full]")
process.exit(1)
}

View file

@ -14,7 +14,6 @@ import {
} from "./shared"
const green = Bun.color("green", "ansi")
const yellow = Bun.color("orange", "ansi")
const red = Bun.color("red", "ansi")
const purple = Bun.color("purple", "ansi")
const reset = "\x1b[0m"

61
src/templates.ts Normal file
View file

@ -0,0 +1,61 @@
import { readdirSync } from "node:fs"
import { join } from "node:path"
import { templatesDir, resolveTemplateConfig, readdirSyncRecursive } from "./shared"
const purple = Bun.color("green", "ansi")
const reset = "\x1b[0m"
interface TreeNode {
name: string
children: TreeNode[]
files: string[]
}
function buildTree(name: string, visited = new Set<string>()): TreeNode {
if (visited.has(name)) return { name, children: [], files: [] }
visited.add(name)
const config = resolveTemplateConfig(name)
const children = (config.inherits ?? []).map(p => buildTree(p, visited))
const dir = join(templatesDir, name)
const files = readdirSyncRecursive(dir).filter(f => f !== ".regime-template.json")
return { name, children, files }
}
function printTree(node: TreeNode, full: boolean, prefix = "", isLast = true, isRoot = true) {
const connector = isRoot ? "" : isLast ? "└── " : "├── "
const line = isRoot ? node.name : `${prefix}${connector}${node.name}`
console.log(line)
const childPrefix = isRoot ? "" : prefix + (isLast ? " " : "│ ")
if (full && node.files.length > 0) {
const hasChildren = node.children.length > 0
for (let i = 0; i < node.files.length; i++) {
const fileConnector = hasChildren || i < node.files.length - 1 ? "│ " : " "
const bullet = "·"
console.log(`${childPrefix}${fileConnector}${purple}${bullet} ${node.files[i]}${reset}`)
}
}
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i]
const last = i === node.children.length - 1
printTree(child, full, childPrefix, last, false)
}
}
export function templates(full: boolean) {
const entries = readdirSync(templatesDir, { withFileTypes: true })
.filter(e => e.isDirectory())
.map(e => e.name)
.sort()
for (let i = 0; i < entries.length; i++) {
const tree = buildTree(entries[i])
printTree(tree, full)
if (i < entries.length - 1) console.log()
}
}