diff options
Diffstat (limited to 'src/PursLoader/Options.purs')
-rw-r--r-- | src/PursLoader/Options.purs | 121 |
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 @@ | |||
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 (Unit(), (<>), (<$>), (<<<), (++), (<*>), ($), 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(), F()) | 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 { bundleOutput :: String } |
19 | 15 | ||
20 | noPreludeOpt = "no-prelude" | 16 | type Options_ = { bundleOutput :: String } |
21 | 17 | ||
22 | noOptsOpt = "no-opts" | 18 | runOptions :: Options -> Options_ |
23 | 19 | runOptions (Options options) = options | |
24 | noMagicDoOpt = "no-magic-do" | ||
25 | |||
26 | noTcoOpt = "no-tco" | ||
27 | |||
28 | verboseErrorsOpt = "verbose-errors" | ||
29 | |||
30 | outputOpt = "output" | ||
31 | |||
32 | commentsOpt = "comments" | ||
33 | |||
34 | noPrefixOpt = "no-prefix" | ||
35 | |||
36 | requirePathOpt = "require-path" | ||
37 | |||
38 | srcOpt = "src" | ||
39 | |||
40 | ffiOpt = "ffi" | ||
41 | |||
42 | newtype 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 | |||
56 | output :: Options -> String | ||
57 | output (Options o) = o.output | ||
58 | 20 | ||
59 | instance isForeignOptions :: IsForeign Options where | 21 | instance 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 | |||
82 | class LoaderOption a where | ||
83 | opt :: String -> NullOrUndefined a -> Array String | ||
84 | |||
85 | instance booleanLoaderOption :: LoaderOption Boolean where | ||
86 | opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val) | ||
87 | |||
88 | instance stringLoaderOption :: LoaderOption String where | ||
89 | opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val) | ||
90 | |||
91 | instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where | ||
92 | opt key val = concat (opt key <$> (NullOrUndefined <<< Just) | ||
93 | <$> (fromMaybe [] (runNullOrUndefined val))) | ||
94 | |||
95 | pscOptions :: Options -> Array String | ||
96 | pscOptions (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 | |||
107 | loaderSrcOption :: Foreign -> Maybe (Array String) | ||
108 | loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) | ||
109 | |||
110 | loaderFFIOption :: Foreign -> Maybe (Array String) | ||
111 | loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query) | ||