@typed-assistant/builder 0.0.39 → 0.0.41

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typed-assistant/builder",
3
- "version": "0.0.39",
3
+ "version": "0.0.41",
4
4
  "exports": {
5
5
  "./appProcess": "./src/appProcess.tsx",
6
6
  "./bunInstall": "./src/bunInstall.tsx",
@@ -32,9 +32,6 @@ export async function setup({
32
32
  const directoryToWatch = join(process.cwd(), "./src")
33
33
  const addonUrl = `${slug}/ingress`
34
34
 
35
- checkProcesses(entryFile, { addonUrl, onProcessError })
36
- await setupGitSync()
37
-
38
35
  let subprocesses = await buildAndStartAppProcess(entryFile, {
39
36
  mdiPaths: mdiPaths,
40
37
  })
@@ -53,6 +50,9 @@ export async function setup({
53
50
  getSubprocesses: () => subprocesses,
54
51
  })
55
52
 
53
+ checkProcesses(entryFile, { addonUrl, onProcessError })
54
+ await setupGitSync()
55
+
56
56
  return subprocesses
57
57
  }
58
58
 
@@ -3,8 +3,6 @@ import { handleFetchError } from "@typed-assistant/utils/getHassAPI"
3
3
  import { withErrorHandling } from "@typed-assistant/utils/withErrorHandling"
4
4
  import { z } from "zod"
5
5
 
6
- const webhookUrl = `${process.env.HASS_EXTERNAL_URL}/webhook`
7
-
8
6
  const commonOptions = {
9
7
  headers: {
10
8
  Accept: "application/vnd.github+json",
@@ -13,6 +11,24 @@ const commonOptions = {
13
11
  },
14
12
  }
15
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(`🚨 Failed to reach webhook "${webhookUrl}"`)
24
+ logger.error(` ${error.message}`)
25
+ return false
26
+ }
27
+
28
+ logger.info("🪝 Webhook reached successfully: ")
29
+ return true
30
+ }
31
+
16
32
  const listRepoWebhooks = async () =>
17
33
  withErrorHandling(() =>
18
34
  fetch(
@@ -36,19 +52,20 @@ const deleteAllRepoWebhooks = async () => {
36
52
  const { data: webhooks, error } = await listRepoWebhooks()
37
53
 
38
54
  if (error) {
39
- logger.error("🚨 Failed fetching webhooks", error.message)
55
+ logger.error("🚨 Failed fetching webhooks")
56
+ logger.error(` ${error.message}`)
40
57
  return
41
58
  }
42
59
 
43
60
  await Promise.all(
44
61
  webhooks.map(async (webhook) => {
45
62
  await deleteRepoWebhook(webhook.id)
46
- logger.info("🚮 Webhook deleted: ", webhook.config.url)
63
+ logger.info("🚮 Webhook deleted: " + webhook.config.url)
47
64
  }),
48
65
  )
49
66
  }
50
67
 
51
- const createRepoWebhook = async () =>
68
+ const createRepoWebhook = async (webhookUrl: string) =>
52
69
  withErrorHandling(() =>
53
70
  fetch(
54
71
  `https://api.github.com/repos/${process.env.GITHUB_USERNAME}/${process.env.GITHUB_REPO}/hooks`,
@@ -100,7 +117,7 @@ type Webhook = z.infer<typeof Webhook>
100
117
 
101
118
  const retryTimeout = 2000
102
119
  let retries = 0
103
- export const setupWebhook = async (): Promise<void> => {
120
+ export const setupWebhook = async (webhookUrl: string): Promise<void> => {
104
121
  const { data: webhooks, error } = await listRepoWebhooks()
105
122
 
106
123
  if (error) {
@@ -112,7 +129,8 @@ export const setupWebhook = async (): Promise<void> => {
112
129
  setTimeout(setupWebhook, retryTimeout)
113
130
  return
114
131
  }
115
- logger.error("🚨 Failed fetching webhooks. Giving up.", error.message)
132
+ logger.error("🚨 Failed fetching webhooks. Giving up.")
133
+ logger.error(` ${error.message}`)
116
134
  return
117
135
  }
118
136
 
@@ -125,7 +143,10 @@ export const setupWebhook = async (): Promise<void> => {
125
143
  return
126
144
  }
127
145
 
128
- const { data: webhook, error: createError } = await createRepoWebhook()
146
+ await webhookIsSetup(webhookUrl)
147
+
148
+ const { data: webhook, error: createError } =
149
+ await createRepoWebhook(webhookUrl)
129
150
 
130
151
  if (createError) {
131
152
  if (retries < 5) {
@@ -136,9 +157,10 @@ export const setupWebhook = async (): Promise<void> => {
136
157
  setTimeout(setupWebhook, retryTimeout)
137
158
  return
138
159
  }
139
- logger.error("🚨 Failed creating webhook. Giving up.", createError.message)
160
+ logger.error("🚨 Failed creating webhook. Giving up.")
161
+ logger.error(` ${createError.message}`)
140
162
  return
141
163
  }
142
164
 
143
- logger.info("🪝 Webhook created: ", webhook.config.url)
165
+ logger.info("🪝 Webhook created: " + webhook.config.url)
144
166
  }
@@ -110,6 +110,18 @@ export const startWebappServer = async ({
110
110
  restartAddon()
111
111
  return { message: "Restarting addon..." }
112
112
  })
113
+ .get(
114
+ "/webhook",
115
+ async ({ query }) => {
116
+ if (query.check === "true") return { message: "Set up successfully" }
117
+ return { message: "Restarting addon..." }
118
+ },
119
+ {
120
+ query: t.Object({
121
+ check: t.Optional(t.Literal("true")),
122
+ }),
123
+ },
124
+ )
113
125
  .get("/addon-info", async () => {
114
126
  const { data, error } = await getAddonInfo()
115
127
 
@@ -138,7 +150,6 @@ export const startWebappServer = async ({
138
150
  }),
139
151
  response: t.Object({ logs: t.Array(t.String()) }),
140
152
  async open(ws) {
141
- console.log("😅😅😅 ~ ws.data.query.limit:", ws.data.query.limit)
142
153
  ws.send(await getLogsFromFile(ws.data.query.limit))
143
154
  logSubscribers.set(ws.id, async () => {
144
155
  ws.send(await getLogsFromFile(ws.data.query.limit))
@@ -226,7 +237,6 @@ export const startWebappServer = async ({
226
237
  export type WebServer = Awaited<ReturnType<typeof startWebappServer>>
227
238
 
228
239
  const getLogsFromFile = async (limit?: string) => {
229
- console.log("😅😅😅 ~ limit:", limit)
230
240
  try {
231
241
  const lines = (await Bun.file("./log.txt").text()).split("\n")
232
242
  const logFile = limit