#! /usr/bin/env Rscript
library(sass)
library(fs)
library(purrr)
library(cliapp)

source("R/make_functions.R")

dir_create("dist")

cli_alert_info("Prepping base16 themes...")
dir_ls("src/palettes/base16/", regexp = "_base16.+scss") %>% 
  walk(base16_make_theme)

cli_alert_info("Compiling themes...")
dir_ls("src", regexp = "^[^_].+\\.scss") %>%
  walk(render_sass)

cli_alert_success('Installing themes to {path {path_home_r(".R", "rstudio", "themes")}}')
dir_ls("dist", regexp = "\\.rstheme") %>%
  walk(cp_to_rstudio)

current_theme <- if (rstudioapi::hasFun("getThemeInfo")) {
  rstudioapi::getThemeInfo()$editor
} else ""

if (current_theme != "" && current_theme %in% list_sassy_themes()) {
  cli_alert_success("Reloading {strong {current_theme}}")
  rstudioapi::applyTheme(current_theme)
} else {
  cli_alert_info("Use {code {fun list_sassy_themes}} to list Sassy RStudio themes")
  cli_alert_info("Use {code {fun rstudioapi::applyTheme}} to enable the above themes")
}
