forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplug-worker.ts
More file actions
93 lines (82 loc) · 1.93 KB
/
plug-worker.ts
File metadata and controls
93 lines (82 loc) · 1.93 KB
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
import path from "path"
import { createPlugTask, type PlugCtx, type PlugDeps } from "../../src/cli/cmd/plug"
import { Filesystem } from "../../src/util"
type Msg = {
dir: string
target: string
mod: string
global?: boolean
force?: boolean
globalDir?: string
vcs?: string
worktree?: string
directory?: string
holdMs?: number
}
function sleep(ms: number) {
return new Promise<void>((resolve) => {
setTimeout(resolve, ms)
})
}
function input() {
const raw = process.argv[2]
if (!raw) {
throw new Error("Missing plug worker input")
}
const msg = JSON.parse(raw) as Partial<Msg>
if (!msg.dir || !msg.target || !msg.mod) {
throw new Error("Invalid plug worker input")
}
return msg as Msg
}
function deps(msg: Msg): PlugDeps {
return {
spinner: () => ({
start() {},
stop() {},
}),
log: {
error() {},
info() {},
success() {},
},
resolve: async () => msg.target,
readText: (file) => Filesystem.readText(file),
write: async (file, text) => {
if (msg.holdMs && msg.holdMs > 0) {
await sleep(msg.holdMs)
}
await Filesystem.write(file, text)
},
exists: (file) => Filesystem.exists(file),
files: (dir, name) => [path.join(dir, `${name}.jsonc`), path.join(dir, `${name}.json`)],
global: msg.globalDir ?? path.join(msg.dir, ".global"),
}
}
function ctx(msg: Msg): PlugCtx {
return {
vcs: msg.vcs ?? "git",
worktree: msg.worktree ?? msg.dir,
directory: msg.directory ?? msg.dir,
}
}
async function main() {
const msg = input()
const run = createPlugTask(
{
mod: msg.mod,
global: msg.global,
force: msg.force,
},
deps(msg),
)
const ok = await run(ctx(msg))
if (!ok) {
throw new Error("Plug task failed")
}
}
await main().catch((err) => {
const text = err instanceof Error ? (err.stack ?? err.message) : String(err)
process.stderr.write(text)
process.exit(1)
})