Skip to content

Commit 320afb1

Browse files
Add pidExists (#53)
* Add pidExists
1 parent d9ad6d0 commit 320afb1

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Breaking changes:
2424

2525
New features:
2626
- Added `fromKillSignal` (#51 by @JordanMartinez)
27+
- Added `pidExists` (#52 by @JordanMartinez)
2728

2829
Other improvements:
2930
- Fix regression: add `ref`/`unref` APIs that were dropped in `v10.0.0` (#50 by @JordanMartinez)

src/Node/ChildProcess.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module Node.ChildProcess
4444
, stdout
4545
, stderr
4646
, pid
47+
, pidExists
4748
, connected
4849
, disconnect
4950
, exitCode
@@ -158,6 +159,9 @@ stderr = toUnsafeChildProcess >>> UnsafeCP.unsafeStderr >>> unsafeFromNull
158159
pid :: ChildProcess -> Effect (Maybe Pid)
159160
pid = unsafeCoerce SafeCP.pid
160161

162+
pidExists :: ChildProcess -> Effect Boolean
163+
pidExists = unsafeCoerce SafeCP.pidExists
164+
161165
-- | Indicates whether it is still possible to send and receive
162166
-- | messages from the child process.
163167
connected :: ChildProcess -> Effect Boolean

src/Node/UnsafeChildProcess/Safe.purs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Node.UnsafeChildProcess.Safe
1010
, messageH
1111
, spawnH
1212
, pid
13+
, pidExists
1314
, connected
1415
, disconnect
1516
, exitCode
@@ -36,7 +37,7 @@ import Data.Posix.Signal as Signal
3637
import Effect (Effect)
3738
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2)
3839
import Foreign (Foreign)
39-
import Node.ChildProcess.Types (Exit(..), Handle, KillSignal, StdIO, UnsafeChildProcess, ipc, pipe, stringSignal)
40+
import Node.ChildProcess.Types (Exit(..), Handle, KillSignal, StdIO, UnsafeChildProcess, intSignal, ipc, pipe, stringSignal)
4041
import Node.Errors.SystemError (SystemError)
4142
import Node.EventEmitter (EventEmitter, EventHandle(..))
4243
import Node.EventEmitter.UtilTypes (EventHandle0, EventHandle1)
@@ -79,6 +80,11 @@ pid cp = map toMaybe $ runEffectFn1 pidImpl cp
7980

8081
foreign import pidImpl :: EffectFn1 (UnsafeChildProcess) (Nullable Pid)
8182

83+
-- | Note: this will not work if the user does not have permission to kill
84+
-- | a `PID`. Uses `cp.kill(0)` underneath.
85+
pidExists :: UnsafeChildProcess -> Effect Boolean
86+
pidExists cp = kill' (intSignal 0) cp
87+
8288
-- | Indicates whether it is still possible to send and receive
8389
-- | messages from the child process.
8490
connected :: UnsafeChildProcess -> Effect Boolean

0 commit comments

Comments
 (0)