Skip to content

Commit

Permalink
fix: watch process sigterm
Browse files Browse the repository at this point in the history
  • Loading branch information
chase-moskal committed Jul 8, 2024
1 parent 934306b commit 409f15b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
6 changes: 4 additions & 2 deletions s/build/procedures/watches/turtle-build-watch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

import {$} from "zx"
import {OnDeath} from "@benev/argv"
import {BuildInputs} from "../../parts/stdparams.js"

export async function turtleBuildWatch({}: BuildInputs) {
$.spawn("tsc", ["-w"], {stdio: "inherit"})
export async function turtleBuildWatch({}: BuildInputs, onDeath: OnDeath) {
const process = $.spawn("tsc", ["-w"], {stdio: "inherit"})
onDeath(() => process.kill())
}

6 changes: 4 additions & 2 deletions s/build/procedures/watches/turtle-ssg-watch.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@

import {OnDeath} from "@benev/argv"

import {turtleCopy} from "../turtle-copy.js"
import {watch} from "../../../utils/watch.js"
import {turtlePages} from "../turtle-pages.js"
import {SsgInputs} from "../../parts/stdparams.js"
import {stdignore} from "../../parts/stdignore.js"
import {turtleScripts} from "../turtle-scripts.js"

export async function turtleSsgWatch(o: SsgInputs) {
export async function turtleSsgWatch(o: SsgInputs, onDeath: OnDeath) {
const {params} = o

const ignored = stdignore([])
const dedupedDirs = [...new Set([...params.in, params.out])]
const patterns = dedupedDirs
.map(dir => `${dir}/**/*.{js,json,css}`)

await watch(patterns, ignored, async() => {
await watch(patterns, ignored, onDeath, async() => {
await turtleCopy(o)
await turtleScripts(o)
await turtlePages(o)
Expand Down
12 changes: 7 additions & 5 deletions s/turtle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import {$} from "zx"
import {cli, command} from "@benev/argv"
import {cli, command, deathWithDignity} from "@benev/argv"
import {handleZxErrors} from "./errors/handle-zx-errors.js"
import {turtleCopy} from "./build/procedures/turtle-copy.js"
import {turtlePages} from "./build/procedures/turtle-pages.js"
Expand All @@ -11,6 +11,8 @@ import {turtleScripts} from "./build/procedures/turtle-scripts.js"
import {turtleSsgWatch} from "./build/procedures/watches/turtle-ssg-watch.js"
import {turtleBuildWatch} from "./build/procedures/watches/turtle-build-watch.js"

const {onDeath} = deathWithDignity()

await cli(process.argv, {
name: "turtle",
help: `🐢 static site generator.`,
Expand Down Expand Up @@ -54,8 +56,8 @@ await cli(process.argv, {
params: ssgparams,
async execute(o) {
await Promise.all([
turtleBuildWatch(o),
turtleSsgWatch(o),
turtleBuildWatch(o, onDeath),
turtleSsgWatch(o, onDeath),
])
},
}),
Expand All @@ -64,15 +66,15 @@ await cli(process.argv, {
args: [],
params: buildparams,
async execute(o) {
await turtleBuildWatch(o)
await turtleBuildWatch(o, onDeath)
},
}),

"ssg-watch": command({
args: [],
params: ssgparams,
async execute(o) {
await turtleSsgWatch(o)
await turtleSsgWatch(o, onDeath)
},
}),

Expand Down
6 changes: 5 additions & 1 deletion s/utils/watch.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@

import chokidar from "chokidar"
import {debounce} from "./debounce/debounce.js"
import { OnDeath } from "@benev/argv"

export async function watch(
patterns: string[],
ignored: string[],
onDeath: OnDeath,
fn: () => Promise<void>,
) {

let busy = false

chokidar.watch(patterns, {ignored})
const watcher = chokidar.watch(patterns, {ignored})
.on("all", debounce(500, async() => {
if (!busy) {
busy = true
await fn()
busy = false
}
}))

onDeath(() => watcher.close())
}

0 comments on commit 409f15b

Please sign in to comment.