Skip to content

Commit 9355fcd

Browse files
committed
Proper data type for platform
1 parent cb2fc7f commit 9355fcd

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/Node/Platform.purs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- | This module defines data type for the different platforms supported by
2+
-- | Node.js
3+
module Node.Platform where
4+
5+
import Prelude
6+
import Data.Function (on)
7+
import Data.Maybe (Maybe(..))
8+
9+
data Platform
10+
= Darwin
11+
| FreeBSD
12+
| Linux
13+
| SunOS
14+
| Win32
15+
16+
-- | The String representation for a platform, recognised by Node.js.
17+
toString :: Platform -> String
18+
toString Darwin = "darwin"
19+
toString FreeBSD = "freebsd"
20+
toString Linux = "linux"
21+
toString SunOS = "sunos"
22+
toString Win32 = "win32"
23+
24+
fromString :: String -> Maybe Platform
25+
fromString "darwin" = Just Darwin
26+
fromString "freebsd" = Just FreeBSD
27+
fromString "linux" = Just Linux
28+
fromString "sunos" = Just SunOS
29+
fromString "win32" = Just Win32
30+
fromString _ = Nothing
31+
32+
instance showPlatform :: Show Platform where
33+
show Darwin = "Darwin"
34+
show FreeBSD = "FreeBSD"
35+
show Linux = "Linux"
36+
show SunOS = "SunOS"
37+
show Win32 = "Win32"
38+
39+
instance eqPlatform :: Eq Platform where
40+
eq = eq `on` toString
41+
42+
instance ordPlatform :: Ord Platform where
43+
compare = compare `on` toString

src/Node/Process.purs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ import Prelude
2626
import Control.Monad.Eff
2727
import Control.Monad.Eff.Console (CONSOLE())
2828
import Control.Monad.Eff.Exception (EXCEPTION())
29-
import Control.Monad.Eff.Unsafe (unsafePerformEff)
3029
import Data.Maybe (Maybe())
30+
import Data.Maybe.Unsafe (fromJust)
3131
import Data.StrMap (StrMap())
3232
import Data.StrMap as StrMap
3333
import Node.Stream (Readable(), Writable())
3434

35+
import Node.Platform (Platform())
36+
import Node.Platform as Platform
37+
3538
-- | An effect tracking interaction with the global `process` object.
3639
foreign import data PROCESS :: !
3740

@@ -94,8 +97,8 @@ foreign import setEnv :: forall eff. String -> String -> Eff (process :: PROCESS
9497
pid :: Int
9598
pid = process.pid
9699

97-
platform :: String
98-
platform = process.platform
100+
platform :: Platform
101+
platform = fromJust (Platform.fromString process.platform)
99102

100103
-- | Cause the process to exit with the supplied integer code. An exit code
101104
-- | of 0 is normally considered successful, and anything else is considered a

0 commit comments

Comments
 (0)