Portal Two update and bug fixes (#1860)
[openemr.git] / gulpfile.js
blob6be688cac8c66b3f8e28d3b04f18dc759dcacdc3
1 'use strict';
3 // modules
4 var browserSync = require('browser-sync');
5 var csso = require('gulp-csso');
6 var del = require('del');
7 var fs = require('fs');
8 var glob = require('glob');
9 var gap = require('gulp-append-prepend');
10 var gulp = require('gulp');
11 var argv = require('minimist')(process.argv.slice(2));
12 var gulpif = require('gulp-if');
13 var prefix = require('gulp-autoprefixer');
14 var reload = browserSync.reload;
15 var rename = require('gulp-rename');
16 var runSequence = require('run-sequence');
17 var sass = require('gulp-sass');
18 var sourcemaps = require('gulp-sourcemaps');
21 var packages = require('./package.json');
23 // configuration
24 var config = {
25     all : [], // must always be empty
27     /* Command Line Arguments */
28     dev: argv['dev'],
29     build: argv['b'],
30     syncOnly: argv['sync-only'],
31     proxy: argv['p'],
32     install: argv['i'],
33     
34     /* Source file locations */
35     src: {
36         styles: {
37             style_uni: 'interface/themes/style_*.scss',
38             style_color: 'interface/themes/colors/*.scss',
39             rtl: 'interface/themes/rtl.scss',
40             all: 'public/themes/**/*style_*.css',
41         }
42     },
43     dist: {
44         assets: './public/assets/', // vendor assets dir
45         fonts: './public/fonts/',
46         storybook: '.docs/.out/'
47     },
48     dest: {
49         themes: 'public/themes'
50     }
53 /**
54  * Clean up lingering static assets 
55  */
56 gulp.task('clean', function () {
57     if (config.dev) {
58         let ignore = "!" + config.dist.storybook + '.gitignore';
59         del.sync([config.dist.storybook + "*", ignore]);
60     }
62     return del.sync([config.dest.themes + "/*"]);
63 });
66 /**
67  * Parses command line arguments
68  */
69 gulp.task('ingest', function() {
70     if (config.dev && typeof config.dev !== "boolean") {
71         // allows for custom proxy to be passed into script
72         config.proxy = config.dev;
73         config.dev = true;
74     }
75 });
78 /**
79  * Will start browser sync and/or watch changes to scss
80  * - Runs task(styles) first
81  * - Includes hack to dump fontawesome to the storybook dist
82  */
83 gulp.task('sync', ['ingest', 'styles'], function() {
84     if (config.proxy) {
85         browserSync.init({
86             proxy: "127.0.0.1:" + config.proxy
87         });
88     }
90     if (config.dev) {
91         // if building storybook, grab the public folder
92         gulp.src(['./public/**/*'], {"base" : "."})
93             .pipe(gulp.dest(config.dist.storybook));
94     }
95     // copy all leftover root-level components to the theme directory
96     // hoping this is only temporary
97     gulp.src(['interface/themes/*.{css,php}'])
98         .pipe(gulp.dest(config.dest.themes));
99 });
101 // definition of header for all compiled css
102 var autoGeneratedHeader = `
103 /*! This style sheet was autogenerated using gulp + scss
104  *  For usage instructions, see: https://github.com/openemr/openemr/blob/master/interface/README.md
105  */
109 // START style task definitions
112  * universal css compilcation
113  */
114 gulp.task('styles:style_uni', function () {
115     gulp.src(config.src.styles.style_uni)
116         .pipe(sourcemaps.init())
117         .pipe(sass().on('error', sass.logError))
118         .pipe(prefix('last 1 version'))
119         .pipe(gap.prependText(autoGeneratedHeader))
120         .pipe(gulpif(!config.dev, csso()))
121         .pipe(gulpif(!config.dev,sourcemaps.write()))
122         .pipe(gulp.dest(config.dest.themes))
123         .pipe(gulpif(config.dev && config.build, gulp.dest(config.dist.storybook + config.dest.themes)))
124         .pipe(gulpif(config.dev, reload({stream:true})));
128  * color compilation for colored themes
129  */
130 gulp.task('styles:style_color', function () {
131     gulp.src(config.src.styles.style_color)
132         .pipe(sourcemaps.init())
133         .pipe(sass().on('error', sass.logError))
134         .pipe(prefix('last 1 version'))
135         .pipe(gap.prependText(autoGeneratedHeader))
136         .pipe(gulpif(!config.dev, csso()))
137         .pipe(gulpif(!config.dev,sourcemaps.write()))
138         .pipe(gulp.dest(config.dest.themes))
139         .pipe(gulpif(config.dev && config.build, gulp.dest(config.dist.storybook + config.dest.themes)))
140         .pipe(gulpif(config.dev, reload({stream:true})));
144  * append rtl css to all style themes
145  * also, create list of all themes for style_list to use
146  */
147 gulp.task('styles:rtl', function () {
148     var uni = glob.sync(config.src.styles.style_uni);
149     var colors = glob.sync(config.src.styles.style_color);
150     config.all = uni.concat(colors);
152     for (var i=0; i<config.all.length; i++) {
153         var name = config.all[i].split('themes/')[1];
154         gulp.src(config.src.styles.rtl)
155         .pipe(gap.prependText('@import "' + name + '";\n'))
156             .pipe(sourcemaps.init())
157             .pipe(sass().on('error', sass.logError))
158             .pipe(prefix('last 1 version'))
159             .pipe(gulpif(!config.dev, csso()))
160             .pipe(gulpif(!name.startsWith('colors'), rename({
161                 basename: name.slice(0,-5), // remove suffix from name
162                 prefix:"rtl_"
163             })))
164             .pipe(gulpif(name.startsWith('colors'), rename({
165                 basename: name.slice(7,-5), // remove prefix and suffix from name
166                 prefix:"rtl_"
167             })))
168             .pipe(gulp.dest(config.dest.themes))
169             .pipe(gulpif(config.dev && config.build, gulp.dest(config.dist.storybook + config.dest.themes)))
170             .pipe(gulpif(config.dev, reload({stream:true})));
171     }
172     
175 gulp.task('styles', ['styles:style_uni', 'styles:style_color', 'styles:rtl']);
176 // END style task definitions
179 * Create a JSON for storybook to use
181 gulp.task('style_list', ['styles'], function () {
182     if (config.dev) {
183         var style_list = [];
184         for (var i=0; i<config.all.length; i++) {
185             var theme_name = "style_" + config.all[i].split('style_')[1].slice(0,-5);
186             style_list.push(theme_name);
187             style_list.push('rtl_' + theme_name);
188         }
189         fs.writeFileSync('.storybook/themeOptions.json', JSON.stringify(style_list), 'utf8');
190     }
195  * Copies node_modules to ./public
196  */
197 gulp.task('install', function() {
199     // combine dependencies and napa sources into one object
200     var dependencies = packages.dependencies;
201     for (var key in packages.napa) {
202         if (packages.napa.hasOwnProperty(key)) {
203             dependencies[key] = packages.napa[key];
204         }
205     }
207     for (var key in dependencies) {
208         // check if the property/key is defined in the object itself, not in parent
209         if (dependencies.hasOwnProperty(key)) {
210             // only copy dist directory, if it exists
211             // skip this if for dwv dependency
212             if (key != 'dwv' && fs.existsSync('node_modules/' + key + '/dist')) {
213                 gulp.src('node_modules/' + key + '/dist/**/*')
214                     .pipe(gulp.dest(config.dist.assets + key + '/dist'));
215             } else {
216                 gulp.src('node_modules/' + key + '/**/*')
217                     .pipe(gulp.dest(config.dist.assets + key));
218             }
219         }
220     }
223 gulp.task('watch', function() {
224     // watch all changes and re-run styles
225     gulp.watch('./interface/**/*.scss', {interval: 1000, mode: 'poll'}, ['styles']);
228 gulp.task('sync-only', function () {
229     browserSync.init({
230         proxy: "127.0.0.1:" + config.proxy,
231         open: false
232     });
235  /**
236  * Default config
237  * - runs by default when `gulp` is called from CLI
238  */
239 if (config.install) {
240     gulp.task('default', [ 'install' ]);
241 } else if (config.syncOnly && config.proxy) {
242     gulp.task('default', ['sync-only', 'watch']);
243 } else {
244     gulp.task('default', function (callback) {
245         runSequence('clean', ['sync'], 'style_list', callback)
246     });