forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
570 lines (536 loc) · 20.3 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
"use strict";
// modules
const csso = require('gulp-csso');
const del = require('del');
const fs = require('fs');
const glob = require('glob');
const gap = require('gulp-append-prepend');
const replace = require('replace-in-file');
const gulp = require('gulp');
const argv = require('minimist')(process.argv.slice(2));
const gulpif = require('gulp-if');
const prefix = require('autoprefixer');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const sass = require('gulp-dart-sass');
const sourcemaps = require('gulp-sourcemaps');
const gulp_watch = require('gulp-watch');
const injector = require('gulp-inject-string');
const colors = require('colors');
// package.json
const packages = require('./package.json');
const logprefix = "[OpenEMR]".bold.cyan + " ";
// configuration
let config = {
all: [], // must always be empty
// Command Line Arguments
dev: argv['dev'],
build: argv['b'],
install: argv['i'],
// Source file locations
src: {
styles: {
style_tabs: 'interface/themes/tabs_style_*.scss',
style_uni: 'interface/themes/oe-styles/style_*.scss',
style_color: 'interface/themes/colors/*.scss',
directional: 'interface/themes/directional.scss',
misc: 'interface/themes/misc/**/*.scss'
}
},
dist: {
assets: 'public/assets/'
},
dest: {
themes: 'public/themes',
misc_themes: 'public/themes/misc'
}
};
if (config.install) {
console.log("\nCopying OpenEMR dependencies using Gulp".bold.yellow + "\n");
} else if (config.build) {
console.log("\nBuilding OpenEMR themes using Gulp".bold.yellow + "\n");
} else if (config.dev) {
console.log("\nBuilding OpenEMR themes using Dev Flag for Gulp".bold.yellow + "\n");
} else if (config.all) {
console.log("\nBuilding OpenEMR themes using All Flag for Gulp".bold.yellow + "\n");
} else {
// This is used for gulp watch & other misc things
console.log("\nRunning Gulp for OpenEMR".bold.yellow + "\n");
}
function log_error(isSuccess, err) {
isSuccess = false;
console.error(logprefix + "An error occured! Check the log for details.");
// Log error to console
console.error(err.toString().red);
// Kills gulp on error since if we keep running it will
// still fail
process.exit(1);
}
// Clean up lingering static themes
function clean(done) {
del.sync([config.dest.themes + "/*"]);
done();
}
// Parses command line arguments
function ingest(done) {
if (config.dev && typeof config.dev !== "boolean") {
config.dev = true;
}
done();
}
// definition of header for all compiled css
const autoGeneratedHeader = `
/*! This style sheet was autogenerated using gulp + scss
* For usage instructions, see: https://github.com/openemr/openemr/blob/master/interface/README.md
*/
`;
// standard themes css compilation
function styles_style_uni() {
let isSuccess = true;
return gulp.src(config.src.styles.style_uni)
.pipe(gap.prependText('$compact-theme: false;\n'))
.pipe(injector.replace('// bs4import', '@import "../../../public/assets/bootstrap/scss/bootstrap";'))
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished compiling OpenEMR base themes");
}
});
}
// standard themes compact css compilation
function styles_style_uni_compact() {
let isSuccess = true;
return gulp.src(config.src.styles.style_uni)
.pipe(gap.prependText('@import "../compact-theme-defaults";\n'))
.pipe(injector.replace('// bs4import', '@import "../oemr_compact_imports";'))
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.pipe(rename({
prefix: "compact_"
}))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished compiling OpenEMR compact base themes");
}
});
}
// color themes css compilation
function styles_style_color() {
let isSuccess = true;
return gulp.src(config.src.styles.style_color)
.pipe(gap.prependText('$compact-theme: false;\n'))
.pipe(injector.replace('// bs4import', '@import "../../../public/assets/bootstrap/scss/bootstrap";'))
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished compiling OpenEMR color themes");
}
});
}
// color themes compact css compilation
function styles_style_color_compact() {
let isSuccess = true;
return gulp.src(config.src.styles.style_color)
.pipe(gap.prependText('@import "../compact-theme-defaults";\n'))
.pipe(injector.replace('// bs4import', '@import "../oemr_compact_imports";'))
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.pipe(rename({
prefix: "compact_"
}))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished compiling OpenEMR compact color themes");
}
});
}
// Tabs CSS compilation
function styles_style_tabs() {
let isSuccess = true;
return gulp.src(config.src.styles.style_tabs)
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished compiling OpenEMR tab navigation styles");
}
});
}
// For anything else that needs to be moved, use misc themes
function styles_style_misc() {
let isSuccess = true;
return gulp.src(config.src.styles.misc)
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.misc_themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished compiling miscellaneous styles");
}
});
}
// rtl standard themes css compilation
function rtl_style_uni() {
let isSuccess = true;
return gulp.src(config.src.styles.style_uni)
.pipe(gap.prependText('$compact-theme: false;\n$dir: rtl;\n@import "../rtl";\n')) // watch out for this relative path!
.pipe(gap.appendText('@include if-rtl { @include rtl_style; #bigCal { border-right: 1px solid $black !important; } }\n'))
.pipe(injector.replace('// bs4import', '@import "../oemr-rtl";'))
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.pipe(rename({
prefix: "rtl_"
}))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished compiling RTL base themes");
}
});
}
// rtl standard themes compact css compilation
function rtl_style_uni_compact() {
let isSuccess = true;
return gulp.src(config.src.styles.style_uni)
.pipe(gap.prependText('@import "../compact-theme-defaults";\n'))
.pipe(gap.prependText('$dir: rtl;\n@import "../rtl";\n')) // watch out for this relative path!
.pipe(gap.appendText('@include if-rtl { @include rtl_style; #bigCal { border-right: 1px solid $black !important; } }\n'))
.pipe(injector.replace('// bs4import', '@import "../oemr_rtl_compact_imports";'))
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.pipe(rename({
prefix: "rtl_compact_"
}))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished compiling RTL base compact themes");
}
});
}
// rtl color themes css compilation
function rtl_style_color() {
let isSuccess = true;
return gulp.src(config.src.styles.style_color)
.pipe(gap.prependText('$compact-theme: false;\n$dir: rtl;\n@import "../rtl";\n')) // watch out for this relative path!
.pipe(gap.appendText('@include if-rtl { @include rtl_style; #bigCal { border-right: 1px solid $black !important; } }\n'))
.pipe(injector.replace('// bs4import', '@import "../oemr-rtl";'))
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.pipe(rename({
prefix: "rtl_"
}))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Compiled OpenEMR RTL color themes");
}
});
}
// rtl color themes compact css compilation
function rtl_style_color_compact() {
let isSuccess = true;
return gulp.src(config.src.styles.style_color)
.pipe(gap.prependText('@import "../compact-theme-defaults";\n'))
.pipe(gap.prependText('$dir: rtl;\n@import "../rtl";\n')) // watch out for this relative path!
.pipe(gap.appendText('@include if-rtl { @include rtl_style; #bigCal { border-right: 1px solid $black !important; } }\n'))
.pipe(injector.replace('// bs4import', '@import "../oemr_rtl_compact_imports";'))
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.pipe(rename({
prefix: "rtl_compact_"
}))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished compiling RTL compact color themes");
}
});
}
// rtl standard themes css compilation
function rtl_style_tabs() {
let isSuccess = true;
return gulp.src(config.src.styles.style_tabs)
.pipe(gap.prependText('$dir: rtl;\n@import "rtl";\n')) // watch out for this relative path!
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.pipe(rename({
prefix: "rtl_"
}))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished compiling RTL tabs styles");
}
});
}
// For anything else that needs to be moved, use misc themes
function rtl_style_misc() {
let isSuccess = true;
return gulp.src(config.src.styles.misc)
.pipe(gap.prependText('$dir: rtl;\n')) // Simply a flag here due to a hierarchy possibly being created
.pipe(sourcemaps.init())
.pipe(sass().on('error', (err) => {
log_error(isSuccess, err);
}))
.pipe(postcss([prefix()]))
.pipe(gap.prependText(autoGeneratedHeader))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(!config.dev, sourcemaps.write()))
.pipe(rename({
prefix: "rtl_"
}))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.misc_themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Compiled rest of RTL SCSS");
}
});
}
// compile themes
const styles = gulp.parallel(styles_style_color, styles_style_color_compact, styles_style_uni, styles_style_uni_compact, styles_style_tabs, styles_style_misc, rtl_style_color, rtl_style_color_compact, rtl_style_uni, rtl_style_uni_compact, rtl_style_tabs, rtl_style_misc);
// Copies (and distills, if possible) assets from node_modules to public/assets
function install(done) {
console.log(logprefix + "Running OpenEMR gulp install task...");
// combine dependencies and napa sources into one object
const dependencies = packages.dependencies;
for (let key in packages.napa) {
if (Object.prototype.hasOwnProperty.call(packages.napa, key)) {
dependencies[key] = packages.napa[key];
}
}
for (let key in dependencies) {
// check if the property/key is defined in the object itself, not in parent
if (Object.prototype.hasOwnProperty.call(dependencies, key)) {
if (key == "dwv") {
// dwv is special and need to copy dist, decoders and locales
gulp.src("node_modules/" + key + "/dist/**/*").pipe(
gulp.dest(config.dist.assets + key + "/dist")
);
gulp.src("node_modules/" + key + "/decoders/**/*").pipe(
gulp.dest(config.dist.assets + key + "/decoders")
);
gulp.src("node_modules/" + key + "/locales/**/*").pipe(
gulp.dest(config.dist.assets + key + "/locales")
);
} else if (key == "bootstrap" || key == "bootstrap-rtl") {
// bootstrap and bootstrap-v4-rtl are special and need to copy dist and scss
gulp.src("node_modules/" + key + "/dist/**/*").pipe(
gulp.dest(config.dist.assets + key + "/dist")
);
gulp.src("node_modules/" + key + "/scss/**/*").pipe(
gulp.dest(config.dist.assets + key + "/scss")
);
} else if (key == "@fortawesome/fontawesome-free") {
// @fortawesome/fontawesome-free is special and need to copy css, scss, and webfonts
gulp.src("node_modules/" + key + "/css/**/*").pipe(
gulp.dest(config.dist.assets + key + "/css")
);
gulp.src("node_modules/" + key + "/scss/**/*").pipe(
gulp.dest(config.dist.assets + key + "/scss")
);
gulp.src("node_modules/" + key + "/webfonts/**/*").pipe(
gulp.dest(config.dist.assets + key + "/webfonts")
);
} else if (key == "moment") {
gulp.src("node_modules/" + key + "/min/**/*").pipe(
gulp.dest(config.dist.assets + key + "/min")
);
gulp.src("node_modules/" + key + "/moment.js").pipe(
gulp.dest(config.dist.assets + key)
);
} else if (fs.existsSync("node_modules/" + key + "/dist")) {
// only copy dist directory, if it exists
gulp.src("node_modules/" + key + "/dist/**/*").pipe(
gulp.dest(config.dist.assets + key + "/dist")
);
} else {
// copy everything
gulp.src("node_modules/" + key + "/**/*").pipe(
gulp.dest(config.dist.assets + key)
);
}
}
}
console.log(logprefix + "Finished running OpenEMR gulp install task");
done();
}
function watch() {
let isSuccess = true;
console.log(logprefix + "Running gulp watch task...");
// watch all changes and re-run styles
gulp.watch('./interface/**/*.scss', {
interval: 1000,
mode: 'poll'
}, styles)
.on('error', (err) => {
log_error(isSuccess, err);
});
// watch php separately since autoprefix is not needed
gulp_watch('./interface/themes/*.php', {
ignoreInitial: false
})
.pipe(gulp.dest(config.dest.themes))
.on('error', (err) => {
log_error(isSuccess, err);
});
// watch all changes to css files in themes and
// autoprefix them before copying to public
return gulp_watch('./interface/themes/*.css', {
ignoreInitial: false
})
.pipe(postcss([prefix()]))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished running gulp watch task");
}
});
}
function sync() {
let isSuccess = true;
console.log(logprefix + "Running gulp sync task...");
// copy all leftover root-level components to the theme directory
// hoping this is only temporary
// Copy php file separately since we don't need to autoprefix them
gulp.src(['interface/themes/*.php'])
.pipe(gulp.dest(config.dest.themes))
.on('error', (err) => {
log_error(isSuccess, err);
});
// Copy CSS files and autoprefix them
return gulp.src(['interface/themes/*.css'])
.pipe(postcss([prefix()]))
.on('error', (err) => {
log_error(isSuccess, err);
})
.pipe(gulp.dest(config.dest.themes))
.on('end', () => {
if (isSuccess) {
console.log(logprefix + "Finished running gulp sync task");
}
});
}
// Export watch task
exports.watch = watch;
// Export pertinent default task
// - Note that the default task runs if no other task is chosen,
// which is generally how this script is always used (except in
// rare case where the user is running the watch task).
if (config.install) {
exports.default = gulp.series(install);
} else {
exports.default = gulp.series(clean, ingest, styles, sync);
}