// @ts-check

require("dotenv").config();

const gulp = require("gulp");
const { assets } = require("./assets");
const check = require("./check");
const { clean, clear, wipe } = require("./clean");
const configure = require("./configure");
const env = require("./env");
const { favicons } = require("./favicons");
const i18n = require("./i18n");
const coreSync = require("./coreSync");
const generateRoute = require("./generate-route");
const generateComponent = require("./generate-component");
const htaccess = require("./htaccess");
const gitignore = require("./gitignore");
const pkg = require("./pkg");
const readme = require("./readme");
const setServerPort = require("./setServerPort");
const svgicons = require("./svgicons");
const watch = require("./watch");
const { ciPre, ciPost } = require("./ci");
const { tplBuild, tplWatch } = require("./tpl");

// FIXME: exit build on failure
// process.exitCode = 1;
// gulp.doneCallback = function(err) {
//   process.exit(err ? 1 : 0);
// };

// simply exposed public tasks
gulp.task(clean);
gulp.task(clear);
gulp.task(wipe);
gulp.task(coreSync);
gulp.task(generateRoute);
gulp.task(generateComponent);
gulp.task(favicons);
gulp.task(svgicons);
gulp.task(i18n);

// composed public tasks
gulp.task(
  "tpl",
  gulp.series(
    clean,
    check,
    env,
    htaccess,
    gulp.parallel(readme, pkg, gitignore, configure, tplBuild),
    i18n,
    tplWatch
  )
);

gulp.task(
  "start",
  gulp.series(
    clean,
    check,
    setServerPort,
    env,
    htaccess,
    gulp.parallel(readme, pkg, gitignore, configure, tplBuild),
    i18n,
    gulp.parallel(svgicons, favicons),
    gulp.parallel(tplWatch, assets, watch)
  )
);

gulp.task(
  "build",
  gulp.series(
    clean,
    check,
    setServerPort,
    env,
    gulp.parallel(pkg, gitignore, tplBuild),
    i18n,
    assets,
    gulp.parallel(svgicons, favicons),
    htaccess
  )
);

gulp.task("deploy", gulp.series(ciPre, "build", ciPost));

gulp.task(
  "test",
  gulp.series("build", () => {
    const { browserSyncStart } = require("../utils/browserSync");
    browserSyncStart();
  })
);

module.exports = gulp;
