-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
351 lines (309 loc) · 10.2 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
'use strict';
// resources to look
// https://cookiecutter-django-gulp.readthedocs.io/en/latest/gulp-tasks.html#gulp-tasks
// https://lincolnloop.com/blog/integrating-front-end-tools-your-django-project/
// https://blog.mozilla.org/webdev/2016/05/27/django-pipeline-and-gulp/
// https://gist.github.com/soin08/4793992d8cc537f62df3
// Load plugins
const gulp = require('gulp'),
browsersync = require('browser-sync').create(),
cleanCSS = require('gulp-clean-css'),
sourcemaps = require('gulp-sourcemaps'),
del = require('del'),
merge = require('merge-stream'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
babel = require('gulp-babel'),
{ sass } = require("@mr-hope/gulp-sass"),
autoprefixer = require("gulp-autoprefixer"),
// exec = require('child_process').exec,
// spawn = require('child_process').spawn,
run = require('gulp-run-command').default,
fs = require('fs'),
pkg = require('./package.json');
// Relative paths function
const pathsConfig = function () {
let root = __dirname;
let dist = root + '/static';
let cssFolder = dist + '/css';
let scssFolder = root + '/scss';
let jsFolder = dist + '/js';
return {
root: root,
html: root + '/templates/**/*.html',
cssDir: cssFolder,
css: cssFolder + '/**/*.css',
cssMin: cssFolder + '/**/*.min.css',
scssDir: scssFolder,
scss: scssFolder + '/**/*.scss',
jsDir: jsFolder,
js: jsFolder + '/**/*.js',
jsMin: jsFolder + '/**/*.min.js',
dist: dist,
vendor: dist + '/vendor',
js9Target: dist + '/vendor/js9',
}
};
const paths = pathsConfig();
// Debug task
function debug(cb) {
console.log(paths)
return cb();
}
// // Run django server
// function runServer() {
// return exec('python manage.py runserver', function (err, stdout, stderr) {
// console.log(stdout);
// console.log(stderr);
// });
// };
// JS9 tasks
function js9Dir() {
var mkDir = run('mkdir -p ' + paths.js9Target)
return mkDir();
}
function js9MakeConfig() {
var config = run(
'./configure --with-webdir=' + paths.js9Target,
{ cwd: './node_modules/js9' }
)
return config();
}
function js9Make() {
var make = run('make', { cwd: './node_modules/js9' })
return make();
}
function js9MakeInst() {
var makeInst = run('make install', { cwd: './node_modules/js9' })
return makeInst();
}
function js9Config(bc) {
// read JSON config and append extra paramenters
let js9Config = require(paths.js9Target + '/js9Prefs.json')
js9Config.globalOpts.installDir = paths.js9Target.replace(paths.root, '')
js9Config.globalOpts.syncOps = [
"colormap",
"contrastbias",
"flip",
"pan",
"rot90",
"scale",
"wcs",
"zoom"
]
js9Config.globalOpts.updateTitlebar = false
js9Config.textColorOpts = { "info": "#000064" }
let outConfig = 'var JS9Prefs = ' + JSON.stringify(js9Config)
// Write JS file with JSON config
return fs.writeFile(paths.js9Target + '/js9prefs.js', outConfig, bc);
}
// JS9 CSS have some references to *.gif wrong and Django collectstatic
// command (with WhiteNoise installed) failed
// see issue https://github.com/ericmandel/js9/issues/74
function js9DelCSS() {
return del([
paths.js9Target + '/**/*.css',
'!' + paths.js9Target + '/js9-allinone.css',
]);
}
function js9MoveGif() {
return gulp
.src([paths.js9Target + '/images/*.gif'])
.pipe(gulp.dest(paths.js9Target));
}
function js9FixStaticUrl(bc) {
// If STATIC_URL isn't in the env, load it from the .env file. We also need BASE_URL
// but since it's optional it may not be set.
if (process.env.STATIC_URL === undefined) {
const result = require('dotenv').config({ 'path': './webinterface/.env' })
// note that .config automatically copies the env vars to process.env
if (result.error) {
throw result.error
}
}
let base_url = process.env.BASE_URL || null
let static_url = process.env.STATIC_URL || '/static/'
let fileContent = fs.readFileSync(paths.js9Target + '/js9prefs.js', 'utf8')
let serving_url = (base_url) ? '/' + base_url.split('/').join('') + '/' + static_url.split('/').join('') + '/' : static_url
return fs.writeFile(
paths.js9Target + '/js9prefs.js',
fileContent.replace('/static/', serving_url),
bc
);
}
// BrowserSync
function browserSync(done) {
browsersync.init(
{
port: 8001,
proxy: "localhost:8000"
}
);
done();
}
// BrowserSync reload
function browserSyncReload(done) {
browsersync.reload();
done();
}
// Clean vendor
function clean() {
return del([paths.vendor]);
}
// Bring third party dependencies from node_modules into vendor directory
function modules() {
// Bootstrap JS
var bootstrapJS = gulp.src('./node_modules/startbootstrap-sb-admin-2/vendor/bootstrap/js/*')
.pipe(gulp.dest(paths.vendor + '/bootstrap/js'));
// Bootstrap Select
var bootstrapSelectJS = gulp.src('./node_modules/bootstrap-select/dist/js/*')
.pipe(gulp.dest(paths.vendor + '/bootstrap-select/js'));
var bootstrapSelectCSS = gulp.src('./node_modules/bootstrap-select/dist/css/*')
.pipe(gulp.dest(paths.vendor + '/bootstrap-select/css'));
// Bokeh
var bokehJS = gulp.src('./node_modules/@bokeh/bokehjs/build/js/bokeh.min.js')
.pipe(gulp.dest(paths.vendor + '/bokehjs'));
var bokehJSwidgets = gulp.src('./node_modules/@bokeh/bokehjs/build/js/bokeh-widgets.min.js')
.pipe(gulp.dest(paths.vendor + '/bokehjs'));
// SB Admin 2 Bootstrap template
var bootstrapSbAdmin2 = gulp.src([
'./node_modules/startbootstrap-sb-admin-2/js/*.js',
'./node_modules/startbootstrap-sb-admin-2/css/*.css'
]).pipe(gulp.dest(paths.vendor + '/startbootstrap-sb-admin-2'));
// Bootstrap4 toggle
var bootstrap4toggle = gulp.src([
'./node_modules/bootstrap4-toggle/js/*.js',
'./node_modules/bootstrap4-toggle/css/*.css'
]).pipe(gulp.dest(paths.vendor + '/bootstrap4-toggle'));
// dataTables
var dataTables = gulp.src([
'./node_modules/datatables.net/js/*.js',
'./node_modules/datatables.net-bs4/js/*.js',
'./node_modules/datatables.net-bs4/css/*.css'
])
.pipe(gulp.dest(paths.vendor + '/datatables'));
// dataTables-buttons
var dataTablesButtons = gulp.src([
'./node_modules/datatables.net-buttons/js/*.js',
'./node_modules/datatables.net-buttons-bs4/js/*.js',
'./node_modules/datatables.net-buttons-bs4/css/*.css'
])
.pipe(gulp.dest(paths.vendor + '/datatables-buttons'));
// Font Awesome
var fontAwesome = gulp.src('./node_modules/@fortawesome/**/*')
.pipe(gulp.dest(paths.vendor + ''));
// jQuery Easing
var jqueryEasing = gulp.src('./node_modules/jquery.easing/*.js')
.pipe(gulp.dest(paths.vendor + '/jquery-easing'));
// jQuery
var jquery = gulp.src([
'./node_modules/jquery/dist/*',
'!./node_modules/jquery/dist/core.js'
])
.pipe(gulp.dest(paths.vendor + '/jquery'));
// jszip
var jszip = gulp.src([
'./node_modules/jszip/dist/*.js',
])
.pipe(gulp.dest(paths.vendor + '/jszip'));
// d3 celestial
var d3Celestial = gulp.src([
'./node_modules/d3-celestial/celestial*.js',
'./node_modules/d3-celestial/lib/d3*.js'
])
.pipe(gulp.dest(paths.vendor + '/d3-celestial'));
var d3CelestialData = gulp.src('./node_modules/d3-celestial/data/*.json')
.pipe(gulp.dest(paths.vendor + '/d3-celestial/data'));
var d3CelestialImage = gulp.src('./node_modules/d3-celestial/images/*')
.pipe(gulp.dest(paths.cssDir + '/images'));
// particles.js
var particlesJs = gulp.src('./node_modules/particles.js/particles.js')
.pipe(gulp.dest(paths.vendor + '/particles.js'));
// PrismJs
var prismJs = gulp.src('./node_modules/prismjs/prism.js')
.pipe(gulp.dest(paths.vendor + '/prismjs'));
var prismJsYaml = gulp.src('./node_modules/prismjs/components/prism-yaml.min.js')
.pipe(gulp.dest(paths.vendor + '/prismjs'));
var prismJsLineNum = gulp.src('./node_modules/prismjs/plugins/line-numbers/prism-line-numbers.min.js')
.pipe(gulp.dest(paths.vendor + '/prismjs/line-numbers'));
var prismJsCss = gulp.src('./node_modules/prismjs/themes/prism.css')
.pipe(gulp.dest(paths.vendor + '/prismjs'));
var prismJsLineNumCss = gulp.src('./node_modules/prismjs/plugins/line-numbers/prism-line-numbers.css')
.pipe(gulp.dest(paths.vendor + '/prismjs/line-numbers'));
return merge(bootstrapJS, bootstrapSbAdmin2, bootstrap4toggle, bokehJS, bokehJSwidgets, dataTables, dataTablesButtons, fontAwesome, jquery, jqueryEasing, jszip, d3Celestial, d3CelestialData, d3CelestialImage, particlesJs, prismJs, prismJsYaml, prismJsLineNum, prismJsCss, prismJsLineNumCss);
}
// SCSS task
function scssTask() {
return gulp
.src(paths.scss)
.pipe(sass({
outputStyle: "expanded",
}))
.on("error", sass.logError)
.pipe(autoprefixer({
cascade: false
}))
.pipe(gulp.dest(paths.cssDir));
}
// CSS task
function cssTask() {
return gulp
.src([
paths.css,
'!' + paths.cssMin,
])
.pipe(sourcemaps.init())
.pipe(rename({
suffix: '.min'
}))
.pipe(cleanCSS())
.pipe(sourcemaps.write('map'))
.pipe(gulp.dest(paths.cssDir))
.pipe(browsersync.stream());
}
// JS task
function jsTask() {
return gulp
.src([
paths.js,
'!' + paths.jsMin,
])
.pipe(sourcemaps.init())
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(uglify())
// .pipe(header(banner, {
// pkg: pkg
// }))
.pipe(rename({
suffix: '.min'
}))
.pipe(sourcemaps.write('map'))
.pipe(gulp.dest(paths.jsDir))
.pipe(browsersync.stream());
}
// Watch files
function watchFiles() {
gulp.watch(paths.scss, scssTask);
gulp.watch([paths.css, '!' + paths.cssMin], cssTask);
gulp.watch([paths.js, '!' + paths.jsMin], jsTask);
gulp.watch(paths.html, browserSyncReload);
}
// Define complex tasks
const js9 = gulp.series(js9Dir, js9MakeConfig, js9Make, js9MakeInst, js9Config)
const assets = gulp.parallel(gulp.series(scssTask, cssTask), jsTask)
const vendor = gulp.series(clean, gulp.parallel(modules, js9))
const build = gulp.series(vendor, assets);
const watch = gulp.series(assets, gulp.parallel(watchFiles, browserSync));
// Export tasks
exports.clean = clean;
exports.js9 = js9;
exports.css = cssTask;
exports.js = jsTask;
exports.vendor = vendor;
exports.build = build;
exports.watch = watch;
exports.default = build;
exports.debug = debug;
exports.js9staticprod = gulp.parallel(js9DelCSS, js9MoveGif, js9FixStaticUrl);