@typed-assistant/builder 0.0.54 → 0.0.55

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.54",
3
+ "version": "0.0.55",
4
4
  "exports": {
5
5
  "./appProcess": "./src/appProcess.tsx",
6
6
  "./bunInstall": "./src/bunInstall.tsx",
@@ -26,9 +26,9 @@
26
26
  "ts-toolbelt": "^9.6.0",
27
27
  "typescript": "^5.3.3",
28
28
  "@typed-assistant/eslint-config": "0.0.9",
29
+ "@typed-assistant/utils": "0.0.15",
29
30
  "@typed-assistant/logger": "0.0.17",
30
- "@typed-assistant/typescript-config": "0.0.9",
31
- "@typed-assistant/utils": "0.0.15"
31
+ "@typed-assistant/typescript-config": "0.0.9"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "home-assistant-js-websocket": "^8.2.0"
@@ -1,3 +1,4 @@
1
+ import type { LogSchema } from "@typed-assistant/logger"
1
2
  import { logger } from "@typed-assistant/logger"
2
3
  import Convert from "ansi-to-html"
3
4
  import type { Subprocess } from "bun"
@@ -9,6 +10,7 @@ import type { List, String } from "ts-toolbelt"
9
10
  import { getAddonInfo } from "./getAddonInfo"
10
11
  import { addKillListener, killSubprocess } from "./killProcess"
11
12
  import { restartAddon } from "./restartAddon"
13
+ import { levels } from "@typed-assistant/logger/levels"
12
14
 
13
15
  const indexHtmlFilePath = `${import.meta.dir}/webserver/index.html` as const
14
16
  const cssFile = `${import.meta.dir}/webserver/input.css` as const
@@ -142,7 +144,7 @@ export const startWebappServer = async ({
142
144
  .get(
143
145
  "/log.txt",
144
146
  async ({ query }) => {
145
- return getLogsFromFile(query.limit)
147
+ return getLogsFromFile("trace", query.limit)
146
148
  },
147
149
  {
148
150
  query: t.Object({
@@ -153,12 +155,32 @@ export const startWebappServer = async ({
153
155
  .ws("/logsws", {
154
156
  query: t.Object({
155
157
  limit: t.Optional(t.String()),
158
+ level: t.Union([
159
+ t.Literal("trace"),
160
+ t.Literal("debug"),
161
+ t.Literal("info"),
162
+ t.Literal("warn"),
163
+ t.Literal("error"),
164
+ t.Literal("fatal"),
165
+ ]),
166
+ }),
167
+ response: t.Object({
168
+ logs: t.Array(
169
+ t.Object({
170
+ level: t.Number(),
171
+ time: t.Number(),
172
+ pid: t.Number(),
173
+ hostname: t.String(),
174
+ msg: t.String(),
175
+ }),
176
+ ),
156
177
  }),
157
- response: t.Object({ logs: t.Array(t.String()) }),
158
178
  async open(ws) {
159
- ws.send(await getLogsFromFile(ws.data.query.limit))
179
+ ws.send(await getLogsFromFile(ws.data.query.level, ws.data.query.limit))
160
180
  logSubscribers.set(ws.id, async () => {
161
- ws.send(await getLogsFromFile(ws.data.query.limit))
181
+ ws.send(
182
+ await getLogsFromFile(ws.data.query.level, ws.data.query.limit),
183
+ )
162
184
  })
163
185
  },
164
186
  close(ws) {
@@ -242,15 +264,36 @@ export const startWebappServer = async ({
242
264
 
243
265
  export type WebServer = Awaited<ReturnType<typeof startWebappServer>>
244
266
 
245
- const getLogsFromFile = async (limit?: string) => {
267
+ const getLogsFromFile = async (level: keyof typeof levels, limit?: string) => {
246
268
  try {
247
- const lines = (await Bun.file("./log.txt").text()).split("\n")
269
+ const lines = (await Bun.file("./log.txt").text())
270
+ .split("\n")
271
+ .map(
272
+ (line) =>
273
+ (line
274
+ ? JSON.parse(line)
275
+ : { msg: "Empty line", level: levels.fatal }) as LogSchema,
276
+ )
277
+ .filter((log) => log.level >= levels[level])
278
+
248
279
  const logFile = limit
249
280
  ? lines.slice(lines.length - 1 - Number(limit), lines.length - 1)
250
281
  : lines
282
+
251
283
  return { logs: logFile }
252
284
  } catch (e) {
253
- return { logs: ["Error reading log.txt file"] }
285
+ return {
286
+ logs: [
287
+ {
288
+ msg: "Error reading log.txt file",
289
+ level: levels.fatal,
290
+ },
291
+ {
292
+ msg: e instanceof Error ? e.message : e,
293
+ level: levels.fatal,
294
+ },
295
+ ] as LogSchema[],
296
+ }
254
297
  }
255
298
  }
256
299
 
@@ -21,15 +21,13 @@ export const Logs = ({ basePath }: { basePath: string }) => {
21
21
 
22
22
  const ws = useWS({
23
23
  subscribe: useCallback(
24
- () => app.logsws.subscribe({ $query: { limit: limit.toString() } }),
25
- [limit],
24
+ () =>
25
+ app.logsws.subscribe({ $query: { level, limit: limit.toString() } }),
26
+ [level, limit],
26
27
  ),
27
28
  onMessage: useCallback((event) => {
28
- setLogs(
29
- (JSON.parse(event.data).logs as string[]).map((log: string) =>
30
- JSON.parse(log),
31
- ),
32
- )
29
+ console.log("😅😅😅 ~ event:", event)
30
+ setLogs((JSON.parse(event.data) as { logs: LogSchema[] }).logs)
33
31
  }, []),
34
32
  })
35
33
 
@@ -11,7 +11,7 @@ export function useWS({
11
11
  const [ws, setWS] = useState(subscribe)
12
12
 
13
13
  useEffect(() => {
14
- let timeout: NodeJS.Timeout
14
+ let timeout: ReturnType<typeof setTimeout>
15
15
 
16
16
  ws.ws.onclose = function () {
17
17
  timeout = setTimeout(() => {