Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to configure detached option for Node.js #132

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions library/process/api/process.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ open class io.matthewnelson.kmp.process.internal/events_EventEmitter { // io.mat
final fun removeAllListeners(kotlin/String): io.matthewnelson.kmp.process.internal/events_EventEmitter // io.matthewnelson.kmp.process.internal/events_EventEmitter.removeAllListeners|removeAllListeners(kotlin.String){}[0]
}

// Targets: [js]
final fun (io.matthewnelson.kmp.process/Process.Builder).io.matthewnelson.kmp.process/detached(kotlin/Boolean): io.matthewnelson.kmp.process/Process.Builder // io.matthewnelson.kmp.process/detached|detached@io.matthewnelson.kmp.process.Process.Builder(kotlin.Boolean){}[0]

// Targets: [js]
final fun (io.matthewnelson.kmp.process/Process.Builder).io.matthewnelson.kmp.process/shell(kotlin/Boolean): io.matthewnelson.kmp.process/Process.Builder // io.matthewnelson.kmp.process/shell|shell@io.matthewnelson.kmp.process.Process.Builder(kotlin.Boolean){}[0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@
**/
package io.matthewnelson.kmp.process

/**
* Configures the `detached` option for `spawn`.
*
* **NOTE:** `unref` will be called immediately on the underlying child process.
* It is upon API consumers to ensure an appropriate [Stdio] configuration is had
* for their detached [Process], as detailed in the documentation linked below.
*
* Default: `false`
*
* [docs#spawn](https://nodejs.org/api/child_process.html#optionsdetached)
* */
public fun Process.Builder.detached(
enable: Boolean,
): Process.Builder = apply {
platform().detached = enable
}

/**
* Configures the `shell` option for `spawn` and `spawnSync`
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import io.matthewnelson.kmp.process.*

internal class NodeJsProcess internal constructor(
private val jsProcess: child_process_ChildProcess,
private val isDetached: Boolean,
command: String,
args: List<String>,
chdir: File?,
Expand Down Expand Up @@ -52,6 +53,8 @@ internal class NodeJsProcess internal constructor(
val t = (err as? Throwable) ?: IOException("$err")
onError(t, lazyContext = { "nodejs.on('error')" })
}

if (isDetached) jsProcess.unref()
}

// @Throws(Throwable::class)
Expand Down Expand Up @@ -109,7 +112,7 @@ internal class NodeJsProcess internal constructor(
null
}

if (wasDestroyed) jsProcess.unref()
if (wasDestroyed && !isDetached) jsProcess.unref()

error?.let { throw it }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.khronos.webgl.Int8Array
// jsMain
internal actual class PlatformBuilder private actual constructor() {

internal var detached: Boolean = false
// String or Boolean
internal var shell: Any = false
internal var windowsVerbatimArguments = false
Expand Down Expand Up @@ -150,12 +151,13 @@ internal actual class PlatformBuilder private actual constructor() {
): Process {
val jsEnv = env.toJsEnv()
val jsStdio = stdio.toJsStdio()
val isDetached = detached

val opts = js("{}")
chdir?.let { opts["cwd"] = it.path }
opts["env"] = jsEnv
opts["stdio"] = jsStdio
opts["detached"] = false
opts["detached"] = isDetached
opts["shell"] = shell
opts["windowsVerbatimArguments"] = windowsVerbatimArguments
opts["windowsHide"] = windowsHide
Expand All @@ -167,6 +169,7 @@ internal actual class PlatformBuilder private actual constructor() {

return NodeJsProcess(
jsProcess,
isDetached,
command,
args,
chdir,
Expand Down
Loading