-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
106 lines (79 loc) · 1.89 KB
/
index.ts
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
import { z } from 'zod'
import kink from '~/code/kink'
import _ from 'lodash'
import { CallCast, ReadCallCast } from './code/cast'
import { loadKink } from './code/host/kink'
import { makeCall } from './code/host'
export * from './code/cast'
export type WorkLoadBaseCast = Record<string, WorkLoadCast>
export type WorkTaskBaseCast = Record<string, WorkTaskCast>
export type TakeCast = z.ZodType<z.ZodTypeAny>
export type WorkLoadCast = {
read: ReadCallCast
take: TakeCast
}
export type WorkTaskCast = {
take: TakeCast
}
export type WorkTakeCast = {
host?: string
code?: string
load?: WorkLoadBaseCast
task?: WorkTaskBaseCast
}
export default class Work {
protected loadBase: WorkLoadBaseCast
protected taskBase: WorkTaskBaseCast
protected codeText?: string
protected hostText?: string
constructor(take?: WorkTakeCast) {
this.loadBase = {}
this.taskBase = {}
if (take?.host) {
this.host(take.host)
}
if (take?.code) {
this.code(take.code)
}
if (take?.load) {
this.load(take.load)
}
if (take?.task) {
this.task(take.task)
}
}
code(code: string) {
this.codeText = code
return this
}
host(host: string) {
this.hostText = host
return this
}
load(base: WorkLoadBaseCast) {
_.extend(this.loadBase, base)
}
task(base: WorkTaskBaseCast) {
_.extend(this.taskBase, base)
}
async call(name: string, call: CallCast) {
if (!this.taskBase[name]) {
throw new Error(`Unknown task '${name}'`)
}
const { take } = this.taskBase[name]
let callHead
try {
// will throw an error
callHead = take.parse(call)
} catch (e) {
return loadKink(e)
}
if (!callHead) {
return
}
if (!this.hostText || !this.codeText) {
return loadKink(kink('call_fail'))
}
return await makeCall(this.hostText, this.codeText, callHead)
}
}