GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — opencode-workflows/src/registry.ts at main · rphang/opencode-workflows

Expand file tree

/

registry.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
200 lines (179 loc) · 7.2 KB
/

registry.ts

Copy path

File metadata and controls

200 lines (179 loc) · 7.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Saved-workflow discovery and saving (PARITY P11, P52, P53).
//
// Discovery order (first hit wins on a name clash):
// 1. project — every `.opencode/workflows/` from cwd up to the repo root, closest first
// 2. personal — `<XDG_CONFIG_HOME or ~/.config>/opencode/workflows/`
// 3. bundled — this package's `workflows/` directory
// Each `*.js` file is keyed by its `meta.name`. A file whose meta is not a pure literal (or is
// missing) is skipped entirely (P11) — it does not shadow a valid workflow further away.
// Within one directory, files are read in sorted file-name order, so duplicates resolve
// deterministically to the first file.
//
// Saving (P52) mirrors Claude Code's symlink rules: for the project location, refuse when
// `.opencode`, `.opencode/workflows` or the target file is a symlink; for the personal location,
// refuse only when the target file is a symlink (a dotfiles-managed config dir keeps working).
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { fileURLToPath } from "node:url"
import { extractMetaLoose } from "./meta.ts"
import type { WorkflowMeta } from "./types.ts"
export type WorkflowOrigin = "project" | "personal" | "bundled"
export interface ResolvedWorkflow {
meta: WorkflowMeta
source: string
path: string
origin: WorkflowOrigin
}
export interface RegistryOptions {
/** Environment used to resolve XDG_CONFIG_HOME (default: process.env). */
env?: Record<string, string | undefined>
/** Explicit config home (overrides env/home); personal dir = <configHome>/opencode/workflows. */
configHome?: string
/** Home directory used when XDG_CONFIG_HOME is unset (default: os.homedir()). */
home?: string
/** Bundled workflows directory (default: BUNDLED_WORKFLOWS_DIR). */
bundledDir?: string
}
export interface SaveOptions {
source: string
/** File base name (without .js); defaults to meta.name. */
name?: string
location: "project" | "personal"
cwd: string
env?: Record<string, string | undefined>
configHome?: string
home?: string
}
const DIR_PARTS = [".opencode", "workflows"] as const
/** This package's bundled workflows directory. */
export const BUNDLED_WORKFLOWS_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "workflows")
/** Closest ancestor of `cwd` (inclusive) containing a `.git` entry (dir or file), or undefined. */
export function findRepoRoot(cwd: string): string | undefined {
let dir = path.resolve(cwd)
for (;;) {
if (fs.existsSync(path.join(dir, ".git"))) return dir
const parent = path.dirname(dir)
if (parent === dir) return undefined
dir = parent
}
}
/** Directories from cwd up to the repo root (inclusive), closest first; just [cwd] outside a repo. */
function ancestorsToRoot(cwd: string): string[] {
const start = path.resolve(cwd)
const root = findRepoRoot(start)
if (!root) return [start]
const out: string[] = []
let dir = start
for (;;) {
out.push(dir)
if (dir === root) break
const parent = path.dirname(dir)
if (parent === dir) break
dir = parent
}
return out
}
/** Every candidate project `.opencode/workflows` directory, closest to cwd first. */
export function projectWorkflowDirs(cwd: string): string[] {
return ancestorsToRoot(cwd).map((d) => path.join(d, ...DIR_PARTS))
}
/** `<XDG_CONFIG_HOME or ~/.config>/opencode/workflows`. */
export function personalWorkflowsDir(env: Record<string, string | undefined> = process.env, home: string = os.homedir()): string {
const xdg = env.XDG_CONFIG_HOME
const base = xdg && xdg.trim() !== "" ? xdg : path.join(home, ".config")
return path.join(base, "opencode", "workflows")
}
function personalDir(opts: { env?: Record<string, string | undefined>; configHome?: string; home?: string }): string {
if (opts.configHome) return path.join(opts.configHome, "opencode", "workflows")
return personalWorkflowsDir(opts.env ?? process.env, opts.home ?? os.homedir())
}
function readDir(dir: string, origin: WorkflowOrigin): ResolvedWorkflow[] {
let names: string[]
try {
names = fs.readdirSync(dir)
} catch {
return []
}
const out: ResolvedWorkflow[] = []
for (const file of names.filter((n) => n.endsWith(".js")).sort()) {
const p = path.join(dir, file)
let source: string
try {
if (!fs.statSync(p).isFile()) continue
source = fs.readFileSync(p, "utf8")
} catch {
continue
}
const meta = extractMetaLoose(source)
if (!meta) continue
out.push({ meta, source, path: p, origin })
}
return out
}
function sources(cwd: string, opts: RegistryOptions): [string, WorkflowOrigin][] {
return [
...projectWorkflowDirs(cwd).map((d): [string, WorkflowOrigin] => [d, "project"]),
[personalDir(opts), "personal"],
[opts.bundledDir ?? BUNDLED_WORKFLOWS_DIR, "bundled"],
]
}
/** All visible workflows (shadowed duplicates removed), sorted by name. */
export function listWorkflows(cwd: string, opts: RegistryOptions = {}): ResolvedWorkflow[] {
const byName = new Map<string, ResolvedWorkflow>()
for (const [dir, origin] of sources(cwd, opts)) {
for (const w of readDir(dir, origin)) if (!byName.has(w.meta.name)) byName.set(w.meta.name, w)
}
return [...byName.values()].sort((a, b) => (a.meta.name < b.meta.name ? -1 : a.meta.name > b.meta.name ? 1 : 0))
}
/** The workflow that `/<name>` runs from `cwd`, or undefined. */
export function resolveWorkflow(name: string, cwd: string, opts: RegistryOptions = {}): ResolvedWorkflow | undefined {
for (const [dir, origin] of sources(cwd, opts)) {
const hit = readDir(dir, origin).find((w) => w.meta.name === name)
if (hit) return hit
}
return undefined
}
const SAFE_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/
function isSymlink(p: string): boolean {
try {
return fs.lstatSync(p).isSymbolicLink()
} catch {
return false
}
}
function refuseSymlink(p: string) {
if (isSymlink(p)) throw new Error(`Refusing to save workflow: ${p} is a symlink`)
}
/** Save a workflow script as `<dir>/<name>.js`; returns the written path. Throws on refusal. */
export function saveWorkflow(opts: SaveOptions): string {
const meta = extractMetaLoose(opts.source)
if (!meta) throw new Error("Cannot save workflow: script must start with `export const meta = {...}` as a pure literal with name and description")
const name = opts.name ?? meta.name
if (!SAFE_NAME.test(name) || name.includes("..")) {
throw new Error(`Cannot save workflow: invalid name '${name}' (letters, digits, '.', '_', '-' only; must not start with '.')`)
}
let dir: string
if (opts.location === "project") {
const bases = ancestorsToRoot(opts.cwd)
const existing = bases.find((b) => fs.existsSync(path.join(b, ...DIR_PARTS)))
const base = existing ?? bases[bases.length - 1]!
const dotDir = path.join(base, DIR_PARTS[0])
dir = path.join(dotDir, DIR_PARTS[1])
refuseSymlink(dotDir)
refuseSymlink(dir)
} else {
dir = personalDir(opts)
}
const target = path.join(dir, `${name}.js`)
refuseSymlink(target)
fs.mkdirSync(dir, { recursive: true })
// Re-check after mkdir in case a link appeared concurrently.
if (opts.location === "project") {
refuseSymlink(path.dirname(dir))
refuseSymlink(dir)
}
refuseSymlink(target)
fs.writeFileSync(target, opts.source)
return target
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.