Compare commits
No commits in common. "735e80748872cadcf690e5a162f32a274f566aab" and "25f50b1100d9d00403349dd169a72c175c310249" have entirely different histories.
735e807488
...
25f50b1100
13
help.txt
13
help.txt
|
@ -1,13 +0,0 @@
|
|||
Ludus CLI (v0.1.0)
|
||||
|
||||
The Ludus CLI has five modes: `help`, `version`, `run`, `listen`, and `send`.
|
||||
|
||||
* `help`: Output this text and exit.
|
||||
|
||||
* `version`: Ouput the current version number.
|
||||
|
||||
* `run`: Runs a Ludus script: `ludus run {script}`.
|
||||
|
||||
* `listen`: Starts a Ludus "listener" server, which receives Ludus source code on a given port and outputs the results. Similar to a REPL, intended to be used in conjunction with `send`, from a text editor.
|
||||
|
||||
* `send`: Sends text from `stdin` to a listening server. Any text editor that can send highlighted text to a command line program can evaluate Ludus source.
|
19
index.ts
19
index.ts
|
@ -1,19 +0,0 @@
|
|||
import { listen } from "./listener"
|
||||
import { run_file } from "./runner"
|
||||
import { send } from "./sender"
|
||||
|
||||
const help_text = await Bun.file("./help.txt").text()
|
||||
|
||||
export async function main() {
|
||||
const cmd = Bun.argv[2]
|
||||
switch (cmd) {
|
||||
case "listen": { return listen() }
|
||||
case "send": { return send() }
|
||||
case "run": { return run_file(Bun.argv[3]) }
|
||||
case "help": { return console.log(help_text) }
|
||||
case "version": { return console.log("0.1.0") }
|
||||
default: console.log("Usage: ludus {help | version | run | listen | send}")
|
||||
}
|
||||
}
|
||||
|
||||
await main()
|
94
listener.ts
94
listener.ts
|
@ -1,73 +1,35 @@
|
|||
import { run } from "@ludus/ludus-js-pure"
|
||||
import { unlinkSync, readFileSync } from "node:fs"
|
||||
|
||||
export async function listen () {
|
||||
const port = Math.floor((Math.random() * 1000) + 50000)
|
||||
const session_token = crypto.randomUUID();
|
||||
const repl_file_path = ".lrepl"
|
||||
const repl_info = { port, session: session_token }
|
||||
const repl_file = Bun.file(repl_file_path)
|
||||
await Bun.write(repl_file, JSON.stringify(repl_info))
|
||||
const port = Math.floor((Math.random() * 1000) + 50000)
|
||||
|
||||
console.log(`Ludus REPL listening on localhost:${port}`)
|
||||
console.log(`Session token: ${session_token}`)
|
||||
const repl_info = { port }
|
||||
|
||||
Bun.listen({
|
||||
hostname: "localhost",
|
||||
port,
|
||||
socket: {
|
||||
data: (_, data) => {
|
||||
const payload = data.toString("utf-8")
|
||||
let session, source
|
||||
try {
|
||||
const json_msg = JSON.parse(payload)
|
||||
session = json_msg.session
|
||||
source = json_msg.source
|
||||
} catch (e) {
|
||||
console.log("Received malformed message.")
|
||||
return
|
||||
}
|
||||
if (session !== session_token) {
|
||||
console.log("Received message not from this REPL session.")
|
||||
return
|
||||
}
|
||||
const result = run(source)
|
||||
if (!result.errors) console.log("=>", result.result)
|
||||
else {
|
||||
console.log("Ludus had some errors:")
|
||||
console.log(result.errors)
|
||||
}
|
||||
},
|
||||
error: (socket, error) => { console.log(error); socket.end() }
|
||||
}
|
||||
})
|
||||
const repl_file = Bun.file("./.lrepl")
|
||||
|
||||
await Bun.write(repl_file, JSON.stringify(repl_info))
|
||||
|
||||
console.log(`Ludus REPL listening on localhost:${port}`)
|
||||
|
||||
Bun.listen({
|
||||
hostname: "localhost",
|
||||
port,
|
||||
socket: {
|
||||
data: (socket, data) => {
|
||||
const source = data.toString("utf-8")
|
||||
const result = run(source)
|
||||
const msgs = result.console
|
||||
for (const msg of msgs) {
|
||||
console.log(msg)
|
||||
}
|
||||
if (!result.errors) console.log(result.result)
|
||||
else {
|
||||
console.log("Ludus had some errors:")
|
||||
console.log(result.errors)
|
||||
}
|
||||
},
|
||||
error: (socket, error) => { console.log(error); socket.end() }
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
process.on("exit", async function () {
|
||||
try { cleanup(repl_file_path, session_token) } catch (e) {}
|
||||
console.log("\nGoodbye.")
|
||||
})
|
||||
|
||||
process.on("SIGINT", () => process.exit())
|
||||
|
||||
// Below here, I'm trying to get ctrl-d to work to close the listener
|
||||
// the while (true) loop locks the listener
|
||||
// see: https://github.com/oven-sh/bun/issues/3255
|
||||
// this should be doable with process.stdin.on("close"...)
|
||||
//const mystdin = Bun.file("/dev/stdin").stream().getReader()
|
||||
|
||||
// while (true) {
|
||||
// const {done, value} = await mystdin.read()
|
||||
// if (done) process.exit()
|
||||
// console.log(value)
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
// Don't delete an .lrepl we don't own
|
||||
function cleanup (path: string, session_token: string) {
|
||||
const repl_file = readFileSync(path).toString("utf-8")
|
||||
const {session} = JSON.parse(repl_file)
|
||||
if (session === session_token) unlinkSync(path)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
{
|
||||
"name": "@ludus/cli",
|
||||
"version": "0.1.0",
|
||||
"description": "A CLI interface to Ludus.",
|
||||
"main": "index.ts",
|
||||
"name": "ludus-repl",
|
||||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"license": "GPL-3.0",
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest"
|
||||
},
|
||||
|
|
29
runner.ts
29
runner.ts
|
@ -1,20 +1,13 @@
|
|||
import { run } from "@ludus/ludus-js-pure"
|
||||
import {run} from "@ludus/ludus-js-pure"
|
||||
|
||||
export async function run_file (path: string): Promise<void> {
|
||||
const handle = Bun.file(path)
|
||||
const file_exists = await handle.exists()
|
||||
if (!file_exists) {
|
||||
console.log(`File not found: ${path}`)
|
||||
return
|
||||
}
|
||||
const source = await handle.text()
|
||||
const output = run(source)
|
||||
const script = Bun.argv[2]
|
||||
const file = Bun.file(script)
|
||||
const source = await file.text()
|
||||
console.log(`Source to run:
|
||||
${source}`)
|
||||
|
||||
const result = run(source)
|
||||
|
||||
if (!result.errors) console.log(result.result)
|
||||
else console.log(result.errors)
|
||||
|
||||
if (output.errors) {
|
||||
for (const error of output.errors) {
|
||||
console.log(error)
|
||||
}
|
||||
} else {
|
||||
console.log(output.result)
|
||||
}
|
||||
}
|
||||
|
|
42
sender.ts
42
sender.ts
|
@ -1,31 +1,19 @@
|
|||
export async function send() {
|
||||
const repl_file = Bun.file("./.lrepl")
|
||||
let repl_info
|
||||
try {
|
||||
repl_info = await repl_file.json()
|
||||
} catch (e) {
|
||||
console.log("No .lrepl file found.")
|
||||
process.exit(1)
|
||||
}
|
||||
const port = repl_info.port
|
||||
const session = repl_info.session
|
||||
const repl_file = Bun.file("./.lrepl")
|
||||
const repl_info = await repl_file.json()
|
||||
const port = repl_info.port
|
||||
|
||||
const socket = await Bun.connect({
|
||||
hostname: "localhost",
|
||||
port,
|
||||
socket: {
|
||||
data: () => process.exit()
|
||||
}
|
||||
})
|
||||
|
||||
for await (const input of Bun.stdin.stream()) {
|
||||
const chunk = Buffer.from(input).toString("utf-8")
|
||||
const payload = {source: chunk, session}
|
||||
// console.log("Sending:", payload)
|
||||
// console.log(`To: ${port}`)
|
||||
socket.write(JSON.stringify(payload))
|
||||
socket.end()
|
||||
process.exit()
|
||||
const socket = await Bun.connect({
|
||||
hostname: "localhost",
|
||||
port,
|
||||
socket: {
|
||||
data: (_, data) => process.exit()
|
||||
}
|
||||
})
|
||||
|
||||
for await (const input of Bun.stdin.stream()) {
|
||||
const chunk = Buffer.from(input).toString("utf-8")
|
||||
socket.write(chunk)
|
||||
socket.end()
|
||||
process.exit()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user