2020-01-11 10:28:54 -05:00
|
|
|
const fs = require("fs");
|
2020-02-25 10:18:55 -05:00
|
|
|
const path = require("path");
|
|
|
|
const l = require("lodash");
|
|
|
|
|
|
|
|
const CleanCSS = require("clean-css");
|
2020-01-20 07:03:01 -05:00
|
|
|
const gulp = require("gulp");
|
|
|
|
const gulpif = require("gulp-if");
|
|
|
|
const gzip = require("gulp-gzip");
|
2020-02-25 10:18:55 -05:00
|
|
|
|
2020-01-20 07:03:01 -05:00
|
|
|
const mustache = require("gulp-mustache");
|
|
|
|
const rename = require("gulp-rename");
|
2020-02-25 10:18:55 -05:00
|
|
|
const svgSprite = require("gulp-svg-sprite");
|
|
|
|
|
|
|
|
const mkdirp = require("mkdirp");
|
2020-01-20 07:03:01 -05:00
|
|
|
const rimraf = require("rimraf");
|
2020-02-25 10:18:55 -05:00
|
|
|
const sass = require("sass");
|
|
|
|
const autoprefixer = require('autoprefixer')
|
|
|
|
const postcss = require('postcss')
|
2016-06-11 13:43:57 -05:00
|
|
|
|
|
|
|
const paths = {};
|
2020-02-25 10:18:55 -05:00
|
|
|
paths.resources = "./resources/";
|
2016-02-21 09:42:35 -05:00
|
|
|
paths.output = "./resources/public/";
|
2020-02-25 10:18:55 -05:00
|
|
|
paths.build = "./target/build/";
|
2020-01-20 07:03:01 -05:00
|
|
|
paths.dist = "./target/dist/";
|
2020-02-25 10:18:55 -05:00
|
|
|
paths.scss = "./resources/styles/**/*.scss";
|
2015-06-18 12:35:50 -05:00
|
|
|
|
2016-06-11 13:43:57 -05:00
|
|
|
/***********************************************
|
2020-02-25 10:18:55 -05:00
|
|
|
* Helpers
|
2016-06-11 13:43:57 -05:00
|
|
|
***********************************************/
|
|
|
|
|
2016-06-14 15:50:55 -05:00
|
|
|
function isProduction() {
|
|
|
|
return (process.env.NODE_ENV === 'production');
|
|
|
|
}
|
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
function scssPipeline(options) {
|
|
|
|
const write = (_path, data) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.writeFile(_path, data, function(err) {
|
|
|
|
if (err) { reject(err); }
|
|
|
|
else { resolve(); }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
const render = (input) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
sass.render({file: input}, async function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err.formatted);
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(result.css);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
const postprocess = (data, input, output) => {
|
|
|
|
return postcss([autoprefixer])
|
|
|
|
.process(data, {map: false, from: input, to: output})
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(next) {
|
2016-06-14 15:50:55 -05:00
|
|
|
const input = options.input;
|
|
|
|
const output = options.output;
|
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
return mkdirp(path.dirname(output))
|
|
|
|
.then(() => render(input))
|
|
|
|
.then((res) => postprocess(res, input, output))
|
|
|
|
.then((res) => write(output, res))
|
|
|
|
.catch((err) => null)
|
|
|
|
.then(() => {
|
|
|
|
next();
|
|
|
|
});
|
2016-06-14 15:50:55 -05:00
|
|
|
};
|
2016-04-03 05:19:11 -05:00
|
|
|
}
|
2016-02-21 09:42:35 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
// Templates
|
2016-04-03 05:19:11 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
function readSvgSprite() {
|
|
|
|
const path = paths.build + "/icons-sprite/symbol/svg/sprite.symbol.svg";
|
|
|
|
const content = fs.readFileSync(path, {encoding: "utf8"});
|
|
|
|
return content;
|
|
|
|
}
|
2016-04-03 05:19:11 -05:00
|
|
|
|
2020-01-11 10:28:54 -05:00
|
|
|
function readLocales() {
|
|
|
|
const path = __dirname + "/resources/locales.json";
|
|
|
|
const content = JSON.parse(fs.readFileSync(path, {encoding: "utf8"}));
|
|
|
|
|
|
|
|
let result = {};
|
|
|
|
for (let key of Object.keys(content)) {
|
|
|
|
const item = content[key];
|
|
|
|
if (l.isString(item)) {
|
|
|
|
result[key] = {"en": item};
|
|
|
|
} else if (l.isPlainObject(item) && l.isPlainObject(item.translations)) {
|
|
|
|
result[key] = item.translations;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(result);
|
|
|
|
}
|
|
|
|
|
2016-06-14 15:50:55 -05:00
|
|
|
function templatePipeline(options) {
|
|
|
|
return function() {
|
|
|
|
const input = options.input;
|
|
|
|
const output = options.output;
|
|
|
|
const ts = Math.floor(new Date());
|
2020-01-11 10:28:54 -05:00
|
|
|
|
|
|
|
const locales = readLocales();
|
2020-02-25 10:18:55 -05:00
|
|
|
const icons = readSvgSprite();
|
2020-01-11 10:28:54 -05:00
|
|
|
|
2016-06-14 15:50:55 -05:00
|
|
|
const tmpl = mustache({
|
2020-01-11 10:28:54 -05:00
|
|
|
ts: ts,
|
2020-02-25 10:18:55 -05:00
|
|
|
ic: icons,
|
2020-01-11 10:28:54 -05:00
|
|
|
tr: JSON.stringify(locales),
|
2016-06-14 15:50:55 -05:00
|
|
|
});
|
2016-06-11 13:43:57 -05:00
|
|
|
|
2016-06-14 15:50:55 -05:00
|
|
|
return gulp.src(input)
|
|
|
|
.pipe(tmpl)
|
|
|
|
.pipe(rename("index.html"))
|
|
|
|
.pipe(gulp.dest(output));
|
|
|
|
};
|
|
|
|
}
|
2016-02-28 03:12:25 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
/***********************************************
|
|
|
|
* Generic
|
|
|
|
***********************************************/
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("scss:main", scssPipeline({
|
|
|
|
input: paths.resources + "styles/main.scss",
|
|
|
|
output: paths.build + "css/main.css"
|
|
|
|
}));
|
2016-06-11 13:43:57 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("scss", gulp.parallel("scss:main"));
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("svg:sprite", function() {
|
|
|
|
return gulp.src(paths.resources + "images/icons/*.svg")
|
|
|
|
.pipe(rename({prefix: 'icon-'}))
|
|
|
|
.pipe(svgSprite({mode:{symbol: {inline: true}}}))
|
|
|
|
.pipe(gulp.dest(paths.build + "icons-sprite/"));
|
2016-06-14 15:50:55 -05:00
|
|
|
});
|
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("template:main", templatePipeline({
|
|
|
|
input: paths.resources + "templates/index.mustache",
|
|
|
|
output: paths.build
|
|
|
|
}));
|
|
|
|
|
|
|
|
gulp.task("templates", gulp.series("svg:sprite", "template:main"));
|
2019-05-31 06:50:55 -05:00
|
|
|
|
2016-06-11 13:43:57 -05:00
|
|
|
/***********************************************
|
2020-02-25 10:18:55 -05:00
|
|
|
* Development
|
2016-06-11 13:43:57 -05:00
|
|
|
***********************************************/
|
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dev:clean", function(next) {
|
|
|
|
rimraf(paths.output, function() {
|
|
|
|
rimraf(paths.build, next);
|
|
|
|
});
|
2016-06-11 13:43:57 -05:00
|
|
|
});
|
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dev:copy:images", function() {
|
|
|
|
return gulp.src(paths.resources + "images/**/*")
|
|
|
|
.pipe(gulp.dest(paths.output + "images/"));
|
|
|
|
});
|
2016-06-11 13:43:57 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dev:copy:css", function() {
|
|
|
|
return gulp.src(paths.build + "css/**/*")
|
|
|
|
.pipe(gulp.dest(paths.output + "css/"));
|
|
|
|
});
|
2016-06-11 13:43:57 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dev:copy:icons-sprite", function() {
|
|
|
|
return gulp.src(paths.build + "icons-sprite/**/*")
|
|
|
|
.pipe(gulp.dest(paths.output + "icons-sprite/"));
|
|
|
|
});
|
2016-02-21 09:42:35 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dev:copy:templates", function() {
|
|
|
|
return gulp.src(paths.build + "index.html")
|
|
|
|
.pipe(gulp.dest(paths.output));
|
|
|
|
});
|
2016-02-21 13:42:01 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dev:copy:fonts", function() {
|
|
|
|
return gulp.src(paths.resources + "fonts/**/*")
|
|
|
|
.pipe(gulp.dest(paths.output + "fonts/"));
|
|
|
|
});
|
2016-06-11 13:43:57 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dev:copy", gulp.parallel("dev:copy:images",
|
|
|
|
"dev:copy:css",
|
|
|
|
"dev:copy:fonts",
|
|
|
|
"dev:copy:icons-sprite",
|
|
|
|
"dev:copy:templates"));
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dev:dirs", function(next) {
|
|
|
|
mkdirp("./resources/public/css/")
|
|
|
|
.then(() => next())
|
|
|
|
});
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("watch:main", function() {
|
|
|
|
gulp.watch(paths.scss, gulp.series("scss", "dev:copy:css"));
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.watch([paths.resources + "templates/*.mustache",
|
|
|
|
paths.resources + "locales.json",
|
|
|
|
paths.resources + "images/**/*"],
|
|
|
|
gulp.series("templates", "dev:copy:images", "dev:copy:icons-sprite"));
|
|
|
|
});
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("watch", gulp.series(
|
|
|
|
"dev:dirs",
|
|
|
|
gulp.parallel("scss", "templates"),
|
|
|
|
"dev:copy",
|
|
|
|
"watch:main"
|
|
|
|
));
|
|
|
|
|
|
|
|
/***********************************************
|
|
|
|
* Production
|
|
|
|
***********************************************/
|
|
|
|
|
|
|
|
gulp.task("dist:clean", function(next) {
|
|
|
|
rimraf(paths.dist, function() {
|
|
|
|
rimraf(paths.build, next);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task("dist:copy:templates", function() {
|
|
|
|
return gulp.src(paths.build + "index.html")
|
|
|
|
.pipe(gulp.dest(paths.dist));
|
2016-02-21 11:01:27 -05:00
|
|
|
});
|
|
|
|
|
2016-06-14 15:50:55 -05:00
|
|
|
gulp.task("dist:copy:images", function() {
|
2020-02-25 10:18:55 -05:00
|
|
|
return gulp.src(paths.resources + "images/**/*")
|
2016-06-14 15:50:55 -05:00
|
|
|
.pipe(gulp.dest(paths.dist + "images/"));
|
2016-02-21 09:42:35 -05:00
|
|
|
});
|
2015-06-18 12:35:50 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dist:copy:styles", function() {
|
|
|
|
return gulp.src(paths.build + "css/**/*")
|
|
|
|
.pipe(gulp.dest(paths.dist + "css/"));
|
|
|
|
});
|
2016-06-11 13:43:57 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dist:copy:icons-sprite", function() {
|
|
|
|
return gulp.src(paths.build + "icons-sprite/**/*")
|
|
|
|
.pipe(gulp.dest(paths.dist + "icons-sprite/"));
|
|
|
|
});
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dist:copy:fonts", function() {
|
|
|
|
return gulp.src(paths.resources + "/fonts/**/*")
|
|
|
|
.pipe(gulp.dest(paths.dist + "fonts/"));
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task("dist:copy", gulp.parallel("dist:copy:fonts",
|
|
|
|
"dist:copy:icons-sprite",
|
|
|
|
"dist:copy:styles",
|
|
|
|
"dist:copy:templates",
|
|
|
|
"dist:copy:images"));
|
2017-02-21 07:23:15 -05:00
|
|
|
|
|
|
|
gulp.task("dist:gzip", function() {
|
|
|
|
return gulp.src(`${paths.dist}**/!(*.gz|*.br|*.jpg|*.png)`)
|
|
|
|
.pipe(gzip({gzipOptions: {level: 9}}))
|
|
|
|
.pipe(gulp.dest(paths.dist));
|
|
|
|
});
|
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dist", gulp.series(
|
|
|
|
"dist:clean",
|
|
|
|
"scss",
|
|
|
|
"templates",
|
|
|
|
"dist:copy"
|
2019-05-31 06:50:55 -05:00
|
|
|
));
|