/**
 * DUET GULP CONFIGURATION
 */
const gulp = require("gulp")
const sass = require("gulp-sass")
const concat = require("gulp-concat")
const rename = require("gulp-rename")
const terser = require("gulp-terser")
const log = require("fancy-log")
const color = require("ansi-colors")

let baseHref
if (process.env.DUET_TOKEN) {
  baseHref = "./dist/" + process.env.DUET_TOKEN + "/"
} else {
  baseHref = "./dist/"
}

// A Task that generates JavaScript bundle
gulp.task("js", function () {
  return (
    gulp
      // You can add more scripts below if necessary
      .src(["./src/js/scripts.js"])
      .pipe(concat("scripts.concat.js"))
      .pipe(gulp.dest(baseHref + "js"))
      .pipe(rename("scripts.min.js"))
      .pipe(terser())
      .on("error", function (err) {
        log(color.red("[Error]"), err.toString())
      })
      .pipe(gulp.dest(baseHref + "js"))
  )
})

// A Task that generates CSS from Sass
gulp.task("sass", function () {
  return gulp
    .src("./src/css/**/*.scss")
    .pipe(
      sass({
        outputStyle: "compressed",
      }).on("error", sass.logError)
    )
    .pipe(gulp.dest(baseHref + "css"))
})

// Define watch tasks
gulp.task("sass:watch", function () {
  gulp.watch("./src/css/**/*.scss", gulp.series("sass"))
})
gulp.task("js:watch", function () {
  gulp.watch("./src/js/**/*.js", gulp.series("js"))
})

// Define the default task
gulp.task("default", gulp.parallel("sass", "js", "sass:watch", "js:watch"))

// Define the build task
gulp.task("build", gulp.parallel("sass", "js"))
