Skip to content

Commit

Permalink
Merge pull request #131 from f-o-a-m/sync-fs
Browse files Browse the repository at this point in the history
Sync FS reads/writes
  • Loading branch information
iostat committed Apr 22, 2021
2 parents 2589607 + aef5606 commit 41d55ba
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "chanterelle",
"version": "5.1.2",
"version": "5.1.3",
"description": "A more functional truffle",
"license": "ISC",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages.dhall
Expand Up @@ -89,7 +89,7 @@ let additions =
, version =
"v1.0.0"
}
, solc =
, solc =
{ dependencies =
[ "aff"
, "argonaut"
Expand Down
20 changes: 16 additions & 4 deletions src/Chanterelle/Internal/Utils/FS.purs
Expand Up @@ -7,17 +7,20 @@ import Chanterelle.Internal.Types.Compile (CompileError(..))
import Chanterelle.Internal.Utils.Error (catchingAff, withExceptT')
import Control.Monad.Error.Class (class MonadThrow, throwError)
import Data.DateTime.Instant (fromDateTime, unInstant)
import Data.Function.Uncurried (runFn2, runFn3)
import Data.Int.Bits ((.|.))
import Effect (Effect)
import Effect.Aff (Milliseconds)
import Effect.Aff.Class (class MonadAff, liftAff)
import Effect.Class (liftEffect)
import Node.Encoding (Encoding(UTF8))
import Node.Encoding (Encoding(..))
import Node.FS.Aff as FS
import Node.FS.Internal as FSInternal
import Node.FS.Stats as Stats
import Node.Path (FilePath)
import Node.Path as Path

unparsePath
unparsePath
:: forall p
. { dir :: String
, name :: String
Expand Down Expand Up @@ -79,7 +82,9 @@ readTextFile
=> MonadThrow String m
=> FilePath
-> m String
readTextFile filename = catchingAff (FS.readTextFile UTF8 filename)
readTextFile filename = catchingAff wrapInternalRead
where wrapInternalRead = liftEffect <<< FSInternal.mkEffect $ \_ -> runFn2
FSInternal.unsafeRequireFS.readFileSync filename { encoding: show UTF8, flag: "rs+" }

writeTextFile
:: forall m
Expand All @@ -88,7 +93,14 @@ writeTextFile
=> FilePath
-> String
-> m Unit
writeTextFile filename contents = catchingAff (FS.writeTextFile UTF8 filename contents)
writeTextFile filename contents = catchingAff wrapInternalWrite
where wrapInternalWrite = liftEffect <<< FSInternal.mkEffect $ \_ -> runFn3
FSInternal.unsafeRequireFS.writeFileSync filename contents { encoding: show UTF8, flag: writeSyncFlag }

writeSyncFlag = FSInternal.unsafeRequireFS."O_TRUNC"
.|. FSInternal.unsafeRequireFS."O_CREAT"
.|. FSInternal.unsafeRequireFS."O_RDWR"
.|. FSInternal.unsafeRequireFS."O_SYNC"

withTextFile
:: forall m
Expand Down

0 comments on commit 41d55ba

Please sign in to comment.