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 replace = require('replace-in-file');
11 var gulp = require('gulp');
12 var argv = require('minimist')(process.argv.slice(2));
13 var gulpif = require('gulp-if');
14 var prefix = require('gulp-autoprefixer');
15 var reload = browserSync.reload;
16 var rename = require('gulp-rename');
17 var runSequence = require('run-sequence');
18 var sass = require('gulp-sass');
19 var sourcemaps = require('gulp-sourcemaps');
20 var watch = require('gulp-watch');
23 var packages = require('./package.json');
27 all : [], // must always be empty
29 /* Command Line Arguments */
32 syncOnly: argv['sync-only'],
36 /* Source file locations */
39 style_uni: 'interface/themes/style_*.scss',
40 style_color: 'interface/themes/colors/*.scss',
41 rtl: 'interface/themes/rtl.scss',
42 directional: 'interface/themes/directional.scss',
43 all: 'public/themes/**/*style_*.css',
47 assets: './public/assets/', // vendor assets dir
48 fonts: './public/fonts/',
49 storybook: '.docs/.out/'
52 themes: 'public/themes'
57 * Clean up lingering static assets
59 gulp.task('clean', function () {
61 let ignore = "!" + config.dist.storybook + '.gitignore';
62 del.sync([config.dist.storybook + "*", ignore]);
65 return del.sync([config.dest.themes + "/*"]);
70 * Parses command line arguments
72 gulp.task('ingest', function() {
73 if (config.dev && typeof config.dev !== "boolean") {
74 // allows for custom proxy to be passed into script
75 config.proxy = config.dev;
82 * Will start browser sync and/or watch changes to scss
83 * - Runs task(styles) first
84 * - Includes hack to dump fontawesome to the storybook dist
86 gulp.task('sync', ['ingest', 'styles'], function() {
89 proxy: "127.0.0.1:" + config.proxy
94 // if building storybook, grab the public folder
95 gulp.src(['./public/**/*'], {"base" : "."})
96 .pipe(gulp.dest(config.dist.storybook));
98 // copy all leftover root-level components to the theme directory
99 // hoping this is only temporary
100 gulp.src(['interface/themes/*.{css,php}'])
101 .pipe(gulp.dest(config.dest.themes));
104 // definition of header for all compiled css
105 var autoGeneratedHeader = `
106 /*! This style sheet was autogenerated using gulp + scss
107 * For usage instructions, see: https://github.com/openemr/openemr/blob/master/interface/README.md
112 // START style task definitions
115 * universal css compilcation
117 gulp.task('styles:style_uni', function () {
118 return gulp.src(config.src.styles.style_uni)
119 .pipe(sourcemaps.init())
120 .pipe(sass().on('error', sass.logError))
121 .pipe(prefix('last 1 version'))
122 .pipe(gap.prependText(autoGeneratedHeader))
123 .pipe(gulpif(!config.dev, csso()))
124 .pipe(gulpif(!config.dev,sourcemaps.write()))
125 .pipe(gulp.dest(config.dest.themes))
126 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dist.storybook + config.dest.themes)))
127 .pipe(gulpif(config.dev, reload({stream:true})));
130 gulp.task('rtl:style_uni', function() {
131 return gulp.src(config.src.styles.style_uni)
132 .pipe(gap.prependText('@import "./rtl.scss";\n')) // watch out for this relative path!
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(rename({ prefix: "rtl_"}))
140 .pipe(gulp.dest(config.dest.themes))
141 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dist.storybook + config.dest.themes)))
142 .pipe(gulpif(config.dev, reload({stream:true})));
146 * color compilation for colored themes
148 gulp.task('styles:style_color', function () {
149 return gulp.src(config.src.styles.style_color)
150 .pipe(sourcemaps.init())
151 .pipe(sass().on('error', sass.logError))
152 .pipe(prefix('last 1 version'))
153 .pipe(gap.prependText(autoGeneratedHeader))
154 .pipe(gulpif(!config.dev, csso()))
155 .pipe(gulpif(!config.dev,sourcemaps.write()))
156 .pipe(gulp.dest(config.dest.themes))
157 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dist.storybook + config.dest.themes)))
158 .pipe(gulpif(config.dev, reload({stream:true})));
161 gulp.task('rtl:style_color', function() {
162 return gulp.src(config.src.styles.style_color)
163 .pipe(gap.prependText('@import "../rtl.scss";\n')) // watch out for this relative path!
164 .pipe(sourcemaps.init())
165 .pipe(sass().on('error', sass.logError))
166 .pipe(prefix('last 1 version'))
167 .pipe(gap.prependText(autoGeneratedHeader))
168 .pipe(gulpif(!config.dev, csso()))
169 .pipe(gulpif(!config.dev,sourcemaps.write()))
170 .pipe(rename({ prefix: "rtl_"}))
171 .pipe(gulp.dest(config.dest.themes))
172 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dist.storybook + config.dest.themes)))
173 .pipe(gulpif(config.dev, reload({stream:true})));
176 gulp.task('styles', ['styles:style_uni', 'styles:style_color']);
178 gulp.task('rtl_styles', ['rtl:style_uni', 'rtl:style_color']);
181 * append rtl css to all style themes
182 * also, create list of all themes for style_list to use
184 gulp.task('rtl:setup', function(callback) {
185 var uni = glob.sync(config.src.styles.style_uni);
186 var colors = glob.sync(config.src.styles.style_color);
187 config.all = uni.concat(colors);
189 // backup and update directional file
190 fs.copyFile(config.src.styles.directional, config.src.styles.directional + '.temp', (err) => {
193 files: config.src.styles.directional,
194 from: /ltr \!default/g,
200 gulp.task('rtl:teardown', function(callback) {
202 files: config.src.styles.directional,
203 from: /rtl \!default/g,
205 }).then(function () {
206 fs.unlink(config.src.styles.directional + '.temp', (err) => {
213 // END style task definitions
216 * Create a JSON for storybook to use
218 gulp.task('style_list', function () {
221 for (var i=0; i<config.all.length; i++) {
222 var theme_name = "style_" + config.all[i].split('style_')[1].slice(0,-5);
223 style_list.push(theme_name);
224 style_list.push('rtl_' + theme_name);
226 fs.writeFileSync('.storybook/themeOptions.json', JSON.stringify(style_list), 'utf8');
232 * Copies node_modules to ./public
234 gulp.task('install', function() {
236 // combine dependencies and napa sources into one object
237 var dependencies = packages.dependencies;
238 for (var key in packages.napa) {
239 if (packages.napa.hasOwnProperty(key)) {
240 dependencies[key] = packages.napa[key];
244 for (var key in dependencies) {
245 // check if the property/key is defined in the object itself, not in parent
246 if (dependencies.hasOwnProperty(key)) {
247 // only copy dist directory, if it exists
248 // skip this if for dwv dependency
249 if (key != 'dwv' && fs.existsSync('node_modules/' + key + '/dist')) {
250 gulp.src('node_modules/' + key + '/dist/**/*')
251 .pipe(gulp.dest(config.dist.assets + key + '/dist'));
253 gulp.src('node_modules/' + key + '/**/*')
254 .pipe(gulp.dest(config.dist.assets + key));
260 gulp.task('watch', function() {
261 // watch all changes and re-run styles
262 gulp.watch('./interface/**/*.scss', {interval: 1000, mode: 'poll'}, ['styles']);
264 // watch all changes to css/php files in themes and copy to public
265 return watch('./interface/themes/*.{css,php}', { ignoreInitial: false })
266 .pipe(gulp.dest(config.dest.themes));
269 gulp.task('sync-only', function () {
271 proxy: "127.0.0.1:" + config.proxy,
278 * - runs by default when `gulp` is called from CLI
280 if (config.install) {
281 gulp.task('default', [ 'install' ]);
282 } else if (config.syncOnly && config.proxy) {
283 gulp.task('default', ['sync-only', 'watch']);
285 gulp.task('default', function (callback) {
286 runSequence('clean', ['sync'], 'rtl:setup', 'rtl_styles', 'rtl:teardown', 'style_list', callback);