From 89a5f523aa62c7d535ccd90bea73b37dcfc95736 Mon Sep 17 00:00:00 2001 From: Nicolas Perriault Date: Tue, 17 Sep 2024 14:57:26 +0200 Subject: [PATCH] refactor: clean code. --- package.json | 2 +- src/CheckDb.elm | 43 +++++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 8ca23eaff..87cafce18 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "decrypt": "./bin/decrypt", "encrypt": "./bin/encrypt", "db:build": "./bin/build-db && npm run db:check", - "db:check": "elm make src/CheckDb.elm --output=check-db-app.js 1> /dev/null && node check-db.js", + "db:check": "elm make src/CheckDb.elm --optimize --output=check-db-app.js 1> /dev/null && node check-db.js", "lint:openapi": "npx swagger-cli validate openapi.yaml", "lint:prettier": "prettier --config .prettierrc --check", "lint:prettier:all": "npm run lint:prettier -- .", diff --git a/src/CheckDb.elm b/src/CheckDb.elm index 9b2e90ecf..2d98b455d 100644 --- a/src/CheckDb.elm +++ b/src/CheckDb.elm @@ -1,6 +1,6 @@ port module CheckDb exposing (main) -import Static.Db as StaticDb +import Static.Db as StaticDb exposing (Db) import Static.Json as StaticJson @@ -10,30 +10,33 @@ type alias Flags = } -checkDbs : Flags -> Result String StaticDb.Db -checkDbs detailedRawProcessesJson = - StaticDb.db StaticJson.rawJsonProcesses - |> Result.mapError (\err -> "Non-detailed Db is invalid: " ++ err) - |> Result.andThen - (StaticDb.db detailedRawProcessesJson - |> Result.mapError (\err -> "Detailed Db is invalid: " ++ err) - |> always - ) +init : Flags -> ( (), Cmd () ) +init flags = + ( () + , case checkStaticDatabases flags of + Err error -> + logAndExit { message = error, status = 1 } + + Ok _ -> + logAndExit { message = "Dbs look fine", status = 0 } + ) + + +checkStaticDatabases : Flags -> Result String ( Db, Db ) +checkStaticDatabases detailedRawJsonProcesses = + Result.map2 Tuple.pair + (StaticDb.db StaticJson.rawJsonProcesses + |> Result.mapError (\err -> "Non-detailed Db is invalid: " ++ err) + ) + (StaticDb.db detailedRawJsonProcesses + |> Result.mapError (\err -> "Detailed Db is invalid: " ++ err) + ) main : Program Flags () () main = Platform.worker - { init = - \flags -> - ( () - , case checkDbs flags of - Err error -> - logAndExit { message = "Db is dubious: " ++ error, status = 1 } - - Ok _ -> - logAndExit { message = "Db is fine", status = 0 } - ) + { init = init , subscriptions = always Sub.none , update = \_ _ -> ( (), Cmd.none ) }