dillonkearns/elm-pages/10.2.2

NOTE: the latest version is 12.3.0

Pages.Script

An elm-pages Script is a way to execute an elm-pages BackendTask.

Read more about using the elm-pages CLI to run (or bundle) scripts, plus a brief tutorial, at https://elm-pages.com/docs/elm-pages-scripts.

type alias Script =
Script

The type for your run function that can be executed by elm-pages run.

Defining Scripts

Config cliOptions
-> (cliOptions -> BackendTask FatalError ())
-> Script

Same as withoutCliOptions, but allows you to define a CLI Options Parser so the user can pass in additional options for the script.

Uses https://package.elm-lang.org/packages/dillonkearns/elm-cli-options-parser/latest/.

Read more at https://elm-pages.com/docs/elm-pages-scripts/#adding-command-line-options.

withoutCliOptions : BackendTask FatalError () -> Script

Define a simple Script (no CLI Options).

module MyScript exposing (run)


import BackendTask
import Pages.Script as Script


run =
    Script.withoutCliOptions
        (Script.log "Hello!"
            |> BackendTask.allowFatal
        )

File System Utilities

{ path : String
, body : String
}
-> BackendTask
{ fatal : FatalError
, recoverable : Error
}
()

Write a file to the file system.

File paths are relative to the root of your elm-pages project (next to the elm.json file and src/ directory), or you can pass in absolute paths beginning with a /.

module MyScript exposing (run)


import BackendTask
import Pages.Script as Script


run =
    Script.withoutCliOptions
        (Script.writeFile
            { path = "hello.json"
            , body = """{ "message": "Hello, World!" }"""
            }
            |> BackendTask.allowFatal
        )

Shell Commands

command : String -> List String -> BackendTask FatalError String

Run a single command and return stderr and stdout combined as a single String.

If you want to do more advanced things like piping together multiple commands in a pipeline, or piping in a file to a command, etc., see the Stream module.

module MyScript exposing (run)


import BackendTask
import Pages.Script as Script exposing (Script)


run : Script
run =
    Script.withoutCliOptions
        (Script.command "ls" []
            |> BackendTask.andThen
                (\files ->
                    Script.log ("Files: " ++ files)
                )
        )
exec : String -> List String -> BackendTask FatalError ()

Like command, but prints stderr and stdout to the console as the command runs instead of capturing them.

module MyScript exposing (run)


import BackendTask
import Pages.Script as Script exposing (Script)


run : Script
run =
    Script.withoutCliOptions
        (Script.exec "ls" [])

Utilities

log : String -> BackendTask error ()

Log to stdout.

module MyScript exposing (run)


import BackendTask
import Pages.Script as Script


run =
    Script.withoutCliOptions
        (Script.log "Hello!"
            |> BackendTask.allowFatal
        )
sleep : Int -> BackendTask error ()

Sleep for a number of milliseconds.

module MyScript exposing (run)


import BackendTask
import Pages.Script as Script


run =
    Script.withoutCliOptions
        (Script.log "Hello..."
            |> Script.doThen
                (Script.sleep 1000)
            |> Script.doThen
                (Script.log "World!")
        )
BackendTask error value
-> BackendTask error ()
-> BackendTask error value

Run a command with no output, then run another command.

module MyScript exposing (run)


import BackendTask
import Pages.Script as Script


run =
    Script.withoutCliOptions
        (Script.log "Hello!"
            |> Script.doThen
                (Script.log "World!")
        )
which : String -> BackendTask error (Maybe String)

Same as expectWhich, but returns Nothing if the command is not found instead of failing with a FatalError.

expectWhich : String -> BackendTask FatalError String

Check if a command is available on the system. If it is, return the full path to the command, otherwise fail with a FatalError.

module MyScript exposing (run)


import BackendTask
import Pages.Script as Script


run : Script
run =
    Script.withoutCliOptions
        (Script.expectWhich "elm-review"
            |> BackendTask.andThen
                (\path ->
                    Script.log ("The path to `elm-review` is: " ++ path)
                )
        )

If you run it with a command that is not available, you will see an error like this:

Script.expectWhich "hype-script"
-- COMMAND NOT FOUND ---------------
I expected to find `hype-script`, but it was not on your PATH. Make sure it is installed and included in your PATH.
question : String -> BackendTask error String
module QuestionDemo exposing (run)


import BackendTask


run : Script
run =
    Script.withoutCliOptions
        (Script.question "What is your name? "
            |> BackendTask.andThen
                (\name ->
                    Script.log ("Hello, " ++ name ++ "!")
                )
        )

Errors

type Error
= FileWriteError

The recoverable error type for file writes. You can use BackendTask.allowFatal if you want to allow the program to crash with an error message if a file write is unsuccessful.