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");
|
2020-04-13 05:03:56 -05:00
|
|
|
const autoprefixer = require("autoprefixer")
|
|
|
|
const postcss = require("postcss")
|
2016-06-11 13:43:57 -05:00
|
|
|
|
2020-06-02 09:14:23 -05:00
|
|
|
const mapStream = require("map-stream");
|
|
|
|
|
|
|
|
|
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-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() {
|
2020-04-13 05:03:56 -05:00
|
|
|
return (process.env.NODE_ENV === "production");
|
2016-06-14 15:50:55 -05:00
|
|
|
}
|
|
|
|
|
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-06-05 07:14:49 -05:00
|
|
|
const touch = (_path) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return fs.utimes(file.path, new Date(), new Date(), () => {
|
|
|
|
resolve(_path);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
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))
|
2020-06-05 07:14:49 -05:00
|
|
|
.then(async (res) => {
|
|
|
|
await write(output, res);
|
|
|
|
await touch(output);
|
|
|
|
return res;
|
|
|
|
})
|
2020-02-25 10:18:55 -05:00
|
|
|
.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-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);
|
|
|
|
}
|
|
|
|
|
2020-04-18 13:34:34 -05:00
|
|
|
function readConfig(data) {
|
2020-05-26 05:47:09 -05:00
|
|
|
const googleClientID = process.env.UXBOX_GOOGLE_CLIENT_ID;
|
|
|
|
const publicURI = process.env.UXBOX_PUBLIC_URI;
|
2020-03-11 03:20:12 -05:00
|
|
|
const demoWarn = process.env.UXBOX_DEMO_WARNING;
|
2020-03-12 06:52:37 -05:00
|
|
|
const deployDate = process.env.UXBOX_DEPLOY_DATE;
|
|
|
|
const deployCommit = process.env.UXBOX_DEPLOY_COMMIT;
|
2020-03-11 03:20:12 -05:00
|
|
|
|
2020-03-11 11:21:07 -05:00
|
|
|
let cfg = {
|
|
|
|
demoWarning: demoWarn === "true"
|
|
|
|
};
|
|
|
|
|
2020-05-26 05:47:09 -05:00
|
|
|
if (googleClientID !== undefined) {
|
|
|
|
cfg.googleClientID = googleClientID;
|
2020-03-11 11:21:07 -05:00
|
|
|
}
|
|
|
|
|
2020-05-26 05:47:09 -05:00
|
|
|
if (publicURI !== undefined) {
|
|
|
|
cfg.publicURI = publicURI;
|
|
|
|
}
|
|
|
|
|
2020-03-12 06:52:37 -05:00
|
|
|
if (deployDate !== undefined) {
|
|
|
|
cfg.deployDate = deployDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deployCommit !== undefined) {
|
|
|
|
cfg.deployCommit = deployCommit;
|
|
|
|
}
|
|
|
|
|
2020-04-18 13:34:34 -05:00
|
|
|
Object.assign(cfg, data);
|
|
|
|
|
2020-03-11 11:21:07 -05:00
|
|
|
return JSON.stringify(cfg);
|
2020-03-11 03:20:12 -05:00
|
|
|
}
|
|
|
|
|
2020-06-02 09:14:23 -05:00
|
|
|
const defaultManifest = {
|
|
|
|
"main": "/js/main.js",
|
|
|
|
"shared": "/js/shared.js",
|
|
|
|
"worker": "js/worker.js"
|
|
|
|
};
|
|
|
|
|
|
|
|
function readManifest() {
|
|
|
|
try {
|
|
|
|
const path = __dirname + "/resources/public/js/manifest.json";
|
|
|
|
const content = JSON.parse(fs.readFileSync(path, {encoding: "utf8"}));
|
|
|
|
|
|
|
|
const index = {};
|
|
|
|
for (let item of content) {
|
|
|
|
index[item.name] = "/js/" + item["output-name"];
|
|
|
|
};
|
|
|
|
|
|
|
|
return index;
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error on reading manifest, using default.");
|
|
|
|
return defaultManifest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function touch() {
|
|
|
|
return mapStream(function(file, cb) {
|
|
|
|
if (file.isNull()) {
|
|
|
|
return cb(null, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update file modification and access time
|
|
|
|
return fs.utimes(file.path, new Date(), new Date(), () => {
|
|
|
|
cb(null, file)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-04-13 05:03:56 -05:00
|
|
|
|
|
|
|
const th = process.env.UXBOX_THEME || "default";
|
|
|
|
const themes = ["default"];
|
2020-01-11 10:28:54 -05:00
|
|
|
|
|
|
|
const locales = readLocales();
|
2020-06-02 09:14:23 -05:00
|
|
|
const manifest = readManifest();
|
|
|
|
const config = readConfig({themes, manifest});
|
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-04-08 03:57:29 -05:00
|
|
|
th: th,
|
2020-06-02 09:14:23 -05:00
|
|
|
manifest: manifest,
|
2020-03-11 03:20:12 -05:00
|
|
|
config: JSON.stringify(config),
|
|
|
|
translations: JSON.stringify(locales),
|
2020-04-08 03:57:29 -05:00
|
|
|
themes: JSON.stringify(themes),
|
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"))
|
2020-06-02 09:14:23 -05:00
|
|
|
.pipe(gulp.dest(output))
|
|
|
|
.pipe(touch());
|
2016-06-14 15:50:55 -05:00
|
|
|
};
|
|
|
|
}
|
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-04-13 05:03:56 -05:00
|
|
|
gulp.task("scss:main-default", scssPipeline({
|
|
|
|
input: paths.resources + "styles/main-default.scss",
|
|
|
|
output: paths.output + "css/main-default.css"
|
2020-04-08 03:57:29 -05:00
|
|
|
}));
|
|
|
|
|
2020-04-13 05:03:56 -05:00
|
|
|
gulp.task("scss", gulp.parallel("scss:main-default"));
|
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")
|
2020-04-13 05:03:56 -05:00
|
|
|
.pipe(rename({prefix: "icon-"}))
|
2020-03-30 16:56:15 -05:00
|
|
|
.pipe(svgSprite({mode:{symbol: {inline: false}}}))
|
|
|
|
.pipe(gulp.dest(paths.output + "images/svg-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",
|
2020-03-30 16:56:15 -05:00
|
|
|
output: paths.output
|
2020-02-25 10:18:55 -05:00
|
|
|
}));
|
|
|
|
|
2020-03-30 16:56:15 -05:00
|
|
|
gulp.task("templates", gulp.series("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-04-18 13:34:34 -05:00
|
|
|
gulp.task("clean", function(next) {
|
2020-03-30 16:56:15 -05:00
|
|
|
rimraf(paths.output, next);
|
2016-06-11 13:43:57 -05:00
|
|
|
});
|
|
|
|
|
2020-04-18 13:34:34 -05:00
|
|
|
gulp.task("copy:assets:images", function() {
|
2020-02-25 10:18:55 -05:00
|
|
|
return gulp.src(paths.resources + "images/**/*")
|
|
|
|
.pipe(gulp.dest(paths.output + "images/"));
|
|
|
|
});
|
2016-06-11 13:43:57 -05:00
|
|
|
|
2020-04-18 13:34:34 -05:00
|
|
|
gulp.task("copy:assets:fonts", function() {
|
2020-02-25 10:18:55 -05:00
|
|
|
return gulp.src(paths.resources + "fonts/**/*")
|
|
|
|
.pipe(gulp.dest(paths.output + "fonts/"));
|
|
|
|
});
|
2016-06-11 13:43:57 -05:00
|
|
|
|
2020-04-18 13:34:34 -05:00
|
|
|
gulp.task("copy:assets", gulp.parallel("copy:assets:images", "copy:assets:fonts"));
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("dev:dirs", function(next) {
|
2020-03-30 16:56:15 -05:00
|
|
|
mkdirp("./resources/public/css/").then(() => next())
|
2020-02-25 10:18:55 -05:00
|
|
|
});
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("watch:main", function() {
|
2020-03-30 16:56:15 -05:00
|
|
|
gulp.watch(paths.scss, gulp.series("scss"));
|
|
|
|
gulp.watch(paths.resources + "images/**/*",
|
2020-04-18 13:34:34 -05:00
|
|
|
gulp.series("svg:sprite", "copy:assets:images"));
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.watch([paths.resources + "templates/*.mustache",
|
2020-03-30 16:56:15 -05:00
|
|
|
paths.resources + "locales.json"],
|
|
|
|
gulp.series("templates"));
|
2020-02-25 10:18:55 -05:00
|
|
|
});
|
2016-06-14 15:50:55 -05:00
|
|
|
|
2020-04-18 13:34:34 -05:00
|
|
|
gulp.task("build", gulp.parallel("scss", "svg:sprite", "templates", "copy:assets"));
|
|
|
|
|
2020-02-25 10:18:55 -05:00
|
|
|
gulp.task("watch", gulp.series(
|
|
|
|
"dev:dirs",
|
2020-04-18 13:34:34 -05:00
|
|
|
"build",
|
2020-02-25 10:18:55 -05:00
|
|
|
"watch:main"
|
|
|
|
));
|
|
|
|
|
|
|
|
/***********************************************
|
|
|
|
* Production
|
|
|
|
***********************************************/
|
|
|
|
|
|
|
|
gulp.task("dist:clean", function(next) {
|
2020-03-30 16:56:15 -05:00
|
|
|
rimraf(paths.dist, next);
|
2020-02-25 10:18:55 -05:00
|
|
|
});
|
|
|
|
|
2020-03-30 16:56:15 -05:00
|
|
|
gulp.task("dist:copy", function() {
|
|
|
|
return gulp.src(paths.output + "**/*")
|
2020-02-25 10:18:55 -05:00
|
|
|
.pipe(gulp.dest(paths.dist));
|
2016-02-21 11:01:27 -05:00
|
|
|
});
|
|
|
|
|
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));
|
|
|
|
});
|