Skip to content

Commit

Permalink
Add which helpers to Script module.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed Mar 14, 2024
1 parent 58f24dc commit c6a752a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
16 changes: 16 additions & 0 deletions examples/end-to-end/script/src/Which.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Which exposing (run)

import BackendTask exposing (BackendTask)
import Pages.Script as Script exposing (Script, doThen, sleep)
import Pages.Script.Spinner as Spinner


run : Script
run =
Script.withoutCliOptions
(Script.expectWhich "elm"
|> BackendTask.andThen
(\elmPath ->
Script.log elmPath
)
)
12 changes: 12 additions & 0 deletions generator/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as fs from "node:fs";
import * as crypto from "node:crypto";
import { restoreColorSafe } from "./error-formatter.js";
import { Spinnies } from './spinnies/index.js'
import { default as which } from "which";

const spinnies = new Spinnies();

Expand Down Expand Up @@ -504,6 +505,8 @@ async function runInternalJob(
return [requestHash, await runWriteFileJob(requestToPerform)];
} else if (requestToPerform.url === "elm-pages-internal://sleep") {
return [requestHash, await runSleep(requestToPerform)];
} else if (requestToPerform.url === "elm-pages-internal://which") {
return [requestHash, await runWhich(requestToPerform)];
} else if (requestToPerform.url === "elm-pages-internal://start-spinner") {
return [requestHash, runStartSpinner(requestToPerform)];
} else if (requestToPerform.url === "elm-pages-internal://stop-spinner") {
Expand Down Expand Up @@ -548,6 +551,15 @@ function runSleep(req) {
});
}

async function runWhich(req) {
const command = req.body.args[0];
try {
return jsonResponse(req, await which(command));
} catch (error) {
return jsonResponse(req, null);
}
}

async function runWriteFileJob(req) {
const data = req.body.args[0];
try {
Expand Down
34 changes: 32 additions & 2 deletions src/Pages/Script.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Pages.Script exposing
( Script
, withCliOptions, withoutCliOptions
, writeFile
, log, sleep, doThen
, log, sleep, doThen, which, expectWhich
, Error(..)
)

Expand All @@ -25,7 +25,7 @@ Read more about using the `elm-pages` CLI to run (or bundle) scripts, plus a bri
## Utilities
@docs log, sleep, doThen
@docs log, sleep, doThen, which, expectWhich
## Errors
Expand Down Expand Up @@ -189,3 +189,33 @@ doThen : BackendTask error value -> BackendTask error () -> BackendTask error va
doThen task1 task2 =
task2
|> BackendTask.andThen (\() -> task1)


{-| -}
which : String -> BackendTask error (Maybe String)
which command =
BackendTask.Internal.Request.request
{ body = BackendTask.Http.jsonBody (Encode.string command)
, expect = BackendTask.Http.expectJson (Decode.nullable Decode.string)
, name = "which"
}


{-| -}
expectWhich : String -> BackendTask FatalError String
expectWhich command =
which command
|> BackendTask.andThen
(\maybePath ->
case maybePath of
Just path ->
BackendTask.succeed path

Nothing ->
BackendTask.fail
(FatalError.build
{ title = "Command not found"
, body = "I expected to find `" ++ command ++ "`, but it was not on your PATH. Make sure it is installed and included in your PATH."
}
)
)

0 comments on commit c6a752a

Please sign in to comment.