Packages

đŸ›’ A small library of helper functions, that works as a companion of gleam_stdlib

Current section

Files

Jump to
gxyz src gxyz@cli.erl
Raw

src/gxyz@cli.erl

-module(gxyz@cli).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/gxyz/cli.gleam").
-export([strip_js_from_argv/1, hard_fail/1, hard_fail_with_msg/2, hard_fail_with_opts/3]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/gxyz/cli.gleam", 8).
?DOC(" strip js/ts files from argv (often present in node and deno implementation)\n").
-spec strip_js_from_argv(list(binary())) -> list(binary()).
strip_js_from_argv(Args) ->
_pipe = Args,
_pipe@1 = gxyz@list:reject(
_pipe,
fun(_capture) ->
gleam_stdlib:string_ends_with(_capture, <<".js"/utf8>>)
end
),
_pipe@2 = gxyz@list:reject(
_pipe@1,
fun(_capture@1) ->
gleam_stdlib:string_ends_with(_capture@1, <<".mjs"/utf8>>)
end
),
_pipe@3 = gxyz@list:reject(
_pipe@2,
fun(_capture@2) ->
gleam_stdlib:string_ends_with(_capture@2, <<".cjs"/utf8>>)
end
),
_pipe@4 = gxyz@list:reject(
_pipe@3,
fun(_capture@3) ->
gleam_stdlib:string_ends_with(_capture@3, <<".ts"/utf8>>)
end
),
_pipe@5 = gxyz@list:reject(
_pipe@4,
fun(_capture@4) ->
gleam_stdlib:string_ends_with(_capture@4, <<".mts"/utf8>>)
end
),
gxyz@list:reject(
_pipe@5,
fun(_capture@5) ->
gleam_stdlib:string_ends_with(_capture@5, <<".cts"/utf8>>)
end
).
-file("src/gxyz/cli.gleam", 18).
-spec do_hard_fail(
{ok, EKL} | {error, any()},
gleam@option:option(binary()),
gleam@option:option(integer())
) -> EKL.
do_hard_fail(Res, Msg, Status_code) ->
case Res of
{ok, A} ->
A;
{error, _} ->
gleam@option:map(Msg, fun gleam_stdlib:print_error/1),
_pipe = Status_code,
_pipe@1 = gleam@option:unwrap(_pipe, 1),
erlang:halt(_pipe@1),
erlang:error(#{gleam_error => panic,
message => <<"Unreachable, please create an issue in https://github.com/bwireman/gxyz if you see this"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"gxyz/cli"/utf8>>,
function => <<"do_hard_fail"/utf8>>,
line => 31})
end.
-file("src/gxyz/cli.gleam", 37).
?DOC(" Hard exit the program if the result is an Error and return the value if an Ok, exit 1, without a message\n").
-spec hard_fail({ok, EKR} | {error, any()}) -> EKR.
hard_fail(Res) ->
do_hard_fail(Res, none, none).
-file("src/gxyz/cli.gleam", 42).
?DOC(" Hard exit the program if the result is an Error and return the value if an Ok, with the exit 1 and printing msg to stderror\n").
-spec hard_fail_with_msg({ok, EKV} | {error, any()}, binary()) -> EKV.
hard_fail_with_msg(Res, Msg) ->
do_hard_fail(Res, {some, Msg}, none).
-file("src/gxyz/cli.gleam", 47).
?DOC(" Hard exit the program if the result is an Error and return the value if an Ok, with the exit code and printing msg to stderror\n").
-spec hard_fail_with_opts({ok, EKZ} | {error, any()}, binary(), integer()) -> EKZ.
hard_fail_with_opts(Res, Msg, Status_code) ->
do_hard_fail(Res, {some, Msg}, {some, Status_code}).