#! /bin/sh # Can override this ENV variables quiet=${quiet:-0} format=${format:-"pdf"} typst_src_dir=${typst_src_dir:-"drafts"} typst_output_dir=${typst_output_dir:-"content"} blue="\033[34m" bold_blue="\033[1;34m" green="\033[32m" bold_green="\033[1;32m" yellow="\033[33m" red="\033[31m" no_color="\033[0m" colored_output() { color_name=$1 shift case $color_name in blue) color=$blue;; bold_blue) color=$bold_blue;; green) color=$green;; bold_green) color=$bold_green;; yellow) color=$yellow;; red) color=$red;; *) color=$no_color;; esac printf "${color}$@${no_color}\n" } helper_log() { if [ $quiet -gt 0 ]; then return 0 fi log_request=$1 shift case $log_request in info) log_level=$(colored_output blue INFO);; success) log_level=$(colored_output green SUCCESS);; warn) log_level=$(colored_output yellow WARNING);; testing) log_level=$(colored_output yellow TEST);; err) log_level=$(colored_output red ERROR);; fail) log_level=$(colored_output red FAILED);; *) log_level=? esac if [ $log_level != "FAILED" ] && [ $log_level != "ERROR" ]; then printf "[$log_level] $@\n" else printf "[$log_level] $@\n >&2" fi } display_help() { echo -e " $(colored_output bold_green "Usage:") $(colored_output bold_blue "helper") COMMAND or $(colored_output bold_blue "helper") > COMMAND > COMMAND > quit $(colored_output bold_green "Commands:") $(colored_output bold_blue "help") Prints help. $(colored_output bold_blue "quit") Quit helper. $(colored_output bold_blue "check") Checks that all dependencies are installed $(colored_output bold_blue "sync") git pull $(colored_output bold_blue "build") Compiles typst file in pdf. There's other formats, see ENV vars section $(colored_output bold_blue "clean") Deletes all typst compiled files of a given format $(colored_output bold_green "ENV vars:") $(colored_output bold_blue "quiet") Make the helper silent : quiet=1 ./helper COMMANDE default value : 0 $(colored_output bold_blue "format") You can change the target format of helper's commands Example : format=png ./helper build will compile all typst source code into png default value : pdf $(colored_output bold_blue "typst_src_dir") Directory with the typst source code default value : drafts $(colored_output bold_blue "typst_output_dir") Target directory with compiled typst default value : content " } greet_user() { helper_log info "hi $(whoami)" } unknown_cmd() { echo "??? get help with './helper help'" } eval_input() { case $1 in help)display_help;; quit)echo "bye $(whoami) ♥" ; exit 0;; check)check;; sync)sync;; build)build;; clean)clean;; *)unknown_cmd;; esac } check() { helper_log testing "Checking dependencies." git -v > /dev/null 2>&1 || (helper_log fail "git isn't installed ???" && exit 1) typst --version > /dev/null 2>&1 || (helper_log fail "typst isn't installed" && exit 1) helper_log success "You're all good :)" } sync() { git pull } build() { typ_files="$(find $typst_src_dir -name "*.typ")" compiled=0 for file in $typ_files; do target_path=$(echo $file | sed "s/"$typst_src_dir"/"$typst_output_dir"/g; s/\.typ$/."$format"/g") target_dir=$(dirname $target_path) if ! [ -d $target_dir ]; then mkdir -p $target_dir fi if [ -f $target_path ]; then helper_log warn "File $file is already compiled here : $target_path, aborting." else helper_log info "Compiling $file here : $target_path" typst compile $file $target_path if [ -f $target_path ]; then helper_log success "Successfully compiled $file here : $target_path ." compiled=$(expr $compiled + 1) else helper_log fail "Couldn't compile $file here : $target_path" fi fi done helper_log info "Found : $(echo "$typ_files" | wc -l) Typst files." helper_log info "Compiled : $compiled $format." } clean() { build_files="$(find $typst_output_dir -name "*.$format")" deleted=0 for file in $build_files; do helper_log info "Deleting $file" rm $file if ! [ -f $file ]; then helper_log success "Successfully deleted $file ." deleted=$(expr $deleted + 1) else helper_log fail "Couldn't delete $file ." fi done helper_log info "Found : $(echo "$build_files" | wc -l) Typst files." helper_log info "Deleted : $deleted $format." empty_dirs="$(find . -type d -empty -not -path "*/.git/*")" while [ "$empty_dirs" != "" ]; do if [ -n "$empty_dirs" ]; then helper_log warn "$(echo "$empty_dirs" | wc -l) empty dirs found." helper_log info "deleting empty dirs" for dir in $empty_dirs; do helper_log info "Deleting $dir" rmdir $dir && helper_log success "Successfully deleted $dir." || helper_log fail "Couldn't delete $dir" done fi empty_dirs="$(find . -type d -empty -not -path "*/.git/*")" done } if ! [ -d $typst_src_dir ]; then mkdir $typst_src_dir fi if ! [ -d $typst_output_dir ]; then mkdir $typst_output_dir fi if [ $# -eq 0 ]; then greet_user while true; do echo -n "> " read user_input eval_input $user_input done elif [ $# -eq 1 ]; then eval_input $1 else unknown_cmd fi