2016-02-26 15:36:00 +00:00
|
|
|
var gulp = require("gulp");
|
2017-02-09 11:45:42 +00:00
|
|
|
var gutil = require("gulp-util");
|
2016-02-26 15:36:00 +00:00
|
|
|
var clean = require("gulp-clean");
|
|
|
|
var newer = require("gulp-newer");
|
|
|
|
var combine = require("stream-combiner");
|
2017-02-10 14:36:38 +00:00
|
|
|
var fs = require("fs");
|
2017-03-04 19:35:26 +00:00
|
|
|
var Promise = require("bluebird");
|
2017-02-10 14:36:38 +00:00
|
|
|
var request = require("request-promise");
|
|
|
|
var unzip = require("unzipper");
|
2017-02-09 11:45:42 +00:00
|
|
|
var webpack = require("webpack");
|
|
|
|
|
2016-10-24 00:35:58 +00:00
|
|
|
var icons = require("./gulpfile-icons");
|
2017-02-09 11:45:42 +00:00
|
|
|
var webpackConfig = require("./webpack.config.js");
|
2016-02-26 15:36:00 +00:00
|
|
|
|
2017-02-09 11:45:42 +00:00
|
|
|
let webpackCompiler = webpack(webpackConfig);
|
2016-02-26 15:36:00 +00:00
|
|
|
|
2017-02-21 23:55:11 +00:00
|
|
|
const staticFrontendFile = `${__dirname}/build/frontend.js`;
|
|
|
|
const staticClientFile = `${__dirname}/build/client.js`;
|
|
|
|
|
|
|
|
gulp.task("default", [ "webpack", "symlinks" ]);
|
2016-02-26 15:36:00 +00:00
|
|
|
|
|
|
|
gulp.task("clean", function() {
|
|
|
|
return combine(
|
|
|
|
gulp.src("build"),
|
|
|
|
clean()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-02-10 14:36:38 +00:00
|
|
|
gulp.task("download-icons", function() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.exists("build/Open-SVG-Map-Icons", (exists) => {
|
|
|
|
resolve(exists);
|
|
|
|
});
|
|
|
|
}).then((exists) => {
|
|
|
|
if(exists)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let extract = unzip.Extract({
|
|
|
|
path: "build/"
|
|
|
|
});
|
|
|
|
|
|
|
|
let download = request.get("https://github.com/twain47/Open-SVG-Map-Icons/archive/master.zip");
|
|
|
|
download.pipe(extract);
|
|
|
|
download.catch((err) => {
|
|
|
|
extract.emit("error", err);
|
|
|
|
});
|
|
|
|
|
|
|
|
return extract.promise().then(() => {
|
2017-03-04 19:35:26 +00:00
|
|
|
return Promise.promisify(fs.rename)("build/Open-SVG-Map-Icons-master", "build/Open-SVG-Map-Icons");
|
2017-02-10 14:36:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task("icons", ["download-icons"], function() {
|
2016-10-24 00:35:58 +00:00
|
|
|
return combine(
|
2017-02-10 14:36:38 +00:00
|
|
|
gulp.src("build/Open-SVG-Map-Icons/svg/**/*.svg"),
|
2016-10-24 00:35:58 +00:00
|
|
|
newer("build/icons.js"),
|
|
|
|
icons("icons.js", "angular.module(\"facilmap\").constant(\"fmIcons\", %s);"),
|
|
|
|
gulp.dest("build")
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-02-10 14:54:11 +00:00
|
|
|
gulp.task("webpack", [ "icons" ], function() {
|
2017-03-04 19:35:26 +00:00
|
|
|
return Promise.promisify(webpackCompiler.run.bind(webpackCompiler))().then(function(stats) {
|
2017-02-10 14:54:11 +00:00
|
|
|
gutil.log("[webpack]", stats.toString());
|
|
|
|
|
|
|
|
if(stats.compilation.errors && stats.compilation.errors.length > 0)
|
|
|
|
throw new gutil.PluginError("webpack", "There were compilation errors.");
|
2017-02-21 23:55:11 +00:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.exists(staticFrontendFile, resolve);
|
|
|
|
}).then((exists) => {
|
|
|
|
if(exists)
|
2017-03-04 19:35:26 +00:00
|
|
|
return Promise.promisify(fs.unlink)(staticFrontendFile);
|
2017-02-21 23:55:11 +00:00
|
|
|
}).then(() => {
|
|
|
|
// Create symlink with fixed file name so that people can include https://facilmap.org/frontend.js
|
2017-03-04 19:35:26 +00:00
|
|
|
return Promise.promisify(fs.symlink)(`frontend-index-${stats.hash}.js`, `${__dirname}/build/frontend.js`);
|
2017-02-21 23:55:11 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task("symlinks", [ "webpack" /* To create the build directory */ ], function() {
|
|
|
|
// Create symlink to facilmap-client so that people can include https://facilmap.org/client.js
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.exists(staticClientFile, resolve);
|
|
|
|
}).then((exists) => {
|
|
|
|
if(!exists)
|
2017-03-04 19:35:26 +00:00
|
|
|
return Promise.promisify(fs.symlink)(require.resolve("facilmap-client/build/client"), staticClientFile);
|
2017-02-21 23:55:11 +00:00
|
|
|
});
|
2017-02-09 11:45:42 +00:00
|
|
|
});
|
|
|
|
|
2017-02-10 14:54:11 +00:00
|
|
|
gulp.task("watch", [ "icons" ], function() {
|
2017-02-09 11:45:42 +00:00
|
|
|
webpackCompiler.watch({
|
|
|
|
}, function(err, stats) {
|
|
|
|
gutil.log("[webpack]", err ? err : stats.toString());
|
|
|
|
});
|
|
|
|
});
|