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');
20 var packages = require('./package.json');
24 all : [], // must always be empty
26 /* Command Line Arguments */
32 /* Source file locations */
35 style_uni: 'interface/themes/style_*.scss',
36 style_color: 'interface/themes/colors/*.scss',
37 rtl: 'interface/themes/rtl.scss',
38 all: 'public/themes/**/*style_*.css',
42 assets: './public/assets/', // vendor assets dir
43 fonts: './public/fonts/',
44 storybook: '.docs/.out/'
47 themes: 'public/themes'
52 * Clean up lingering static assets
54 gulp.task('clean', function () {
56 let ignore = "!" + config.dist.storybook + '.gitignore';
57 del.sync([config.dist.storybook + "*", ignore]);
60 return del.sync([config.dest.themes + "/*"]);
65 * Parses command line arguments
67 gulp.task('ingest', function() {
68 if (config.dev && typeof config.dev !== "boolean") {
69 // allows for custom proxy to be passed into script
70 config.proxy = config.dev;
77 * Will start browser sync and/or watch changes to scss
78 * - Runs task(styles) first
79 * - Includes hack to dump fontawesome to the storybook dist
81 gulp.task('sync', function() {
84 proxy: "127.0.0.1:" + config.proxy
91 // if building storybook, grab the public folder
92 gulp.src(['./public/**/*'], {"base" : "."})
93 .pipe(gulp.dest(config.dist.storybook));
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));
102 // watch for changes and run styles on change
103 gulp.watch('inteface/themes/**/*.scss', ['styles']);
108 // definition of header for all compiled css
109 var autoGeneratedHeader = `
110 /*! This style sheet was autogenerated using gulp + scss
111 * For usage instructions, see: https://github.com/openemr/openemr/blob/master/interface/README.md
116 // START style task definitions
119 * universal css compilcation
121 gulp.task('styles:style_uni', function () {
122 gulp.src(config.src.styles.style_uni)
123 .pipe(sourcemaps.init())
124 .pipe(sass().on('error', sass.logError))
125 .pipe(prefix('last 1 version'))
126 .pipe(gap.prependText(autoGeneratedHeader))
127 .pipe(gulpif(!config.dev, csso()))
128 .pipe(gulpif(!config.dev,sourcemaps.write()))
129 .pipe(gulp.dest(config.dest.themes))
130 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dist.storybook + config.dest.themes)))
131 .pipe(gulpif(config.dev, reload({stream:true})));
135 * color compilation for colored themes
137 gulp.task('styles:style_color', function () {
138 gulp.src(config.src.styles.style_color)
139 .pipe(sourcemaps.init())
140 .pipe(sass().on('error', sass.logError))
141 .pipe(prefix('last 1 version'))
142 .pipe(gap.prependText(autoGeneratedHeader))
143 .pipe(gulpif(!config.dev, csso()))
144 .pipe(gulpif(!config.dev,sourcemaps.write()))
145 .pipe(gulp.dest(config.dest.themes))
146 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dist.storybook + config.dest.themes)))
147 .pipe(gulpif(config.dev, reload({stream:true})));
151 * append rtl css to all style themes
152 * also, create list of all themes for style_list to use
154 gulp.task('styles:rtl', function () {
155 var uni = glob.sync(config.src.styles.style_uni);
156 var colors = glob.sync(config.src.styles.style_color);
157 config.all = uni.concat(colors);
159 for (var i=0; i<config.all.length; i++) {
160 var name = config.all[i].split('themes/')[1];
161 gulp.src(config.src.styles.rtl)
162 .pipe(gap.prependText('@import "' + name + '";\n'))
163 .pipe(sourcemaps.init())
164 .pipe(sass().on('error', sass.logError))
165 .pipe(prefix('last 1 version'))
166 .pipe(gulpif(!config.dev, csso()))
167 .pipe(gulpif(!name.startsWith('colors'), rename({
168 basename: name.slice(0,-5), // remove suffix from name
171 .pipe(gulpif(name.startsWith('colors'), rename({
172 basename: name.slice(7,-5), // remove prefix and suffix from name
175 .pipe(gulp.dest(config.dest.themes))
176 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dist.storybook + config.dest.themes)))
177 .pipe(gulpif(config.dev, reload({stream:true})));
182 gulp.task('styles', ['styles:style_uni', 'styles:style_color', 'styles:rtl']);
183 // END style task definitions
186 * Create a JSON for storybook to use
188 gulp.task('style_list', function () {
191 for (var i=0; i<config.all.length; i++) {
192 var theme_name = "style_" + config.all[i].split('style_')[1].slice(0,-5);
193 style_list.push(theme_name);
194 style_list.push('rtl_' + theme_name);
196 fs.writeFileSync('.storybook/themeOptions.json', JSON.stringify(style_list), 'utf8');
202 * Copies node_modules to ./public
204 gulp.task('install', function() {
206 // combine dependencies and napa sources into one object
207 var dependencies = packages.dependencies;
208 for (var key in packages.napa) {
209 if (packages.napa.hasOwnProperty(key)) {
210 dependencies[key] = packages.napa[key];
214 for (var key in dependencies) {
215 // check if the property/key is defined in the object itself, not in parent
216 if (dependencies.hasOwnProperty(key)) {
217 // only copy dist directory, if it exists
218 // skip this if for dwv dependency
219 if (key != 'dwv' && fs.existsSync('node_modules/' + key + '/dist')) {
220 gulp.src('node_modules/' + key + '/dist/**/*')
221 .pipe(gulp.dest(config.dist.assets + key + '/dist'));
223 gulp.src('node_modules/' + key + '/**/*')
224 .pipe(gulp.dest(config.dist.assets + key));
232 * - runs by default when `gulp` is called from CLI
234 if (config.install) {
235 gulp.task('default', [ 'install' ]);
237 gulp.task('default', function (callback) {
238 runSequence(['ingest', 'clean'], [ 'styles', 'sync' ], 'style_list', callback)