Skip to content

Commit

Permalink
Replace Deno.Command with dax
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed May 11, 2023
1 parent 6dbac5e commit 21fd1bd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
3 changes: 2 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ title: "11. Changelog"

All notable changes to this project will be documented in this section.

## [1.0.0-beta.23] - 2023-05-11
## [1.0.0-beta.23] - Unreleased

- Reduce `pup service install/uninstall/generate` to `pup install/uninstall [--dry-run]`
- Use dax to launch process, enabling shell functions and chaining of processes

## [1.0.0-beta.22] - 2023-05-10

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/basic/pup.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// One object per process ...
{
"id": "kept-alive-server", // Required
"cmd": ["deno", "run", "--allow-read", "--allow-env", "./server.js"], // Required
"cmd": ["echo ","server starting","&&","deno", "run", "--allow-read", "--allow-env", "./server.js"], // Required
"env": { // Default undefined
"TZ": "Europe/Berlin"
},
Expand Down
48 changes: 21 additions & 27 deletions lib/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { readLines, StringReader } from "../../deps.ts"

import { BaseRunner, RunnerCallback, RunnerResult } from "../types/runner.ts"

import $ from "https://deno.land/x/[email protected]/mod.ts";
import { CommandChild } from "https://deno.land/x/[email protected]/src/command.ts";

class Runner extends BaseRunner {
private process?: Deno.ChildProcess
private process?: CommandChild

constructor(pup: Pup, processConfig: ProcessConfiguration) {
super(pup, processConfig)
Expand Down Expand Up @@ -57,6 +60,7 @@ class Runner extends BaseRunner {
}

async run(runningCallback: RunnerCallback) {

if (!this.processConfig.cmd) {
throw new Error("No command specified")
}
Expand All @@ -76,42 +80,32 @@ class Runner extends BaseRunner {
}
}

// Start the process
const commander = new Deno.Command(
this.processConfig.cmd[0],
{
args: this.processConfig.cmd.slice(1),
cwd: this.processConfig.cwd,
env: env,
stdout: "piped",
stderr: "piped",
},
)

this.process = commander.spawn()
this.process.ref()
// Build command string
const commandString = this.processConfig.cmd.join(" ")

// Optimally, every item of this.processConfig.cmd should be escaped
let child = $.raw`${commandString}`.stdout("piped").stderr("piped")
if (this.processConfig.cwd) child = child.cwd(this.processConfig.cwd)
if (env) child = child.env(env)

// Process started, report pid to callback and file
runningCallback(this.process.pid)
// Spawn the process
this.process = child.spawn()

runningCallback(/*pid*/0)
this.writePidFile()
this.pipeToLogger("stdout", this.process.stdout())
this.pipeToLogger("stderr", this.process.stderr())
const result = await this.process

this.pipeToLogger("stdout", this.process.stdout)
this.pipeToLogger("stderr", this.process.stderr)

// Wait for process to stop and retrieve exit status
const result = await this.process.status

// Important! Close streams
// ToDo: Is it possible to ref the process?

// ... and clean up the pid file
this.removePidFile()

// Create a RunnerResult
const runnerResult: RunnerResult = {
code: result.code,
signal: result.signal,
success: result.success,
signal: null,
success: result.code === 0 ? true : false,
}

return runnerResult
Expand Down

0 comments on commit 21fd1bd

Please sign in to comment.