Skip to content

Commit

Permalink
Got subprocesses with logging working
Browse files Browse the repository at this point in the history
  • Loading branch information
chadmmills committed Aug 3, 2023
1 parent 316fd48 commit a3fa92f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
40 changes: 31 additions & 9 deletions packages/scripts/dev.all.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
const DEV_CMDS = [
// { cmd: [ "bun", "--watch", "server/index.ts"], options: { cwd: "./api" } },
{ cmd: [ "bun", "--watch", "server/index.ts"], options: { stdout: "inherit" } },
type DevCmd = {
name: string,
cmd: string[],
options: {
cwd?: string,
stderr?: "inherit" | "pipe" | "ignore" ,
}
}

const DEV_CMDS: DevCmd[] = [
{ name: "API", cmd: [ "bun", "--watch", "server/index.ts"], options: { cwd: "../api" } },
{ name: "WEB", cmd: [ "npm", "run", "dev" ], options: { cwd: "../web" } },
]

const procs = DEV_CMDS.map(({ cmd, options }) => {
return Bun.spawn(cmd, options)
})
function consoleWriteForName(name: string) {
const consoleWrite = new WritableStream({
write(chunk) {
const buffer = new ArrayBuffer(1)
const view = new Uint8Array(buffer)
view[0] = chunk
const decoded = new TextDecoder("utf-8").decode(chunk)
console.log(name, ":", decoded.replace(/\n$/, ""))
}
})

for (const proc of procs) {
// console.log(proc.stdout.pipeTo(process.stdout))
return consoleWrite
}

console.log(DEV_CMDS)
const procs = DEV_CMDS.map(({ cmd, name, options }) => {
let spawnedProc = Bun.spawn(cmd, { ...options, stdout: "pipe" })
spawnedProc.stdout?.pipeTo(consoleWriteForName(name))

return spawnedProc
})

process.on("SIGINT", () => {
console.info("SIGINT signal received... quitting all processes")
procs.forEach(proc => proc.kill())
process.exit()
})

3 changes: 3 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "scripts",
"scripts": {
"dev:all": "bun dev.all.ts"
},
"devDependencies": {
"bun-types": "^0.7.0"
}
Expand Down

0 comments on commit a3fa92f

Please sign in to comment.