-module(tale@cli). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/tale/cli.gleam"). -export([cli/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " # Tale CLI\n" " ## Available Commands:\n" " - `serve`: Serve a site and rebuild when changes are detected.\n" " - `build`: Build a site into static HTML files.\n" " - `new`: Create sites or themes via subcommands.\n" " - `new site`: Create a new blog or site. (eg: `tale new site my-blog`)\n" " - `new theme`: Create a new theme. (eg: `tale new theme my-theme`)\n" " - `new post`: Create a new post. (eg: `tale new post post_name_needs_underscores`)\n" " - `version`: Display the version of Tale.\n" ). -file("src/tale/cli.gleam", 47). ?DOC(" Initialize the new command.\n"). -spec new_command() -> glint:command(nil). new_command() -> glint:command_help( <<"Create sites, themes or posts via subcommands."/utf8>>, fun() -> glint:command( fun(_, _, _) -> begin gleam_stdlib:println( <<"Use `tale new site|theme|post `."/utf8>> ), nil end end ) end ). -file("src/tale/cli.gleam", 131). ?DOC(" Builds the static files into a directory(default:public)\n"). -spec build_command() -> glint:command(nil). build_command() -> glint:command_help( <<"Build the static files into the public directory."/utf8>>, fun() -> glint:command( fun(_, _, _) -> begin case tale@site:build_site() of {ok, Messages} -> gleam@list:each( Messages, fun gleam_stdlib:println/1 ); {error, Problem} -> gleam_stdlib:println( <<"Build failed: "/utf8, Problem/binary>> ) end, nil end end ) end ). -file("src/tale/cli.gleam", 144). ?DOC(" Prints the version\n"). -spec version_command(binary()) -> glint:command(nil). version_command(Version) -> glint:command_help( <<"Print the Tale CLI version."/utf8>>, fun() -> glint:command( fun(_, _, _) -> begin gleam_stdlib:println(<<"Tale v"/utf8, Version/binary>>), nil end end ) end ). -file("src/tale/cli.gleam", 154). ?DOC(" Prints messages of generators\n"). -spec print_result({ok, binary()} | {error, binary()}) -> nil. print_result(Outcome) -> case Outcome of {ok, Message} -> gleam_stdlib:println(Message); {error, Problem} -> gleam_stdlib:println(<<"Error: "/utf8, Problem/binary>>) end. -file("src/tale/cli.gleam", 58). ?DOC( " Command that generates a new site/blog\n" " See generators.gleam for details\n" ). -spec new_site_command() -> glint:command(nil). new_site_command() -> glint:command_help( <<"Create a new blog or site. Usage: tale new site "/utf8>>, fun() -> glint:unnamed_args( {eq_args, 1}, fun() -> glint:command( fun(_, Args, _) -> begin case Args of [Name] -> print_result( tale@generators:new_site_gen(Name) ); _ -> gleam_stdlib:println( <<"Missing site name. Run `tale new site `."/utf8>> ) end, nil end end ) end ) end ). -file("src/tale/cli.gleam", 75). ?DOC( " Command that generates a new theme.\n" " See generators.gleam for details\n" ). -spec new_theme_command() -> glint:command(nil). new_theme_command() -> glint:command_help( <<"Create a new theme inside themes/. Usage: tale new theme "/utf8>>, fun() -> glint:unnamed_args( {eq_args, 1}, fun() -> glint:command( fun(_, Args, _) -> begin case Args of [Name] -> print_result( tale@generators:new_theme_gen(Name) ); _ -> gleam_stdlib:println( <<"Missing theme name. Run `tale new theme `."/utf8>> ) end, nil end end ) end ) end ). -file("src/tale/cli.gleam", 91). ?DOC(" Command that generates a new Markdown post from the archetype.\n"). -spec new_post_command() -> glint:command(nil). new_post_command() -> glint:command_help( <<"Create a new post from archetypes/default.md. Usage: tale new post "/utf8>>, fun() -> glint:unnamed_args( {eq_args, 1}, fun() -> glint:command( fun(_, Args, _) -> begin case Args of [Name] -> print_result( tale@generators:new_post_gen(Name) ); _ -> gleam_stdlib:println( <<"Missing post name. Run `tale new post `."/utf8>> ) end, nil end end ) end ) end ). -file("src/tale/cli.gleam", 162). ?DOC(" Starts the development server\n"). -spec start_server(integer()) -> nil. start_server(Port) -> gleam_stdlib:println( <<<<"Starting Tale dev server on http://localhost:"/utf8, (erlang:integer_to_binary(Port))/binary>>/binary, " (Ctrl+C to stop)..."/utf8>> ), tale@server:serve(Port). -file("src/tale/cli.gleam", 107). ?DOC(" Triggers a simple static server for development purposes\n"). -spec serve_command() -> glint:command(nil). serve_command() -> glint:command_help( <<"Serve a site with a basic server. Usage: tale serve [port]"/utf8>>, fun() -> glint:command( fun(_, Args, _) -> begin case Args of [] -> start_server(8000); [Port_str] -> case gleam_stdlib:parse_int(Port_str) of {ok, Port} -> start_server(Port); {error, _} -> gleam_stdlib:println( <<<<"Invalid port value \""/utf8, Port_str/binary>>/binary, "\". Please pass a number."/utf8>> ) end; _ -> gleam_stdlib:println( <<"Provide at most one port value, e.g. `tale serve 4500`."/utf8>> ) end, nil end end ) end ). -file("src/tale/cli.gleam", 23). ?DOC(" Initialize the CLI application.\n"). -spec cli(binary()) -> nil. cli(Version) -> Args = case erlang:element(4, argv:load()) of [<<"help"/utf8>> | Rest] -> lists:append(Rest, [<<"--help"/utf8>>]); [] -> [<<"--help"/utf8>>]; Other -> Other end, App = begin _pipe = glint:new(), _pipe@1 = glint:pretty_help(_pipe, glint:default_pretty_help()), _pipe@2 = glint:with_name(_pipe@1, <<"tale"/utf8>>), _pipe@3 = glint:global_help( _pipe@2, <<"\x{001b}[1;3mTale - A static story telling generator\x{001b}[0m"/utf8>> ), _pipe@4 = glint:add( _pipe@3, [<<"version"/utf8>>], version_command(Version) ), _pipe@5 = glint:add(_pipe@4, [<<"new"/utf8>>], new_command()), _pipe@6 = glint:add( _pipe@5, [<<"new"/utf8>>, <<"site"/utf8>>], new_site_command() ), _pipe@7 = glint:add( _pipe@6, [<<"new"/utf8>>, <<"theme"/utf8>>], new_theme_command() ), _pipe@8 = glint:add( _pipe@7, [<<"new"/utf8>>, <<"post"/utf8>>], new_post_command() ), _pipe@9 = glint:add(_pipe@8, [<<"serve"/utf8>>], serve_command()), glint:add(_pipe@9, [<<"build"/utf8>>], build_command()) end, glint:run_and_handle(App, Args, fun(_) -> nil end).