Skip to content

Commit

Permalink
feat(debugger): merge with developer-settings plugin, improve serve…
Browse files Browse the repository at this point in the history
…r logging
  • Loading branch information
PalmDevs committed Dec 31, 2024
1 parent 7b32d54 commit b7729c0
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 398 deletions.
Binary file modified bun.lockb
Binary file not shown.
49 changes: 29 additions & 20 deletions scripts/debugger.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env node
import * as repl from 'node:repl'
import { WebSocketServer } from 'ws'
import os from 'os'
import chalk from 'chalk'
import clipboardy from 'clipboardy'
import { join, resolve } from 'node:path'
import { existsSync } from 'node:fs'
import { mkdir, writeFile } from 'node:fs/promises'
import { join, resolve } from 'path'
import { existsSync } from 'fs'
import { mkdir, writeFile } from 'fs/promises'

const debuggerHistoryPath = resolve(join('node_modules', 'debugger'))

Expand All @@ -32,12 +33,12 @@ const logAsClient = message => console.info(clientColorify(null, message))
const logAsClientWarn = message => console.warn(clientColorify('warn', message))
const logAsClientError = message => console.error(clientColorify('error', message))

const copyPrompt = ' --copy'
const clearHistoryPrompt = '--ch'
const copyPrompt = '--copy'
const clearHistoryPrompt = '--clear'

export function serve() {
let websocketOpen = false
let awaitingReply
let nextReply

const wss = new WebSocketServer({
port: 9090,
Expand All @@ -46,25 +47,25 @@ export function serve() {
if (websocketOpen) return
websocketOpen = true

logAsDebugger('Starting debugger session')
logAsDebugger('Client connected')

ws.on('message', data => {
try {
/** @type {{ level: "info" | "warn" | "error", message: string, nonce?: string }} */
const json = JSON.parse(data.toString())

if (awaitingReply?.cb && awaitingReply?.nonce && awaitingReply.nonce === json.nonce) {
if (json.level === 'info' && awaitingReply.toCopy) {
if (nextReply?.cb && nextReply?.nonce && nextReply.nonce === json.nonce) {
if (json.level === 'info' && nextReply.toCopy) {
clipboardy.write(json.message)
awaitingReply.cb(null, debuggerColorify('Copied result to clipboard'))
nextReply.cb(null, debuggerColorify('Copied result to clipboard'))
} else
awaitingReply.cb(
nextReply.cb(
null,
json.level === 'error'
? clientColorify('error', json.message)
: clientColorify(null, json.message),
)
awaitingReply = null
nextReply = null
isPrompting = true
} else {
if (json.level === 'error') logAsClientError(json.message)
Expand All @@ -88,19 +89,19 @@ export function serve() {
const code = input.trim()
if (code === clearHistoryPrompt) {
writeFile(join(debuggerHistoryPath, 'history.txt'), '')
logAsDebugger('Cleared repl history')
logAsDebugger('Cleared REPL history')
return cb()
}

awaitingReply = {
nextReply = {
nonce: crypto.randomUUID(),
cb,
toCopy: code.endsWith(copyPrompt),
}
ws.send(
JSON.stringify({
code: code.endsWith(copyPrompt) ? code.slice(0, -copyPrompt.length) : code,
nonce: awaitingReply.nonce,
nonce: nextReply.nonce,
}),
)
} catch (e) {
Expand All @@ -116,19 +117,27 @@ export function serve() {
rl.on('close', () => {
isPrompting = false
ws.close()
logAsDebugger('Closing debugger, press Ctrl+C to exit')
})

ws.on('close', () => {
logAsDebugger('Websocket was closed')
logAsDebugger('Client disconnected')
rl.close()
websocketOpen = false
})
})

logAsDebugger('Debugger ready at :9090')
logAsDebugger(`Add${chalk.bold(copyPrompt)} to your prompt to copy the result to clipboard`)
logAsDebugger(`Type ${chalk.bold(clearHistoryPrompt)} to clear your repl history `)
console.info(chalk.red('\nDebugger ready, available on:\n'))
const netInterfaces = os.networkInterfaces()
for (const netinterfaces of Object.values(netInterfaces)) {
for (const details of netinterfaces || []) {
if (details.family !== 'IPv4') continue
const port = chalk.yellowBright(wss.address()?.port.toString())
console.info(` ${chalk.gray('http://')}${details.address}:${port}`)
}
}

console.log(chalk.gray.underline(`\nRun with ${chalk.bold.white(copyPrompt)} to your prompt to copy the result to clipboard`))
console.log(chalk.gray.underline(`Run with ${chalk.bold.white(clearHistoryPrompt)} to clear your REPL history\n`))

return wss
}
Expand Down
115 changes: 0 additions & 115 deletions src/plugins/debugger/index.tsx

This file was deleted.

95 changes: 0 additions & 95 deletions src/plugins/debugger/pages/Debugger.tsx

This file was deleted.

14 changes: 14 additions & 0 deletions src/plugins/developer-settings/debugger.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare global {
var dbgr: {
reload(): void
patcher: {
// biome-ignore lint/suspicious/noExplicitAny: The object can be anything
snipe(object: any, key: string, callback?: (args: unknown) => void): void
// biome-ignore lint/suspicious/noExplicitAny: The object can be anything
noop(object: any, key: string): void
wipe(): void
}
} | undefined
}

export {}
Loading

0 comments on commit b7729c0

Please sign in to comment.