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');
25 all : [], // must always be empty
27 /* Command Line Arguments */
30 syncOnly: argv['sync-only'],
34 /* Source file locations */
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',
44 assets: './public/assets/', // vendor assets dir
45 fonts: './public/fonts/',
46 storybook: '.docs/.out/'
49 themes: 'public/themes'
54 * Clean up lingering static assets
56 gulp.task('clean', function () {
58 let ignore = "!" + config.dist.storybook + '.gitignore';
59 del.sync([config.dist.storybook + "*", ignore]);
62 return del.sync([config.dest.themes + "/*"]);
67 * Parses command line arguments
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;
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
83 gulp.task('sync', ['ingest', 'styles'], function() {
86 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));
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
109 // START style task definitions
112 * universal css compilcation
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
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
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
164 .pipe(gulpif(name.startsWith('colors'), rename({
165 basename: name.slice(7,-5), // remove prefix and suffix from name
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})));
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 () {
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);
189 fs.writeFileSync('.storybook/themeOptions.json', JSON.stringify(style_list), 'utf8');
195 * Copies node_modules to ./public
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];
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'));
216 gulp.src('node_modules/' + key + '/**/*')
217 .pipe(gulp.dest(config.dist.assets + key));
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 () {
230 proxy: "127.0.0.1:" + config.proxy,
237 * - runs by default when `gulp` is called from CLI
239 if (config.install) {
240 gulp.task('default', [ 'install' ]);
241 } else if (config.syncOnly && config.proxy) {
242 gulp.task('default', ['sync-only', 'watch']);
244 gulp.task('default', function (callback) {
245 runSequence('clean', ['sync'], 'style_list', callback)