@typed-assistant/builder 0.0.84 → 0.0.85
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/src/appProcess.tsx +10 -0
- package/src/setupWebserver.tsx +14 -5
- package/src/setupWebhook.tsx +0 -174
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/appProcess.tsx
CHANGED
|
@@ -45,6 +45,16 @@ export async function setup({
|
|
|
45
45
|
subprocesses,
|
|
46
46
|
)
|
|
47
47
|
},
|
|
48
|
+
onProcessError: async (message) => {
|
|
49
|
+
const messageAll = `${message}. Restarting app...`
|
|
50
|
+
logger.fatal({ emoji: "🚨" }, messageAll)
|
|
51
|
+
onProcessError?.(messageAll, addonUrl)
|
|
52
|
+
subprocesses = await killAndRestartApp(
|
|
53
|
+
entryFile,
|
|
54
|
+
{ mdiPaths },
|
|
55
|
+
subprocesses,
|
|
56
|
+
)
|
|
57
|
+
},
|
|
48
58
|
})
|
|
49
59
|
setupWatcher({
|
|
50
60
|
directoryToWatch,
|
package/src/setupWebserver.tsx
CHANGED
|
@@ -101,12 +101,14 @@ export const startWebappServer = async ({
|
|
|
101
101
|
basePath,
|
|
102
102
|
getSubprocesses,
|
|
103
103
|
onRestartAppRequest,
|
|
104
|
+
onProcessError,
|
|
104
105
|
}: {
|
|
105
106
|
basePath: string
|
|
106
107
|
getSubprocesses: () => {
|
|
107
108
|
app: Subprocess<"ignore", "pipe", "pipe">
|
|
108
109
|
}
|
|
109
110
|
onRestartAppRequest: () => void
|
|
111
|
+
onProcessError: (message: string) => void
|
|
110
112
|
}) => {
|
|
111
113
|
const buildResult = await Bun.build({
|
|
112
114
|
entrypoints: [tsEntryPoint],
|
|
@@ -344,6 +346,8 @@ export const startWebappServer = async ({
|
|
|
344
346
|
await server.stop()
|
|
345
347
|
})
|
|
346
348
|
|
|
349
|
+
let emptyStringCount = 0
|
|
350
|
+
|
|
347
351
|
// eslint-disable-next-line no-constant-condition
|
|
348
352
|
while (true) {
|
|
349
353
|
const app = getSubprocesses().app
|
|
@@ -380,11 +384,15 @@ export const startWebappServer = async ({
|
|
|
380
384
|
lastMessage = convertedMessage
|
|
381
385
|
}
|
|
382
386
|
if (convertedMessage === "") {
|
|
387
|
+
emptyStringCount += 1
|
|
388
|
+
const emptyStringMessage =
|
|
389
|
+
"Process is returning an empty string"
|
|
390
|
+
if (emptyStringCount === 10) {
|
|
391
|
+
onProcessError(emptyStringMessage)
|
|
392
|
+
}
|
|
383
393
|
subscribers.forEach((send) =>
|
|
384
|
-
send(
|
|
385
|
-
|
|
386
|
-
lastMessage,
|
|
387
|
-
),
|
|
394
|
+
send("Process is returning an empty string. This was the last non-empty message:\n\n" +
|
|
395
|
+
lastMessage),
|
|
388
396
|
)
|
|
389
397
|
logger.fatal(
|
|
390
398
|
{
|
|
@@ -393,11 +401,12 @@ export const startWebappServer = async ({
|
|
|
393
401
|
exitCode: getSubprocesses().app.exitCode,
|
|
394
402
|
}),
|
|
395
403
|
},
|
|
396
|
-
|
|
404
|
+
emptyStringMessage,
|
|
397
405
|
)
|
|
398
406
|
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
399
407
|
continue
|
|
400
408
|
}
|
|
409
|
+
emptyStringCount = 0
|
|
401
410
|
subscribers.forEach((send) => send(convertedMessage))
|
|
402
411
|
}
|
|
403
412
|
|
package/src/setupWebhook.tsx
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import { logger } from "@typed-assistant/logger"
|
|
2
|
-
import { handleFetchError } from "@typed-assistant/utils/getHassAPI"
|
|
3
|
-
import { withErrorHandling } from "@typed-assistant/utils/withErrorHandling"
|
|
4
|
-
import { z } from "zod"
|
|
5
|
-
|
|
6
|
-
const commonOptions = {
|
|
7
|
-
headers: {
|
|
8
|
-
Accept: "application/vnd.github+json",
|
|
9
|
-
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
|
|
10
|
-
"X-GitHub-Api-Version": "2022-11-28",
|
|
11
|
-
},
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const webhookIsSetup = async (webhookUrl: string) => {
|
|
15
|
-
const { error } = await fetch(webhookUrl, {
|
|
16
|
-
...commonOptions,
|
|
17
|
-
body: JSON.stringify({ check: "true" }),
|
|
18
|
-
})
|
|
19
|
-
.then(handleFetchError)
|
|
20
|
-
.then((d) => d.json())
|
|
21
|
-
|
|
22
|
-
if (error) {
|
|
23
|
-
logger.error(
|
|
24
|
-
{ additionalDetails: error.message, emoji: "🚨" },
|
|
25
|
-
`Failed to reach webhook "${webhookUrl}"`,
|
|
26
|
-
)
|
|
27
|
-
return false
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
logger.debug({ emoji: "🪝" }, "Webhook reached successfully: ")
|
|
31
|
-
return true
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const listRepoWebhooks = async () =>
|
|
35
|
-
withErrorHandling(() =>
|
|
36
|
-
fetch(
|
|
37
|
-
`https://api.github.com/repos/${process.env.GITHUB_USERNAME}/${process.env.GITHUB_REPO}/hooks`,
|
|
38
|
-
{ ...commonOptions },
|
|
39
|
-
)
|
|
40
|
-
.then(handleFetchError)
|
|
41
|
-
.then((d) => d?.json())
|
|
42
|
-
.then(z.array(Webhook).parse),
|
|
43
|
-
)()
|
|
44
|
-
|
|
45
|
-
// const deleteRepoWebhook = async (id: number) =>
|
|
46
|
-
// withErrorHandling(() =>
|
|
47
|
-
// fetch(
|
|
48
|
-
// `https://api.github.com/repos/${process.env.GITHUB_USERNAME}/${process.env.GITHUB_REPO}/hooks/${id}`,
|
|
49
|
-
// { ...commonOptions, method: "DELETE" },
|
|
50
|
-
// ),
|
|
51
|
-
// )()
|
|
52
|
-
|
|
53
|
-
// const deleteAllRepoWebhooks = async () => {
|
|
54
|
-
// const { data: webhooks, error } = await listRepoWebhooks()
|
|
55
|
-
|
|
56
|
-
// if (error) {
|
|
57
|
-
// logger.error("🚨 Failed fetching webhooks")
|
|
58
|
-
// logger.error(` ${error.message}`)
|
|
59
|
-
// return
|
|
60
|
-
// }
|
|
61
|
-
|
|
62
|
-
// await Promise.all(
|
|
63
|
-
// webhooks.map(async (webhook) => {
|
|
64
|
-
// await deleteRepoWebhook(webhook.id)
|
|
65
|
-
// logger.info("🚮 Webhook deleted: " + webhook.config.url)
|
|
66
|
-
// }),
|
|
67
|
-
// )
|
|
68
|
-
// }
|
|
69
|
-
|
|
70
|
-
const createRepoWebhook = async (webhookUrl: string) =>
|
|
71
|
-
withErrorHandling(() =>
|
|
72
|
-
fetch(
|
|
73
|
-
`https://api.github.com/repos/${process.env.GITHUB_USERNAME}/${process.env.GITHUB_REPO}/hooks`,
|
|
74
|
-
{
|
|
75
|
-
...commonOptions,
|
|
76
|
-
method: "POST",
|
|
77
|
-
body: JSON.stringify({
|
|
78
|
-
name: "web",
|
|
79
|
-
active: true,
|
|
80
|
-
config: {
|
|
81
|
-
url: webhookUrl,
|
|
82
|
-
content_type: "json",
|
|
83
|
-
insecure_ssl: "0",
|
|
84
|
-
},
|
|
85
|
-
events: ["push"],
|
|
86
|
-
}),
|
|
87
|
-
},
|
|
88
|
-
)
|
|
89
|
-
.then(handleFetchError)
|
|
90
|
-
.then((d) => d.json())
|
|
91
|
-
.then(Webhook.parse),
|
|
92
|
-
)()
|
|
93
|
-
|
|
94
|
-
const Webhook = z.object({
|
|
95
|
-
type: z.literal("Repository"),
|
|
96
|
-
id: z.number(),
|
|
97
|
-
name: z.literal("web"),
|
|
98
|
-
active: z.boolean(),
|
|
99
|
-
events: z.array(z.string()),
|
|
100
|
-
config: z.object({
|
|
101
|
-
content_type: z.string(),
|
|
102
|
-
insecure_ssl: z.enum(["0", "1"]),
|
|
103
|
-
url: z.string(),
|
|
104
|
-
}),
|
|
105
|
-
updated_at: z.string(),
|
|
106
|
-
created_at: z.string(),
|
|
107
|
-
url: z.string(),
|
|
108
|
-
test_url: z.string(),
|
|
109
|
-
ping_url: z.string(),
|
|
110
|
-
deliveries_url: z.string(),
|
|
111
|
-
last_response: z.object({
|
|
112
|
-
code: z.number().nullable(),
|
|
113
|
-
status: z.string().nullable(),
|
|
114
|
-
message: z.string().nullable(),
|
|
115
|
-
}),
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
type Webhook = z.infer<typeof Webhook>
|
|
119
|
-
|
|
120
|
-
const retryTimeout = 2000
|
|
121
|
-
let retries = 0
|
|
122
|
-
export const setupWebhook = async (webhookUrl: string): Promise<void> => {
|
|
123
|
-
const { data: webhooks, error } = await listRepoWebhooks()
|
|
124
|
-
|
|
125
|
-
if (error) {
|
|
126
|
-
if (retries < 5) {
|
|
127
|
-
retries++
|
|
128
|
-
logger.error(
|
|
129
|
-
{ emoji: "🔁" },
|
|
130
|
-
`Failed fetching webhooks. Retrying setup in ${retryTimeout / 1000}s...`,
|
|
131
|
-
)
|
|
132
|
-
setTimeout(setupWebhook, retryTimeout)
|
|
133
|
-
return
|
|
134
|
-
}
|
|
135
|
-
logger.error(
|
|
136
|
-
{ additionalDetails: error.message, emoji: "🚨" },
|
|
137
|
-
"Failed fetching webhooks. Giving up.",
|
|
138
|
-
)
|
|
139
|
-
return
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const webhookAlreadyExists = webhooks.some(
|
|
143
|
-
async (webhook) => webhook.config.url === webhookUrl,
|
|
144
|
-
)
|
|
145
|
-
|
|
146
|
-
if (webhookAlreadyExists) {
|
|
147
|
-
logger.info({ emoji: "🪝" }, "Webhook already set up")
|
|
148
|
-
return
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
await webhookIsSetup(webhookUrl)
|
|
152
|
-
|
|
153
|
-
const { data: webhook, error: createError } =
|
|
154
|
-
await createRepoWebhook(webhookUrl)
|
|
155
|
-
|
|
156
|
-
if (createError) {
|
|
157
|
-
if (retries < 5) {
|
|
158
|
-
retries++
|
|
159
|
-
logger.error(
|
|
160
|
-
{ emoji: "🔁" },
|
|
161
|
-
`Failed creating webhook. Retrying setup in ${retryTimeout / 1000}s...`,
|
|
162
|
-
)
|
|
163
|
-
setTimeout(setupWebhook, retryTimeout)
|
|
164
|
-
return
|
|
165
|
-
}
|
|
166
|
-
logger.error(
|
|
167
|
-
{ additionalDetails: createError.message, emoji: "🚨" },
|
|
168
|
-
"Failed creating webhook. Giving up.",
|
|
169
|
-
)
|
|
170
|
-
return
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
logger.info({ emoji: "🪝" }, "Webhook created: " + webhook.config.url)
|
|
174
|
-
}
|