#!/usr/bin/env Rscript

# This script downloads strftime-min.js from its GitHub repository,
# https://github.com/samsonjs/strftime

# This script can be sourced from RStudio, or run with Rscript.

version <- "0.9.2"
ref <- paste0("v", version)
destdir <- rprojroot::find_package_root_file("inst", "www", "shared", "strftime")

download.file(
  paste0("https://raw.githubusercontent.com/samsonjs/strftime/", ref, "/strftime-min.js"),
  destfile = file.path(destdir, "strftime-min.js")
)

writeLines(
  c(
    "# Generated by tools/updateStrftime.R; do not edit by hand",
    sprintf('version_strftime <- "%s"', version)
  ),
  rprojroot::find_package_root_file("R", "version_strftime.R")
)

# Update TypeScript installation
withr::with_dir(
  rprojroot::find_package_root_file(),
  {
    exit_code <- system(paste0("yarn add strftime@", version))
    if (exit_code != 0) stop("yarn could not install strftime")
  }
)
