@@ -51,15 +51,13 @@ import Docs.Search.Types (PartId)
5151import Effect (Effect )
5252import Effect.Aff (Aff , parallel , sequential )
5353import Effect.Class (liftEffect )
54- import Effect.Console (log )
5554import JSON (JSON )
5655import JSON as JSON
5756import Node.Encoding (Encoding (UTF8))
5857import Node.FS.Aff (mkdir , readFile , readTextFile , readdir , stat , writeFile , writeTextFile )
5958import Node.FS.Stats (isDirectory , isFile )
6059import Node.FS.Sync (exists )
6160import Node.Path as Path
62- import Node.Process as Process
6361import Registry.Manifest (Manifest (..))
6462import Registry.Manifest as Manifest
6563import Registry.PackageName (PackageName )
@@ -72,15 +70,16 @@ type Config =
7270 , generatedDocs :: String
7371 , workspacePackages :: Set PackageName
7472 , moduleGraph :: Graph.ModuleGraphWithPackage
73+ , log :: String -> Aff Unit
74+ , die :: String -> Aff Unit
7575 }
7676
7777run :: Config -> Aff Unit
78- run cfg = do
78+ run cfg@{ log } = do
7979
8080 checkDirectories cfg
8181
82- liftEffect do
83- log " Building the search index..."
82+ log " Building the search index..."
8483
8584 docsJsons /\ packageMetas <- sequential $
8685 Tuple
@@ -91,13 +90,12 @@ run cfg = do
9190 countOfPackages = Array .length packageMetas
9291 countOfModules = Array .length docsJsons
9392
94- liftEffect do
95- log $
96- " Indexing "
97- <> show countOfModules
98- <> " modules from "
99- <> show countOfPackages
100- <> " packages..."
93+ log $
94+ " Indexing "
95+ <> show countOfModules
96+ <> " modules from "
97+ <> show countOfPackages
98+ <> " packages..."
10199
102100 let
103101 scores = mkScores packageMetas
@@ -108,32 +106,28 @@ run cfg = do
108106
109107 createDirectories cfg
110108
111- void $ sequential do
112- ignore <$> parallel (writeIndex cfg index)
113- < *> parallel (writeTypeIndex typeIndex)
114- < *> parallel (writePackageInfo packageInfo)
115- < *> parallel (writeModuleIndex moduleIndex)
116- < *> parallel (patchDocs cfg)
117- < *> parallel (copyAppFile cfg)
109+ sequential $
110+ parallel (writeIndex cfg index)
111+ *> parallel (writeTypeIndex typeIndex)
112+ *> parallel (writePackageInfo packageInfo)
113+ *> parallel (writeModuleIndex moduleIndex)
114+ *> parallel (patchDocs cfg)
115+ *> parallel (copyAppFile cfg)
118116
119117 let
120118 countOfDefinitions = Trie .size $ unwrap index
121119 countOfTypeDefinitions =
122120 sum $ fromMaybe 0 <$> map Array .length <$> Map .values (unwrap typeIndex)
123121
124- liftEffect do
125- log $
126- " Added "
127- <> show countOfDefinitions
128- <> " definitions and "
129- <> show countOfTypeDefinitions
130- <> " type definitions from "
131- <> show countOfPackages
132- <>
133- " packages to the search index."
134-
135- where
136- ignore _ _ _ _ _ _ _ = unit
122+ log $
123+ " Added "
124+ <> show countOfDefinitions
125+ <> " definitions and "
126+ <> show countOfTypeDefinitions
127+ <> " type definitions from "
128+ <> show countOfPackages
129+ <>
130+ " packages to the search index."
137131
138132-- | Exit early if something is missing.
139133checkDirectories :: Config -> Aff Unit
@@ -147,23 +141,20 @@ checkDirectories cfg = do
147141
148142 for_ dirs \dir -> do
149143 whenM (not <$> directoryExists dir) $
150- liftEffect do
151- logAndExit " Build the documentation first!"
144+ cfg.die " Build the documentation first!"
152145
153146-- | Read and decode given `docs.json` files.
154147decodeDocsJsons
155- :: forall rest
156- . { docsFiles :: Array String | rest }
148+ :: ∀ rest
149+ . { docsFiles :: Array String , log :: String -> Aff Unit , die :: String -> Aff Unit | rest }
157150 -> Aff (Array DocModule )
158- decodeDocsJsons cfg@{ docsFiles } = do
151+ decodeDocsJsons cfg@{ docsFiles, log } = do
159152
160153 paths <- getPathsByGlobs docsFiles
161154
162155 when (Array .null paths) do
163- liftEffect do
164- logAndExit $
165- " The following globs do not match any files: " <> showGlobs cfg.docsFiles <>
166- " .\n Build the documentation first!"
156+ cfg.die $
157+ " The following globs do not match any files: " <> showGlobs cfg.docsFiles <> " .\n Build the documentation first!"
167158
168159 docsJsons <- Array .catMaybes <$> for paths \jsonFile -> do
169160 doesExist <- fileExists jsonFile
@@ -179,38 +170,36 @@ decodeDocsJsons cfg@{ docsFiles } = do
179170
180171 case eiResult of
181172 Left error -> do
182- liftEffect $ log $
183- " \" docs.json\" decoding failed failed for " <> jsonFile <> " : " <> error
173+ log $ " \" docs.json\" decoding failed failed for " <> jsonFile <> " : " <> error
184174 pure Nothing
185175 Right result -> pure $ Just result
186176
187177 else do
188- liftEffect $ do
189- log $
190- " File does not exist: " <> jsonFile
178+ log $ " File does not exist: " <> jsonFile
191179 pure Nothing
192180
193181 when (Array .null docsJsons) do
194- liftEffect $ logAndExit $
182+ cfg.die $
195183 " Couldn't decode any of the files matched by the following globs: " <> showGlobs cfg.docsFiles
196184
197185 pure docsJsons
198186
199- decodePursJsons :: forall rest . { pursJsonFiles :: Array String | rest } -> Aff (Array Manifest )
200- decodePursJsons { pursJsonFiles } = do
187+ decodePursJsons
188+ :: ∀ rest
189+ . { pursJsonFiles :: Array String , log :: String -> Aff Unit , die :: String -> Aff Unit | rest }
190+ -> Aff (Array Manifest )
191+ decodePursJsons cfg@{ pursJsonFiles } = do
201192 paths <- getPathsByGlobs pursJsonFiles
202193
203194 when (Array .null paths) do
204- liftEffect do
205- logAndExit $
206- " The following globs do not match any files: " <> showGlobs pursJsonFiles <>
207- " .\n Are you in a project directory?"
208-
195+ cfg.die $
196+ " The following globs do not match any files: " <> showGlobs pursJsonFiles <>
197+ " .\n Are you in a project directory?"
209198 Array .nubBy compareNames
210199 <$> Array .catMaybes
211200 <$>
212201 for paths \jsonFileName ->
213- join <$> withExisting jsonFileName
202+ join <$> withExisting cfg jsonFileName
214203 \contents ->
215204 either (logError jsonFileName) (pure <<< Just )
216205 ( JSON .parse contents >>=
@@ -224,24 +213,23 @@ decodePursJsons { pursJsonFiles } = do
224213 (Manifest { name: name2 }) = compare name1 name2
225214
226215 logError fileName error = do
227- liftEffect $ log $
228- " \" purs.json\" decoding failed for " <> fileName <> " : " <> error
216+ cfg.log $ " \" purs.json\" decoding failed for " <> fileName <> " : " <> error
229217 pure Nothing
230218
231219-- | Write type index parts to files.
232220writeTypeIndex :: TypeIndex -> Aff Unit
233221writeTypeIndex typeIndex =
234222 for_ entries \(Tuple typeShape results) -> do
235223 writeTextFile UTF8 (unwrap Config .typeIndexDirectory <> " /" <> typeShape <> " .js" )
236- (mkHeader typeShape <> JSON .print (CJ .encode codec results))
224+ (mkHeader typeShape <> JSON .print (CJ .encode codec $ fromMaybe [] results))
237225 where
238226 mkHeader typeShape =
239227 " // This file was generated by docs-search\n "
240228 <> " window.DocsSearchTypeIndex[\" "
241229 <> typeShape
242230 <> " \" ] = "
243231
244- codec = CJ.Common .maybe $ CJ . array SearchResult .searchResultCodec
232+ codec = CJ .array SearchResult .searchResultCodec
245233
246234 entries :: Array _
247235 entries = Map .toUnfoldableUnordered (unwrap typeIndex)
@@ -350,18 +338,18 @@ patchDocs cfg = do
350338-- | Create directories for two indices, or fail with a message
351339-- | in case the docs were not generated.
352340createDirectories :: Config -> Aff Unit
353- createDirectories { generatedDocs } = do
341+ createDirectories { generatedDocs, die } = do
354342 let
355343 htmlDocs = Path .concat [ generatedDocs, " html" ]
356344 indexDir = Path .concat [ generatedDocs, " html" , " index" ]
357345 declIndexDir = Path .concat [ generatedDocs, " html" , " index" , " declarations" ]
358346 typeIndexDir = Path .concat [ generatedDocs, " html" , " index" , " types" ]
359347
360- whenM (not <$> directoryExists generatedDocs) $ liftEffect do
361- logAndExit " Generate the documentation first!"
348+ whenM (not <$> directoryExists generatedDocs) $
349+ die " Generate the documentation first!"
362350
363- whenM (not <$> directoryExists htmlDocs) $ liftEffect do
364- logAndExit " Generate the documentation first!"
351+ whenM (not <$> directoryExists htmlDocs) $
352+ die " Generate the documentation first!"
365353
366354 whenM (not <$> directoryExists indexDir) do
367355 mkdir indexDir
@@ -375,13 +363,13 @@ createDirectories { generatedDocs } = do
375363-- | Copy the client-side application, responsible for handling user input and rendering
376364-- | the results, to the destination path.
377365copyAppFile :: Config -> Aff Unit
378- copyAppFile { generatedDocs } = do
366+ copyAppFile { generatedDocs, die } = do
379367 appFile <- liftEffect getDocsSearchAppPath
380- whenM (not <$> fileExists appFile) do
381- liftEffect do
382- logAndExit $
383- " Client-side app was not found at " <> appFile <> " .\n " <>
384- " Check your installation."
368+ unlessM ( fileExists appFile)
369+ $ die
370+ $
371+ " Client-side app was not found at " <> appFile <> " .\n " <>
372+ " Check your installation."
385373 buffer <- readFile appFile
386374 writeFile (Path .concat [ generatedDocs, " html" , " docs-search-app.js" ]) buffer
387375
@@ -399,25 +387,18 @@ fileExists path = do
399387 false -> pure false
400388 true -> isFile <$> stat path
401389
402- withExisting :: forall a . String -> (String -> Aff a ) -> Aff (Maybe a )
403- withExisting file f = do
390+ withExisting :: ∀ a r . { log :: String -> Aff Unit | r } -> String -> (String -> Aff a ) -> Aff (Maybe a )
391+ withExisting cfg file f = do
404392 doesExist <- fileExists file
405393
406394 if doesExist then do
407395 contents <- readTextFile UTF8 file
408396 res <- f contents
409397 pure $ Just res
410398 else do
411- liftEffect $ do
412- log $
413- " File does not exist: " <> file
399+ cfg.log $ " File does not exist: " <> file
414400 pure Nothing
415401
416- logAndExit :: forall a . String -> Effect a
417- logAndExit message = do
418- log message
419- Process .exit' 1
420-
421402showGlobs :: Array String -> String
422403showGlobs = Array .intercalate " , "
423404
0 commit comments