spectabular/gulpfile.js

108 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-06-24 19:30:23 +00:00
/* jshint esversion: 6, node: true */
"use strict";
const browserify = require("browserify");
2018-06-28 23:20:13 +00:00
const chalk = require("chalk");
2018-06-24 19:30:23 +00:00
const del = require("del");
const gulp = require("gulp");
const gulpif = require("gulp-if");
const jshint = require("gulp-jshint");
const jshintStylish = require("jshint-stylish");
2018-06-28 23:20:13 +00:00
const log = require("fancy-log");
2018-06-28 23:13:29 +00:00
const plumber = require("gulp-plumber");
2018-06-24 19:30:23 +00:00
const replaceExt = require("replace-ext");
const sequence = require("run-sequence");
const sass = require("gulp-sass");
const source = require("vinyl-source-stream");
const sourcemaps = require("gulp-sourcemaps");
const tsify = require("tsify");
const tslint = require("gulp-tslint");
const path = require("path");
const yargs = require("yargs");
const TS_SRC_DIR = "src/ts/";
const TS_ENTRYPOINTS = ["browser-action.ts"];
const JS_OUT_DIR = "spectabular/js/";
const SASS_SRC_DIR = "src/scss/";
const CSS_OUT_DIR = "spectabular/css/";
const TWIG_SRC_DIR = "src/twig/";
const TWIG_OUT_DIR = "spectabular/twig/";
2018-06-24 19:30:23 +00:00
2018-06-28 23:13:29 +00:00
//Map the various directories to the gulp tasks
const WATCH_MAPPINGS = {
[TS_SRC_DIR]: ["typescript-lint", "typescript"],
[SASS_SRC_DIR]: ["sass"],
[TWIG_SRC_DIR]: ["twig"],
2018-06-28 23:13:29 +00:00
};
2018-06-24 19:30:23 +00:00
let isProdBuild = yargs.argv.hasOwnProperty("prod");
gulp.task("build", (callback) => {
sequence("gulpfile-lint", "typescript-lint", "typescript", "sass", "twig", callback);
});
gulp.task("clean", () => {
return del([
JS_OUT_DIR,
CSS_OUT_DIR
].map((folder) => {
return path.join(folder, "*");
}));
});
2018-06-28 23:13:29 +00:00
gulp.task("watch", () => {
Object.keys(WATCH_MAPPINGS).forEach((dir) => {
let globbedPath = path.join(dir, "*");
let tasks = WATCH_MAPPINGS[dir];
let watchTask = gulp.watch(globbedPath, tasks);
2018-06-28 23:13:29 +00:00
watchTask.on("change", (file) => {
let relativePath = path.relative(__dirname, file.path);
log(`[${chalk.blue(tasks.join(", "))}] Change detected: ${chalk.green(relativePath)}`);
2018-06-28 23:13:29 +00:00
});
});
});
2018-06-24 19:30:23 +00:00
gulp.task("typescript", () => {
let buildStreams = TS_ENTRYPOINTS.map((entrypoint) => {
let entrypointPath = path.join(TS_SRC_DIR, entrypoint);
let bundler = browserify(entrypointPath, {
plugin: ["tsify"],
debug: !isProdBuild
});
2018-06-28 23:13:29 +00:00
return plumber()
.pipe(bundler.bundle())
2018-06-24 19:30:23 +00:00
.pipe(source(replaceExt(entrypoint, ".js")))
.pipe(gulp.dest(JS_OUT_DIR));
});
});
gulp.task("sass", () => {
return gulp.src(path.join(SASS_SRC_DIR, "*.scss"))
2018-06-28 23:13:29 +00:00
.pipe(plumber())
2018-06-24 19:30:23 +00:00
.pipe(sourcemaps.init())
.pipe(sass().on("error", sass.logError))
.pipe(gulpif(!isProdBuild, sourcemaps.write()))
.pipe(gulp.dest(CSS_OUT_DIR));
});
gulp.task("twig", () => {
return gulp.src(path.join(TWIG_SRC_DIR, "*.{html,html.twig}"))
.pipe(gulp.dest(TWIG_OUT_DIR));
});
gulp.task("gulpfile-lint", () => {
return gulp.src("gulpfile.js")
2018-06-28 23:13:29 +00:00
.pipe(plumber())
2018-06-24 19:30:23 +00:00
.pipe(jshint())
.pipe(jshint.reporter(jshintStylish));
});
gulp.task("typescript-lint", () => {
return gulp.src(path.join(TS_SRC_DIR, "*.ts"))
2018-06-28 23:13:29 +00:00
.pipe(plumber())
2018-06-24 19:30:23 +00:00
.pipe(tslint({
formatter: "stylish"
}))
.pipe(tslint.report());
});