Packages
gleam_tailwind
0.1.0
Gleam modules and functions for installing and invoking TailwindCSS
Retired package: Renamed
Current section
Files
Jump to
Current section
Files
src/tailwind.erl
-module(tailwind).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([get_args/0, run/1, install/0, install_and_run/1]).
-spec generate_config() -> {ok, nil} | {error, binary()}.
generate_config() ->
case simplifile:is_file(<<"./tailwind.config.js"/utf8>>) of
true ->
gleam@io:println(<<"TailwindCSS config already exists."/utf8>>),
{ok, nil};
false ->
_pipe = <<"
// See the Tailwind configuration guide for advanced usage
// https://tailwindcss.com/docs/configuration
let plugin = require('tailwindcss/plugin')
module.exports = {
content: ['./src/**/*.{html,gleam}'],
theme: {
extend: {},
},
plugins: [require('@tailwindcss/forms')],
}
"/utf8>>,
_pipe@1 = simplifile:write(<<"./tailwind.config.js"/utf8>>, _pipe),
_pipe@2 = gleam@result:map_error(
_pipe@1,
fun(Err) ->
<<"Error: Couldn't create tailwind config. Reason: "/utf8,
(gleam@string:inspect(Err))/binary>>
end
),
gleam@result:map(
_pipe@2,
fun(_) ->
gleam@io:println(<<"TailwindCSS config created."/utf8>>),
nil
end
)
end.
-spec get_config() -> {ok, gleam@map:map_(binary(), tom:toml())} |
{error, binary()}.
get_config() ->
_pipe = simplifile:read(<<"./gleam.toml"/utf8>>),
_pipe@1 = gleam@result:map_error(
_pipe,
fun(Err) ->
<<"Error: Couldn't read config. Reason: "/utf8,
(gleam@string:inspect(Err))/binary>>
end
),
gleam@result:'try'(_pipe@1, fun(Config) -> _pipe@2 = tom:parse(Config),
gleam@result:replace_error(
_pipe@2,
<<"Error: Couldn't parse config."/utf8>>
) end).
-spec get_config_string(binary()) -> {ok, binary()} | {error, binary()}.
get_config_string(Key) ->
_pipe = get_config(),
gleam@result:'try'(
_pipe,
fun(Parsed) ->
_pipe@1 = tom:get_string(Parsed, [<<"tailwind"/utf8>>, Key]),
gleam@result:replace_error(
_pipe@1,
<<<<"Error: Config key \""/utf8, Key/binary>>/binary,
"\" not found."/utf8>>
)
end
).
-spec get_args() -> {ok, list(binary())} | {error, binary()}.
get_args() ->
_pipe = get_config(),
gleam@result:'try'(
_pipe,
fun(Parsed) ->
_pipe@1 = tom:get_array(
Parsed,
[<<"tailwind"/utf8>>, <<"args"/utf8>>]
),
_pipe@2 = gleam@result:replace_error(
_pipe@1,
<<"Error: Config arguments not found. Is the \"args\" key set in the \"gleam.toml\"?"/utf8>>
),
gleam@result:map(
_pipe@2,
fun(Args) -> gleam@list:map(Args, fun(Arg) -> case Arg of
{string, A} ->
A;
_ ->
<<""/utf8>>
end end) end
)
end
).
-spec get_cli_path() -> binary().
get_cli_path() ->
_pipe = get_config_string(<<"path"/utf8>>),
gleam@result:unwrap(_pipe, <<"./build/bin/tailwindcss-cli"/utf8>>).
-spec run(list(binary())) -> {ok, binary()} | {error, binary()}.
run(Args) ->
Cli = get_cli_path(),
case simplifile:is_file(Cli) of
true ->
_pipe = unicode:characters_to_list(
gleam@string:join([Cli | Args], <<" "/utf8>>)
),
_pipe@1 = os:cmd(_pipe),
{ok, _pipe@1};
false ->
{error, <<"Error: TailwindCSS CLI isn't installed."/utf8>>}
end.
-spec get_tailwind_version() -> binary().
get_tailwind_version() ->
_pipe = get_config_string(<<"version"/utf8>>),
gleam@result:unwrap(_pipe, <<"3.3.5"/utf8>>).
-spec target() -> binary().
target() ->
[Arch | _] = begin
_pipe = erlang:binary_to_atom(<<"system_architecture"/utf8>>),
_pipe@1 = erlang:system_info(_pipe),
gleam@string:split(_pipe@1, <<"-"/utf8>>)
end,
Wordsize = begin
_pipe@2 = erlang:binary_to_atom(<<"wordsize"/utf8>>),
_pipe@3 = erlang:system_info(_pipe@2),
gleam@int:multiply(_pipe@3, 8)
end,
case {gleam_erlang_ffi:os_family(), Arch, Wordsize} of
{windows_nt, <<"x86_64"/utf8>>, 64} ->
<<"windows-x64.exe"/utf8>>;
{windows_nt, <<"arm"/utf8>>, 64} ->
<<"windows-arm64.exe"/utf8>>;
{darwin, <<"aarch64"/utf8>>, 64} ->
<<"macos-arm64"/utf8>>;
{darwin, <<"arm"/utf8>>, 64} ->
<<"macos-arm64"/utf8>>;
{darwin, <<"x86_64"/utf8>>, 64} ->
<<"macos-x64"/utf8>>;
{linux, <<"aarch64"/utf8>>, 64} ->
<<"linux-arm64"/utf8>>;
{linux, <<"arm"/utf8>>, 32} ->
<<"linux-armv7"/utf8>>;
{linux, <<"armv7"/utf8, _/binary>>, 32} ->
<<"linux-armv7"/utf8>>;
{linux, <<"x86_64"/utf8>>, 64} ->
<<"linux-x64"/utf8>>;
{linux, <<"amd64"/utf8>>, 64} ->
<<"linux-x64"/utf8>>;
{_, _, _} ->
<<<<<<(erlang:error(#{gleam_error => panic,
message => <<"Error: Tailwind is not available for "/utf8>>,
module => <<"tailwind"/utf8>>,
function => <<"target"/utf8>>,
line => 200}))/binary, (gleam@string:inspect(
gleam_erlang_ffi:os_family()
))/binary>>/binary, " "/utf8>>/binary, Arch/binary>>
end.
-spec download_tailwind(binary(), binary()) -> {ok, nil} | {error, binary()}.
download_tailwind(Version, Target) ->
case simplifile:is_file(<<"./build/bin/tailwindcss-cli"/utf8>>) of
true ->
gleam@io:println(<<"TailwindCSS CLI already exists."/utf8>>),
{ok, nil};
false ->
Path = gleam@string:concat(
[<<"/tailwindlabs/tailwindcss/releases/download/v"/utf8>>,
Version,
<<"/tailwindcss-"/utf8>>,
Target]
),
_assert_subject = simplifile:create_directory_all(
<<"./build/bin/"/utf8>>
),
{ok, nil} = case _assert_subject of
{ok, nil} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"tailwind"/utf8>>,
function => <<"download_tailwind"/utf8>>,
line => 220})
end,
gleam@io:println(
<<<<"Downloading TailwindCSS "/utf8, Target/binary>>/binary,
"..."/utf8>>
),
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_method(_pipe, get),
_pipe@2 = gleam@http@request:set_host(
_pipe@1,
<<"github.com"/utf8>>
),
_pipe@3 = gleam@http@request:set_path(_pipe@2, Path),
_pipe@4 = gleam@http@request:map(
_pipe@3,
fun gleam_stdlib:identity/1
),
_pipe@5 = gleam@httpc:send_bits(_pipe@4),
_pipe@6 = gleam@result:map_error(
_pipe@5,
fun(Err) ->
<<"Error: Couldn't download tailwind. Reason: "/utf8,
(gleam@string:inspect(Err))/binary>>
end
),
gleam@result:'try'(
_pipe@6,
fun(Resp) ->
_pipe@7 = simplifile:write_bits(
<<"./build/bin/tailwindcss-cli"/utf8>>,
erlang:element(4, Resp)
),
_pipe@8 = gleam@result:map_error(
_pipe@7,
fun(Err@1) ->
<<"Error: Couldn't write tailwind binary. Reason: "/utf8,
(gleam@string:inspect(Err@1))/binary>>
end
),
gleam@result:'try'(
_pipe@8,
fun(_) ->
_pipe@9 = tailwind_erl:change_file_permissions(
<<"./build/bin/tailwindcss-cli"/utf8>>,
755
),
gleam@result:map_error(
_pipe@9,
fun(Err@2) ->
<<"Error: Can't change tailwindcli permissions. Reason: "/utf8,
(gleam@string:inspect(Err@2))/binary>>
end
)
end
)
end
)
end.
-spec install() -> {ok, nil} | {error, binary()}.
install() ->
gleam@io:println(<<"Installing TailwindCSS..."/utf8>>),
Output = begin
_pipe = generate_config(),
gleam@result:'try'(
_pipe,
fun(_) ->
Version = get_tailwind_version(),
download_tailwind(Version, target())
end
)
end,
case Output of
{ok, _} ->
gleam@io:println(<<"TailwindCSS installed!"/utf8>>),
{ok, nil};
{error, Err} ->
gleam@io:println(Err),
{error, Err}
end.
-spec install_and_run(list(binary())) -> {ok, binary()} | {error, binary()}.
install_and_run(Args) ->
_pipe = install(),
gleam@result:'try'(_pipe, fun(_) -> run(Args) end).