aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader/Options.purs
diff options
context:
space:
mode:
Diffstat (limited to 'src/PursLoader/Options.purs')
-rw-r--r--src/PursLoader/Options.purs121
1 files changed, 20 insertions, 101 deletions
diff --git a/src/PursLoader/Options.purs b/src/PursLoader/Options.purs
index 1650652..706ddd2 100644
--- a/src/PursLoader/Options.purs
+++ b/src/PursLoader/Options.purs
@@ -1,111 +1,30 @@
1module PursLoader.Options 1module PursLoader.Options
2 ( pscOptions 2 ( Options()
3 , loaderSrcOption 3 , runOptions
4 , loaderFFIOption
5 , Options()
6 , output
7 ) where 4 ) where
8 5
9import Prelude (Unit(), (<>), (<$>), (<<<), (++), (<*>), ($), const, id) 6import Prelude ((<$>), (<<<), id)
10 7
11import Data.Array (concat) 8import Data.Foreign.Class (IsForeign, readProp)
12import Data.Either (either) 9import Data.Foreign.NullOrUndefined (runNullOrUndefined)
10import Data.Maybe (maybe)
13 11
14import Data.Foreign (Foreign(), F()) 12import PursLoader.Path (joinPath)
15import Data.Foreign.Class (IsForeign, read, readProp)
16import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined)
17 13
18import Data.Maybe (Maybe(..), maybe, fromMaybe) 14newtype Options = Options { bundleOutput :: String }
19 15
20noPreludeOpt = "no-prelude" 16type Options_ = { bundleOutput :: String }
21 17
22noOptsOpt = "no-opts" 18runOptions :: Options -> Options_
23 19runOptions (Options options) = options
24noMagicDoOpt = "no-magic-do"
25
26noTcoOpt = "no-tco"
27
28verboseErrorsOpt = "verbose-errors"
29
30outputOpt = "output"
31
32commentsOpt = "comments"
33
34noPrefixOpt = "no-prefix"
35
36requirePathOpt = "require-path"
37
38srcOpt = "src"
39
40ffiOpt = "ffi"
41
42newtype Options
43 = Options { noPrelude :: NullOrUndefined Boolean
44 , noOpts :: NullOrUndefined Boolean
45 , noMagicDo :: NullOrUndefined Boolean
46 , noTco :: NullOrUndefined Boolean
47 , verboseErrors :: NullOrUndefined Boolean
48 , comments :: NullOrUndefined Boolean
49 , output :: String
50 , noPrefix :: NullOrUndefined Boolean
51 , requirePath :: String
52 , src :: NullOrUndefined (Array String)
53 , ffi :: NullOrUndefined (Array String)
54 }
55
56output :: Options -> String
57output (Options o) = o.output
58 20
59instance isForeignOptions :: IsForeign Options where 21instance isForeignOptions :: IsForeign Options where
60 read obj = Options <$> ({ noPrelude: _ 22 read obj =
61 , noOpts: _ 23 Options <$> ({ bundleOutput: _ }
62 , noMagicDo: _ 24 <$> (maybe bundleOutputDefault id <<< runNullOrUndefined <$> readProp bundleOutput obj))
63 , noTco: _ 25 where
64 , verboseErrors: _ 26 bundleOutput :: String
65 , comments: _ 27 bundleOutput = "bundleOutput"
66 , output: _ 28
67 , noPrefix: _ 29 bundleOutputDefault :: String
68 , requirePath: "../" 30 bundleOutputDefault = joinPath "output" "bundle.js"
69 , src: _
70 , ffi: _
71 } <$> readProp noPreludeOpt obj
72 <*> readProp noOptsOpt obj
73 <*> readProp noMagicDoOpt obj
74 <*> readProp noTcoOpt obj
75 <*> readProp verboseErrorsOpt obj
76 <*> readProp commentsOpt obj
77 <*> (maybe "output" id <<< runNullOrUndefined <$> readProp outputOpt obj)
78 <*> readProp noPrefixOpt obj
79 <*> readProp srcOpt obj
80 <*> readProp ffiOpt obj)
81
82class LoaderOption a where
83 opt :: String -> NullOrUndefined a -> Array String
84
85instance booleanLoaderOption :: LoaderOption Boolean where
86 opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val)
87
88instance stringLoaderOption :: LoaderOption String where
89 opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val)
90
91instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where
92 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
93 <$> (fromMaybe [] (runNullOrUndefined val)))
94
95pscOptions :: Options -> Array String
96pscOptions (Options a) = opt noPreludeOpt a.noPrelude <>
97 opt noOptsOpt a.noOpts <>
98 opt noMagicDoOpt a.noMagicDo <>
99 opt noTcoOpt a.noTco <>
100 opt verboseErrorsOpt a.verboseErrors <>
101 opt commentsOpt a.comments <>
102 opt outputOpt (NullOrUndefined $ Just a.output) <>
103 opt noPrefixOpt a.noPrefix <>
104 opt requirePathOpt (NullOrUndefined $ Just a.requirePath) <>
105 opt ffiOpt a.ffi
106
107loaderSrcOption :: Foreign -> Maybe (Array String)
108loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
109
110loaderFFIOption :: Foreign -> Maybe (Array String)
111loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)