Skip to content

Commit 7935fad

Browse files
committed
Initial commit
0 parents  commit 7935fad

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/.psci*
6+
/src/.webpack.js

bower.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "purescript-node-process",
3+
"moduleType": [
4+
"node"
5+
],
6+
"ignore": [
7+
"**/.*",
8+
"node_modules",
9+
"bower_components",
10+
"output"
11+
],
12+
"dependencies": {
13+
"purescript-console": "^0.1.0",
14+
"purescript-maps": "~0.5.4",
15+
"purescript-node-streams": "~0.3.0",
16+
"purescript-maybe": "~0.3.5",
17+
"purescript-unsafe-coerce": "~0.1.0",
18+
"purescript-exceptions": "~0.3.3"
19+
}
20+
}

src/Node/Process.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
// module Node.Process
3+
4+
exports.globalProcessObject = process;
5+
6+
exports.readMutableProperty = function(propName) {
7+
return function() {
8+
return process[propName];
9+
}
10+
}

src/Node/Process.purs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
-- | Bindings to the global `process` object in Node.js. See also [the Node API documentation](https://nodejs.org/api/process.html)
2+
module Node.Process
3+
( PROCESS()
4+
, onBeforeExit
5+
, onExit
6+
, onSignal
7+
, argv
8+
, execArgv
9+
, chdir
10+
, cwd
11+
, getEnv
12+
, lookupEnv
13+
, setEnv
14+
, pid
15+
, platform
16+
, exit
17+
, stdin
18+
, stdout
19+
, stderr
20+
, stdoutIsTTY
21+
, version
22+
) where
23+
24+
import Prelude
25+
import Control.Monad.Eff
26+
import Control.Monad.Eff.Console (CONSOLE())
27+
import Control.Monad.Eff.Exception (EXCEPTION())
28+
import Data.Maybe (Maybe())
29+
import Data.StrMap (StrMap())
30+
import Data.StrMap as StrMap
31+
import Node.Stream (Readable(), Writable())
32+
import Unsafe.Coerce (unsafeCoerce)
33+
34+
-- | An effect tracking interaction with the global `process` object.
35+
foreign import data PROCESS :: !
36+
37+
-- YOLO
38+
foreign import globalProcessObject :: forall props. { | props }
39+
40+
-- | Very unsafe. Be careful. Reads a mutable property off the global `process`
41+
-- | object.
42+
foreign import readMutableProperty :: forall eff a. String -> Eff eff a
43+
44+
-- | Read an immutable property off the global `process` object.
45+
readImmutableProperty :: forall a. String -> a
46+
readImmutableProperty = unsafeCoerce <<< readMutableProperty
47+
48+
-- | Register a callback to be performed when the event loop empties, and
49+
-- | Node.js is about to exit. Asynchronous calls can be made in the callback,
50+
-- | and if any are made, it will cause the process to continue a little longer.
51+
foreign import onBeforeExit :: forall eff. Eff (process :: PROCESS | eff) Unit -> Eff (process :: PROCESS | eff) Unit
52+
53+
-- | Register a callback to be performed when the process is about to exit.
54+
-- | Any work scheduled via asynchronous calls made here will not be performed
55+
-- | in time.
56+
-- |
57+
-- | The argument to the callback is the exit code which the process is about
58+
-- | to exit with.
59+
foreign import onExit :: forall eff. (Int -> Eff (process :: PROCESS | eff) Unit) -> Eff (process :: PROCESS | eff) Unit
60+
61+
-- | Install a handler for a particular signal.
62+
foreign import onSignal :: forall eff. String -> Eff (process :: PROCESS | eff) Unit -> Eff (process :: PROCESS | eff) Unit
63+
64+
-- | Get an array containing the command line arguments. Be aware
65+
-- | that this can change over the course of the program.
66+
argv :: forall eff. Eff (process :: PROCESS | eff) (Array String)
67+
argv = readMutableProperty "argv"
68+
69+
-- | Node-specific options passed to the `node` executable. Be aware that
70+
-- | this can change over the course of the program.
71+
execArgv :: forall eff. Eff (process :: PROCESS | eff) (Array String)
72+
execArgv = readMutableProperty "execArgv"
73+
74+
-- | The absolute pathname of the `node` executable that started the
75+
-- | process.
76+
execPath :: forall eff. Eff (process :: PROCESS | eff) String
77+
execPath = readMutableProperty "execPath"
78+
79+
-- | Change the current working directory of the process. If the current
80+
-- | directory could not be changed, an exception will be thrown.
81+
foreign import chdir :: forall eff. String -> Eff (err :: EXCEPTION, process :: PROCESS | eff) Unit
82+
83+
-- | Get the current working directory of the process.
84+
foreign import cwd :: forall eff. Eff (process :: PROCESS | eff) String
85+
86+
-- | Get a copy of the current environment.
87+
getEnv :: forall eff. Eff (process :: PROCESS | eff) (StrMap String)
88+
getEnv = readMutableProperty "env"
89+
90+
-- | Lookup a particular environment variable.
91+
lookupEnv :: forall eff. String -> Eff (process :: PROCESS | eff) (Maybe String)
92+
lookupEnv k = StrMap.lookup k <$> getEnv
93+
94+
-- | Set an environment variable.
95+
foreign import setEnv :: forall eff. String -> String -> Eff (process :: PROCESS | eff) Unit
96+
97+
pid :: Int
98+
pid = readImmutableProperty "pid"
99+
100+
platform :: String
101+
platform = readImmutableProperty "platform"
102+
103+
-- | Cause the process to exit with the supplied integer code. An exit code
104+
-- | of 0 is normally considered successful, and anything else is considered a
105+
-- | failure.
106+
foreign import exit :: forall eff. Int -> Eff (process :: PROCESS | eff) Unit
107+
108+
stdin :: forall eff. Readable () (console :: CONSOLE | eff)
109+
stdin = readImmutableProperty "stdin"
110+
111+
stdout :: forall eff. Writable () (console :: CONSOLE | eff)
112+
stdout = readImmutableProperty "stdout"
113+
114+
stderr :: forall eff. Writable () (console :: CONSOLE | eff)
115+
stderr = readImmutableProperty "stderr"
116+
117+
-- | Check whether the process is being run inside a TTY context
118+
foreign import stdoutIsTTY :: forall eff. Eff (process :: PROCESS | eff) Boolean
119+
120+
-- | Get the Node.js version.
121+
version :: String
122+
version = readImmutableProperty "version"

0 commit comments

Comments
 (0)