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