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