init
This commit is contained in:
3
script/custom-backend-task.ts
Normal file
3
script/custom-backend-task.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export async function hello(name) {
|
||||
return `Hello ${name}!`;
|
||||
}
|
61
script/elm.json
Normal file
61
script/elm.json
Normal file
@ -0,0 +1,61 @@
|
||||
{
|
||||
"type": "application",
|
||||
"source-directories": [
|
||||
"src",
|
||||
"../codegen"
|
||||
],
|
||||
"elm-version": "0.19.1",
|
||||
"dependencies": {
|
||||
"direct": {
|
||||
"dillonkearns/elm-cli-options-parser": "3.2.0",
|
||||
"dillonkearns/elm-pages": "10.0.0",
|
||||
"elm/bytes": "1.0.8",
|
||||
"elm/core": "1.0.5",
|
||||
"elm/html": "1.0.0",
|
||||
"elm/json": "1.1.3",
|
||||
"mdgriffith/elm-codegen": "3.0.0"
|
||||
},
|
||||
"indirect": {
|
||||
"Chadtech/elm-bool-extra": "2.4.2",
|
||||
"avh4/elm-color": "1.0.0",
|
||||
"danfishgold/base64-bytes": "1.1.0",
|
||||
"danyx23/elm-mimetype": "4.0.1",
|
||||
"dillonkearns/elm-bcp47-language-tag": "1.0.1",
|
||||
"dillonkearns/elm-date-or-date-time": "2.0.0",
|
||||
"dillonkearns/elm-form": "3.0.0",
|
||||
"elm/browser": "1.0.2",
|
||||
"elm/file": "1.0.5",
|
||||
"elm/http": "2.0.0",
|
||||
"elm/parser": "1.1.0",
|
||||
"elm/random": "1.0.0",
|
||||
"elm/regex": "1.0.0",
|
||||
"elm/time": "1.0.0",
|
||||
"elm/url": "1.0.0",
|
||||
"elm/virtual-dom": "1.0.3",
|
||||
"elm-community/basics-extra": "4.1.0",
|
||||
"elm-community/list-extra": "8.7.0",
|
||||
"elm-community/maybe-extra": "5.3.0",
|
||||
"fredcy/elm-parseint": "2.0.1",
|
||||
"jluckyiv/elm-utc-date-strings": "1.0.0",
|
||||
"justinmimbs/date": "4.0.1",
|
||||
"miniBill/elm-codec": "2.0.0",
|
||||
"miniBill/elm-unicode": "1.0.3",
|
||||
"noahzgordon/elm-color-extra": "1.0.2",
|
||||
"robinheghan/fnv1a": "1.0.0",
|
||||
"robinheghan/murmur3": "1.0.0",
|
||||
"rtfeldman/elm-css": "18.0.0",
|
||||
"rtfeldman/elm-hex": "1.0.0",
|
||||
"rtfeldman/elm-iso8601-date-strings": "1.1.4",
|
||||
"stil4m/elm-syntax": "7.2.9",
|
||||
"stil4m/structured-writer": "1.0.3",
|
||||
"the-sett/elm-pretty-printer": "3.0.0",
|
||||
"the-sett/elm-syntax-dsl": "6.0.2",
|
||||
"turboMaCk/non-empty-list-alias": "1.3.1",
|
||||
"vito/elm-ansi": "10.0.1"
|
||||
}
|
||||
},
|
||||
"test-dependencies": {
|
||||
"direct": {},
|
||||
"indirect": {}
|
||||
}
|
||||
}
|
312
script/src/AddRoute.elm
Normal file
312
script/src/AddRoute.elm
Normal file
@ -0,0 +1,312 @@
|
||||
module AddRoute exposing (run)
|
||||
|
||||
import BackendTask
|
||||
import Cli.Option as Option
|
||||
import Cli.OptionsParser as OptionsParser
|
||||
import Cli.Program as Program
|
||||
import Elm
|
||||
import Elm.Annotation as Type
|
||||
import Elm.Case
|
||||
import Elm.Declare
|
||||
import Elm.Let
|
||||
import Elm.Op
|
||||
import Gen.BackendTask
|
||||
import Gen.Effect as Effect
|
||||
import Gen.FatalError
|
||||
import Gen.Form as Form
|
||||
import Gen.Form.FieldView as FieldView
|
||||
import Gen.Html as Html
|
||||
import Gen.Html.Attributes as Attr
|
||||
import Gen.Json.Encode
|
||||
import Gen.List
|
||||
import Gen.Maybe
|
||||
import Gen.Pages.Form as PagesForm
|
||||
import Gen.Pages.Script
|
||||
import Gen.Server.Request as Request
|
||||
import Gen.Server.Response as Response
|
||||
import Gen.View
|
||||
import Pages.Script as Script exposing (Script)
|
||||
import Scaffold.Form
|
||||
import Scaffold.Route exposing (Type(..))
|
||||
|
||||
|
||||
type alias CliOptions =
|
||||
{ moduleName : List String
|
||||
, fields : List ( String, Scaffold.Form.Kind )
|
||||
}
|
||||
|
||||
|
||||
run : Script
|
||||
run =
|
||||
Script.withCliOptions program
|
||||
(\cliOptions ->
|
||||
cliOptions
|
||||
|> createFile
|
||||
|> Script.writeFile
|
||||
|> BackendTask.allowFatal
|
||||
)
|
||||
|
||||
|
||||
program : Program.Config CliOptions
|
||||
program =
|
||||
Program.config
|
||||
|> Program.add
|
||||
(OptionsParser.build CliOptions
|
||||
|> OptionsParser.with (Option.requiredPositionalArg "module" |> Scaffold.Route.moduleNameCliArg)
|
||||
|> OptionsParser.withRestArgs Scaffold.Form.restArgsParser
|
||||
)
|
||||
|
||||
|
||||
createFile : CliOptions -> { path : String, body : String }
|
||||
createFile { moduleName, fields } =
|
||||
let
|
||||
formHelpers :
|
||||
Maybe
|
||||
{ formHandlers : Elm.Expression
|
||||
, form : Elm.Expression
|
||||
, declarations : List Elm.Declaration
|
||||
}
|
||||
formHelpers =
|
||||
Scaffold.Form.provide
|
||||
{ fields = fields
|
||||
, elmCssView = False
|
||||
, view =
|
||||
\{ formState, params } ->
|
||||
Elm.Let.letIn
|
||||
(\fieldView ->
|
||||
Elm.list
|
||||
((params
|
||||
|> List.map
|
||||
(\{ name, kind, param } ->
|
||||
fieldView (Elm.string name) param
|
||||
)
|
||||
)
|
||||
++ [ Elm.ifThen formState.submitting
|
||||
(Html.button
|
||||
[ Attr.disabled True
|
||||
]
|
||||
[ Html.text "Submitting..."
|
||||
]
|
||||
)
|
||||
(Html.button []
|
||||
[ Html.text "Submit"
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
)
|
||||
|> Elm.Let.fn2 "fieldView"
|
||||
( "label", Type.string |> Just )
|
||||
( "field", Nothing )
|
||||
(\label field ->
|
||||
Html.div []
|
||||
[ Html.label []
|
||||
[ Html.call_.text (Elm.Op.append label (Elm.string " "))
|
||||
, field |> FieldView.input []
|
||||
, errorsView.call formState.errors field
|
||||
]
|
||||
]
|
||||
)
|
||||
|> Elm.Let.toExpression
|
||||
}
|
||||
in
|
||||
Scaffold.Route.serverRender
|
||||
{ moduleName = moduleName
|
||||
, action =
|
||||
( Alias
|
||||
(Type.record
|
||||
(case formHelpers of
|
||||
Just _ ->
|
||||
[ ( "errors", Type.namedWith [ "Form" ] "ServerResponse" [ Type.string ] )
|
||||
]
|
||||
|
||||
Nothing ->
|
||||
[]
|
||||
)
|
||||
)
|
||||
, \routeParams request ->
|
||||
formHelpers
|
||||
|> Maybe.map
|
||||
(\justFormHelp ->
|
||||
Request.formData justFormHelp.formHandlers request
|
||||
|> Gen.Maybe.call_.map
|
||||
(Elm.fn ( "formData", Nothing )
|
||||
(\formData ->
|
||||
Elm.Case.tuple formData
|
||||
"response"
|
||||
"parsedForm"
|
||||
(\response parsedForm ->
|
||||
Elm.Case.custom parsedForm
|
||||
Type.int
|
||||
[ Elm.Case.branch1 "Form.Valid"
|
||||
( "validatedForm", Type.int )
|
||||
(\validatedForm ->
|
||||
Elm.Case.custom validatedForm
|
||||
Type.int
|
||||
[ Elm.Case.branch1 "Action"
|
||||
( "parsed", Type.int )
|
||||
(\parsed ->
|
||||
Scaffold.Form.recordEncoder parsed fields
|
||||
|> Gen.Json.Encode.encode 2
|
||||
|> Gen.Pages.Script.call_.log
|
||||
|> Gen.BackendTask.call_.map
|
||||
(Elm.fn ( "_", Nothing )
|
||||
(\_ ->
|
||||
Response.render
|
||||
(Elm.record
|
||||
[ ( "errors", response )
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
]
|
||||
)
|
||||
, Elm.Case.branch2 "Form.Invalid"
|
||||
( "parsed", Type.int )
|
||||
( "error", Type.int )
|
||||
(\_ _ ->
|
||||
"Form validations did not succeed!"
|
||||
|> Gen.Pages.Script.log
|
||||
|> Gen.BackendTask.call_.map
|
||||
(Elm.fn ( "_", Nothing )
|
||||
(\_ ->
|
||||
Response.render
|
||||
(Elm.record
|
||||
[ ( "errors", response )
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
|> Gen.Maybe.withDefault
|
||||
(Gen.BackendTask.fail
|
||||
(Gen.FatalError.fromString "Expected form post")
|
||||
)
|
||||
)
|
||||
|> Maybe.withDefault
|
||||
(Gen.BackendTask.succeed
|
||||
(Response.render
|
||||
(Elm.record [])
|
||||
)
|
||||
)
|
||||
)
|
||||
, data =
|
||||
( Alias (Type.record [])
|
||||
, \routeParams request ->
|
||||
Gen.BackendTask.succeed
|
||||
(Response.render
|
||||
(Elm.record [])
|
||||
)
|
||||
)
|
||||
, head = \app -> Elm.list []
|
||||
}
|
||||
|> Scaffold.Route.addDeclarations
|
||||
(formHelpers
|
||||
|> Maybe.map .declarations
|
||||
|> Maybe.map ((::) errorsView.declaration)
|
||||
|> Maybe.withDefault []
|
||||
)
|
||||
|> Scaffold.Route.buildWithLocalState
|
||||
{ view =
|
||||
\{ shared, model, app } ->
|
||||
Gen.View.make_.view
|
||||
{ title = moduleName |> String.join "." |> Elm.string
|
||||
, body =
|
||||
Elm.list
|
||||
(case formHelpers of
|
||||
Just justFormHelp ->
|
||||
[ Html.h2 [] [ Html.text "Form" ]
|
||||
, justFormHelp.form
|
||||
|> PagesForm.call_.renderHtml
|
||||
(Elm.list [])
|
||||
(Form.options "form"
|
||||
|> Form.withServerResponse
|
||||
(app
|
||||
|> Elm.get "action"
|
||||
|> Gen.Maybe.map (Elm.get "errors")
|
||||
)
|
||||
)
|
||||
app
|
||||
]
|
||||
|
||||
Nothing ->
|
||||
[ Html.h2 [] [ Html.text "New Page" ]
|
||||
]
|
||||
)
|
||||
}
|
||||
, update =
|
||||
\{ shared, app, msg, model } ->
|
||||
Elm.Case.custom msg
|
||||
(Type.named [] "Msg")
|
||||
[ Elm.Case.branch0 "NoOp"
|
||||
(Elm.tuple model
|
||||
Effect.none
|
||||
)
|
||||
]
|
||||
, init =
|
||||
\{ shared, app } ->
|
||||
Elm.tuple (Elm.record []) Effect.none
|
||||
, subscriptions =
|
||||
\{ routeParams, path, shared, model } ->
|
||||
Elm.val "Sub.none"
|
||||
, model =
|
||||
Alias (Type.record [])
|
||||
, msg =
|
||||
Custom [ Elm.variant "NoOp" ]
|
||||
}
|
||||
|
||||
|
||||
errorsView :
|
||||
{ declaration : Elm.Declaration
|
||||
, call : Elm.Expression -> Elm.Expression -> Elm.Expression
|
||||
, callFrom : List String -> Elm.Expression -> Elm.Expression -> Elm.Expression
|
||||
, value : List String -> Elm.Expression
|
||||
}
|
||||
errorsView =
|
||||
Elm.Declare.fn2 "errorsView"
|
||||
( "errors", Type.namedWith [ "Form" ] "Errors" [ Type.string ] |> Just )
|
||||
( "field"
|
||||
, Type.namedWith [ "Form", "Validation" ]
|
||||
"Field"
|
||||
[ Type.string
|
||||
, Type.var "parsed"
|
||||
, Type.var "kind"
|
||||
]
|
||||
|> Just
|
||||
)
|
||||
(\errors field ->
|
||||
Elm.ifThen
|
||||
(Gen.List.call_.isEmpty (Form.errorsForField field errors))
|
||||
(Html.div [] [])
|
||||
(Html.div
|
||||
[]
|
||||
[ Html.call_.ul (Elm.list [])
|
||||
(Gen.List.call_.map
|
||||
(Elm.fn ( "error", Nothing )
|
||||
(\error ->
|
||||
Html.li
|
||||
[ Attr.style "color" "red"
|
||||
]
|
||||
[ Html.call_.text error
|
||||
]
|
||||
)
|
||||
)
|
||||
(Form.errorsForField field errors)
|
||||
)
|
||||
]
|
||||
)
|
||||
|> Elm.withType
|
||||
(Type.namedWith [ "Html" ]
|
||||
"Html"
|
||||
[ Type.namedWith
|
||||
[ "PagesMsg" ]
|
||||
"PagesMsg"
|
||||
[ Type.named [] "Msg" ]
|
||||
]
|
||||
)
|
||||
)
|
42
script/src/Stars.elm
Normal file
42
script/src/Stars.elm
Normal file
@ -0,0 +1,42 @@
|
||||
module Stars exposing (run)
|
||||
|
||||
import BackendTask exposing (BackendTask)
|
||||
import BackendTask.Http
|
||||
import Cli.Option as Option
|
||||
import Cli.OptionsParser as OptionsParser
|
||||
import Cli.Program as Program
|
||||
import Json.Decode as Decode
|
||||
import Pages.Script as Script exposing (Script)
|
||||
|
||||
|
||||
run : Script
|
||||
run =
|
||||
Script.withCliOptions program
|
||||
(\{ username, repo } ->
|
||||
BackendTask.Http.getJson
|
||||
("https://api.github.com/repos/dillonkearns/" ++ repo)
|
||||
(Decode.field "stargazers_count" Decode.int)
|
||||
|> BackendTask.allowFatal
|
||||
|> BackendTask.andThen
|
||||
(\stars ->
|
||||
Script.log (String.fromInt stars)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
type alias CliOptions =
|
||||
{ username : String
|
||||
, repo : String
|
||||
}
|
||||
|
||||
|
||||
program : Program.Config CliOptions
|
||||
program =
|
||||
Program.config
|
||||
|> Program.add
|
||||
(OptionsParser.build CliOptions
|
||||
|> OptionsParser.with
|
||||
(Option.optionalKeywordArg "username" |> Option.withDefault "dillonkearns")
|
||||
|> OptionsParser.with
|
||||
(Option.optionalKeywordArg "repo" |> Option.withDefault "elm-pages")
|
||||
)
|
Reference in New Issue
Block a user