@@ -35,17 +35,11 @@ import Data.Posix.Signal (Signal)
35
35
import Data.Posix.Signal as Signal
36
36
import Effect (Effect )
37
37
import Effect.Exception (Error )
38
+ import Effect.Uncurried (EffectFn1 , EffectFn2 , runEffectFn1 , runEffectFn2 )
38
39
import Foreign.Object as FO
39
40
import Node.Platform (Platform )
40
41
import Node.Platform as Platform
41
42
import Node.Stream (Readable , Writable )
42
- import Unsafe.Coerce (unsafeCoerce )
43
-
44
- -- YOLO
45
- foreign import process :: forall props . { | props }
46
-
47
- mkEffect :: forall a . (Unit -> a ) -> Effect a
48
- mkEffect = unsafeCoerce
49
43
50
44
-- | Register a callback to be performed when the event loop empties, and
51
45
-- | Node.js is about to exit. Asynchronous calls can be made in the callback,
@@ -86,102 +80,101 @@ onSignal sig = onSignalImpl (Signal.toString sig)
86
80
-- | Register a callback to run as soon as the current event loop runs to
87
81
-- | completion.
88
82
nextTick :: Effect Unit -> Effect Unit
89
- nextTick callback = mkEffect \_ -> process.nextTick callback
83
+ nextTick cb = runEffectFn1 nextTickImpl cb
84
+
85
+ foreign import nextTickImpl :: EffectFn1 (Effect Unit ) (Unit )
90
86
91
- -- | Get an array containing the command line arguments.
92
- argv :: Effect (Array String )
93
- argv = copyArray process.argv
87
+ -- | Get a copy of the array containing the command line arguments.
88
+ foreign import argv :: Effect (Array String )
94
89
95
- -- | Node-specific options passed to the `node` executable.
96
- execArgv :: Effect (Array String )
97
- execArgv = copyArray process.execArgv
90
+ -- | Get a copy of the Node-specific options passed to the `node` executable.
91
+ foreign import execArgv :: Effect (Array String )
98
92
99
93
-- | The absolute pathname of the `node` executable that started the
100
94
-- | process.
101
- execPath :: Effect String
102
- execPath = mkEffect \_ -> process.execPath
95
+ foreign import execPath :: Effect (String )
103
96
104
97
-- | Change the current working directory of the process. If the current
105
98
-- | directory could not be changed, an exception will be thrown.
106
- foreign import chdir :: String -> Effect Unit
99
+ chdir :: String -> Effect Unit
100
+ chdir dir = runEffectFn1 chdirImpl dir
101
+
102
+ foreign import chdirImpl :: EffectFn1 (String ) (Unit )
107
103
108
104
-- | Get the current working directory of the process.
109
- cwd :: Effect String
110
- cwd = process.cwd
105
+ foreign import cwd :: Effect (String )
111
106
112
107
-- | Get a copy of the current environment.
113
- getEnv :: Effect (FO.Object String )
114
- getEnv = copyObject process.env
108
+ -- | If you only want to look up a value without paying
109
+ -- | for the overhead of the copy, use `lookupEnv`.
110
+ foreign import getEnv :: Effect (FO.Object String )
111
+
112
+ -- | Get the current environment object without copying it.
113
+ -- | Any mutations to the returned object
114
+ -- | or any mutations via `unsetEnv` and `setEnv`
115
+ -- | will affect all values that were obtained
116
+ -- | via this function.
117
+ -- | Thus, this is an internal function that is
118
+ -- | not exported.
119
+ foreign import unsafeGetEnv :: Effect (FO.Object String )
115
120
116
121
-- | Lookup a particular environment variable.
117
122
lookupEnv :: String -> Effect (Maybe String )
118
- lookupEnv k = lookupMutableObject k process.env
123
+ lookupEnv k = map ( FO .lookup k) $ unsafeGetEnv
119
124
120
125
-- | Set an environment variable.
121
- foreign import setEnv :: String -> String -> Effect Unit
126
+ setEnv :: String -> String -> Effect Unit
127
+ setEnv key value = runEffectFn2 setEnvImpl key value
128
+
129
+ foreign import setEnvImpl :: EffectFn2 (String ) (String ) (Unit )
122
130
123
131
-- | Delete an environment variable.
124
132
-- | Use case: to hide secret environment variable from child processes.
125
- foreign import unsetEnv :: String -> Effect Unit
133
+ unsetEnv :: String -> Effect Unit
134
+ unsetEnv key = runEffectFn1 unsetEnvImpl key
135
+
136
+ foreign import unsetEnvImpl :: EffectFn1 (String ) (Unit )
126
137
127
- pid :: Pid
128
- pid = process.pid
138
+ foreign import pid :: Pid
129
139
130
140
platform :: Maybe Platform
131
141
platform = Platform .fromString platformStr
132
142
133
- platformStr :: String
134
- platformStr = process.platform
143
+ foreign import platformStr :: String
135
144
136
145
-- | Cause the process to exit with the supplied integer code. An exit code
137
146
-- | of 0 is normally considered successful, and anything else is considered a
138
147
-- | failure.
139
- foreign import exit :: forall a . Int -> Effect a
148
+ exit :: forall a . Int -> Effect a
149
+ exit code = runEffectFn1 exitImpl code
150
+
151
+ foreign import exitImpl :: forall a . EffectFn1 (Int ) (a )
140
152
141
153
-- | The standard input stream. Note that this stream will never emit an `end`
142
154
-- | event, so any handlers attached via `onEnd` will never be called.
143
- stdin :: Readable ()
144
- stdin = process.stdin
155
+ foreign import stdin :: Readable ()
145
156
146
157
-- | The standard output stream. Note that this stream cannot be closed; calling
147
158
-- | `end` will result in an exception being thrown.
148
- stdout :: Writable ()
149
- stdout = process.stdout
159
+ foreign import stdout :: Writable ()
150
160
151
161
-- | The standard error stream. Note that this stream cannot be closed; calling
152
162
-- | `end` will result in an exception being thrown.
153
- stderr :: Writable ()
154
- stderr = process.stderr
163
+ foreign import stderr :: Writable ()
155
164
156
165
-- | Check whether the standard input stream appears to be attached to a TTY.
157
166
-- | It is a good idea to check this before processing the input data from stdin.
158
- stdinIsTTY :: Boolean
159
- stdinIsTTY = process.stdin.isTTY
167
+ foreign import stdinIsTTY :: Boolean
160
168
161
169
-- | Check whether the standard output stream appears to be attached to a TTY.
162
170
-- | It is a good idea to check this before printing ANSI codes to stdout
163
171
-- | (e.g. for coloured text in the terminal).
164
- stdoutIsTTY :: Boolean
165
- stdoutIsTTY = process.stdout.isTTY
172
+ foreign import stdoutIsTTY :: Boolean
166
173
167
174
-- | Check whether the standard error stream appears to be attached to a TTY.
168
175
-- | It is a good idea to check this before printing ANSI codes to stderr
169
176
-- | (e.g. for coloured text in the terminal).
170
- stderrIsTTY :: Boolean
171
- stderrIsTTY = process.stderr.isTTY
177
+ foreign import stderrIsTTY :: Boolean
172
178
173
179
-- | Get the Node.js version.
174
- version :: String
175
- version = process.version
176
-
177
- -- Utils
178
-
179
- foreign import data MutableArray :: Type -> Type
180
- foreign import data MutableObject :: Type -> Type
181
-
182
- foreign import copyArray :: forall a . MutableArray a -> Effect (Array a )
183
- foreign import copyObject :: forall a . MutableObject a -> Effect (FO.Object a )
184
-
185
- lookupMutableObject :: forall a . String -> MutableObject a -> Effect (Maybe a )
186
- lookupMutableObject k o =
187
- mkEffect \_ -> FO .lookup k (unsafeCoerce o)
180
+ foreign import version :: String
0 commit comments