spectabular/gulpfile.js

143 lines
3.7 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 eslint = require("gulp-eslint");
2018-06-24 19:30:23 +00:00
const gulp = require("gulp");
const gulpif = require("gulp-if");
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 sass = require("gulp-sass");
const source = require("vinyl-source-stream");
const sourcemaps = require("gulp-sourcemaps");
const streamToPromise = require("stream-to-promise");
2018-06-24 19:30:23 +00:00
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-07-09 08:19:52 +00:00
const FONT_AWESOME_BASE_DIR = "node_modules/@fortawesome/fontawesome-free/";
const FONT_AWESOME_OUT_DIR = "spectabular/webfonts";
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 = {
2018-07-27 03:24:56 +00:00
[TS_SRC_DIR]: ["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");
2018-07-27 03:24:56 +00:00
function buildAll(done) {
return gulp.series(
"lint-gulpfile",
gulp.parallel(
"typescript",
"sass",
"twig",
"fontawesome"
)
)(done);
}
2018-06-24 19:30:23 +00:00
2018-07-27 03:24:56 +00:00
function clean() {
2018-06-24 19:30:23 +00:00
return del([
JS_OUT_DIR,
2018-07-09 07:43:04 +00:00
CSS_OUT_DIR,
2018-07-09 08:19:52 +00:00
TWIG_OUT_DIR,
FONT_AWESOME_OUT_DIR
2018-06-24 19:30:23 +00:00
].map((folder) => {
return path.join(folder, "*");
}));
2018-07-27 03:24:56 +00:00
}
2018-06-24 19:30:23 +00:00
2018-07-27 03:24:56 +00:00
function watch(done) {
2018-06-28 23:13:29 +00:00
Object.keys(WATCH_MAPPINGS).forEach((dir) => {
let globbedPath = path.join(dir, "*");
let tasks = WATCH_MAPPINGS[dir];
let watchTask = gulp.watch(globbedPath, () => sequence(...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-07-27 03:24:56 +00:00
done();
};
function lintGulpfile() {
return gulp.src("gulpfile.js")
.pipe(plumber())
.pipe(eslint())
.pipe(eslint.format("stylish"))
.pipe(eslint.failAfterError());
}
function lintTypescript() {
return gulp.src(path.join(TS_SRC_DIR, "*.ts"))
.pipe(plumber())
.pipe(tslint({
formatter: "stylish"
}))
.pipe(tslint.report());
}
2018-06-28 23:13:29 +00:00
2018-07-27 03:24:56 +00:00
function compileTypescript(done) {
let promises = TS_ENTRYPOINTS.map(async (entrypoint) => {
2018-06-24 19:30:23 +00:00
let entrypointPath = path.join(TS_SRC_DIR, entrypoint);
2018-07-24 04:43:37 +00:00
let bundler = browserify(entrypointPath, {debug: !isProdBuild})
.plugin("tsify", {target: "ES2017"});
let stream = plumber()
2018-06-28 23:13:29 +00:00
.pipe(bundler.bundle())
2018-06-24 19:30:23 +00:00
.pipe(source(replaceExt(entrypoint, ".js")))
.pipe(gulp.dest(JS_OUT_DIR));
return streamToPromise(stream);
});
Promise.all(promises).then(() => {
2018-07-27 03:24:56 +00:00
done();
2018-06-24 19:30:23 +00:00
});
2018-07-27 03:24:56 +00:00
}
2018-06-24 19:30:23 +00:00
2018-07-27 03:24:56 +00:00
function compileSass() {
2018-06-24 19:30:23 +00:00
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())
2018-06-30 02:32:57 +00:00
.pipe(sass({
includePaths: ["node_modules/"]
}).on("error", sass.logError))
2018-06-24 19:30:23 +00:00
.pipe(gulpif(!isProdBuild, sourcemaps.write()))
.pipe(gulp.dest(CSS_OUT_DIR));
2018-07-27 03:24:56 +00:00
}
2018-06-24 19:30:23 +00:00
2018-07-27 03:24:56 +00:00
function copyTwig() {
2018-06-24 19:30:23 +00:00
return gulp.src(path.join(TWIG_SRC_DIR, "*.{html,html.twig}"))
.pipe(gulp.dest(TWIG_OUT_DIR));
2018-07-27 03:24:56 +00:00
}
2018-06-24 19:30:23 +00:00
2018-07-27 03:24:56 +00:00
function copyFontawesome() {
2018-07-09 08:19:52 +00:00
return gulp.src([
path.join(FONT_AWESOME_BASE_DIR, "webfonts/*"),
path.join(FONT_AWESOME_BASE_DIR, "LICENSE.txt")
])
.pipe(gulp.dest(FONT_AWESOME_OUT_DIR));
2018-07-27 03:24:56 +00:00
}
gulp.task("typescript", gulp.series(lintTypescript, compileTypescript));
gulp.task("sass", compileSass);
gulp.task("twig", copyTwig);
gulp.task("fontawesome", copyFontawesome);
gulp.task("lint-gulpfile", lintGulpfile);
gulp.task(watch);
gulp.task("default", buildAll);
gulp.task(clean);