Skip to content

Add mutable references for argv, execArgv and env #25

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 3 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
"output"
],
"dependencies": {
"purescript-arrays": "master",
"purescript-effect": "master",
"purescript-foreign-object": "master",
"purescript-maybe": "master",
"purescript-node-streams": "master",
"purescript-posix-types": "master",
"purescript-unsafe-coerce": "master",
"purescript-prelude": "master"
"purescript-prelude": "master",
"purescript-st": "master"
}
}
14 changes: 0 additions & 14 deletions src/Node/Process.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,6 @@ exports.chdir = function (dir) {
};
};

exports.setEnv = function (var_) {
return function (val) {
return function () {
process.env[var_] = val;
};
};
};

exports.unsetEnv = function (var_) {
return function () {
delete process.env[var_];
};
};

exports.exit = function (code) {
return function () {
process.exit(code);
Expand Down
42 changes: 32 additions & 10 deletions src/Node/Process.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ module Node.Process
, onUncaughtException
, onUnhandledRejection
, argv
, argv'
, execArgv
, execArgv'
, execPath
, chdir
, cwd
, getEnv
, env
, lookupEnv
, setEnv
, unsetEnv
Expand All @@ -27,13 +30,19 @@ module Node.Process

import Prelude

import Control.Monad.ST.Global (Global)
import Control.Monad.ST.Global as STGlobal
import Data.Array.ST (STArray)
import Data.Array.ST as STArray
import Data.Maybe (Maybe)
import Data.Posix (Pid)
import Data.Posix.Signal (Signal)
import Data.Posix.Signal as Signal
import Effect (Effect)
import Effect.Exception (Error)
import Foreign.Object as FO
import Foreign.Object.ST (STObject)
import Foreign.Object.ST as STObject
import Node.Platform (Platform)
import Node.Platform as Platform
import Node.Stream (Readable, Writable)
Expand Down Expand Up @@ -86,15 +95,22 @@ onSignal sig = onSignalImpl (Signal.toString sig)
nextTick :: Effect Unit -> Effect Unit
nextTick callback = mkEffect \_ -> process.nextTick callback

-- | Get an array containing the command line arguments. Be aware
-- | that this can change over the course of the program.
-- | Get an array containing the command line arguments.
argv :: Effect (Array String)
argv = mkEffect \_ -> process.argv
argv = STGlobal.toEffect (STArray.freeze argv')

-- | Node-specific options passed to the `node` executable. Be aware that
-- | this can change over the course of the program.
-- | Get the mutable array containing the command line arguments.
argv' :: STArray Global String
argv' = process.argv

-- | Node-specific options passed to the `node` executable.
execArgv :: Effect (Array String)
execArgv = mkEffect \_ -> process.execArgv
execArgv = STGlobal.toEffect (STArray.freeze execArgv')

-- | Mutable array of node-specific options passed to the
-- | `node` executable.
execArgv' :: STArray Global String
execArgv' = process.execArgv

-- | The absolute pathname of the `node` executable that started the
-- | process.
Expand All @@ -111,18 +127,24 @@ cwd = process.cwd

-- | Get a copy of the current environment.
getEnv :: Effect (FO.Object String)
getEnv = mkEffect \_ -> process.env
getEnv = STGlobal.toEffect (FO.freezeST env)

-- | Mutable reference to the current environment.
env :: STObject Global String
env = process.env

-- | Lookup a particular environment variable.
lookupEnv :: String -> Effect (Maybe String)
lookupEnv k = FO.lookup k <$> getEnv
lookupEnv k = STGlobal.toEffect (STObject.peek k env)

-- | Set an environment variable.
foreign import setEnv :: String -> String -> Effect Unit
setEnv :: String -> String -> Effect Unit
setEnv k v = void $ STGlobal.toEffect (STObject.poke k v env)

-- | Delete an environment variable.
-- | Use case: to hide secret environment variable from child processes.
foreign import unsetEnv :: String -> Effect Unit
unsetEnv :: String -> Effect Unit
unsetEnv k = void $ STGlobal.toEffect (STObject.delete k env)

pid :: Pid
pid = process.pid
Expand Down