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/'
51 themes: 'public/themes'
56 * Clean up lingering static assets
58 gulp.task('clean', function () {
59 return del.sync([config.dest.themes + "/*"]);
64 * Parses command line arguments
66 gulp.task('ingest', function () {
67 if (config.dev && typeof config.dev !== "boolean") {
68 // allows for custom proxy to be passed into script
69 config.proxy = config.dev;
76 * Will start browser sync and/or watch changes to scss
77 * - Runs task(styles) first
79 gulp.task('sync', ['ingest', 'styles'], function () {
82 proxy: "127.0.0.1:" + config.proxy
86 // copy all leftover root-level components to the theme directory
87 // hoping this is only temporary
88 gulp.src(['interface/themes/*.{css,php}'])
89 .pipe(gulp.dest(config.dest.themes));
92 // definition of header for all compiled css
93 var autoGeneratedHeader = `
94 /*! This style sheet was autogenerated using gulp + scss
95 * For usage instructions, see: https://github.com/openemr/openemr/blob/master/interface/README.md
100 // START style task definitions
103 * universal css compilcation
105 gulp.task('styles:style_uni', function () {
106 return gulp.src(config.src.styles.style_uni)
107 .pipe(sourcemaps.init())
108 .pipe(sass().on('error', sass.logError))
109 .pipe(prefix('last 1 version'))
110 .pipe(gap.prependText(autoGeneratedHeader))
111 .pipe(gulpif(!config.dev, csso()))
112 .pipe(gulpif(!config.dev, sourcemaps.write()))
113 .pipe(gulp.dest(config.dest.themes))
114 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dest.themes)))
115 .pipe(gulpif(config.dev, reload({ stream: true })));
118 gulp.task('rtl:style_uni', function () {
119 return gulp.src(config.src.styles.style_uni)
120 .pipe(gap.prependText('@import "./rtl.scss";\n')) // watch out for this relative path!
121 .pipe(sourcemaps.init())
122 .pipe(sass().on('error', sass.logError))
123 .pipe(prefix('last 1 version'))
124 .pipe(gap.prependText(autoGeneratedHeader))
125 .pipe(gulpif(!config.dev, csso()))
126 .pipe(gulpif(!config.dev, sourcemaps.write()))
127 .pipe(rename({ prefix: "rtl_" }))
128 .pipe(gulp.dest(config.dest.themes))
129 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dest.themes)))
130 .pipe(gulpif(config.dev, reload({ stream: true })));
134 * color compilation for colored themes
136 gulp.task('styles:style_color', function () {
137 return gulp.src(config.src.styles.style_color)
138 .pipe(sourcemaps.init())
139 .pipe(sass().on('error', sass.logError))
140 .pipe(prefix('last 1 version'))
141 .pipe(gap.prependText(autoGeneratedHeader))
142 .pipe(gulpif(!config.dev, csso()))
143 .pipe(gulpif(!config.dev, sourcemaps.write()))
144 .pipe(gulp.dest(config.dest.themes))
145 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dest.themes)))
146 .pipe(gulpif(config.dev, reload({ stream: true })));
149 gulp.task('rtl:style_color', function () {
150 return gulp.src(config.src.styles.style_color)
151 .pipe(gap.prependText('@import "../rtl.scss";\n')) // watch out for this relative path!
152 .pipe(sourcemaps.init())
153 .pipe(sass().on('error', sass.logError))
154 .pipe(prefix('last 1 version'))
155 .pipe(gap.prependText(autoGeneratedHeader))
156 .pipe(gulpif(!config.dev, csso()))
157 .pipe(gulpif(!config.dev, sourcemaps.write()))
158 .pipe(rename({ prefix: "rtl_" }))
159 .pipe(gulp.dest(config.dest.themes))
160 .pipe(gulpif(config.dev && config.build, gulp.dest(config.dest.themes)))
161 .pipe(gulpif(config.dev, reload({ stream: true })));
164 gulp.task('styles', ['styles:style_uni', 'styles:style_color']);
166 gulp.task('rtl_styles', ['rtl:style_uni', 'rtl:style_color']);
169 * append rtl css to all style themes
170 * also, create list of all themes for style_list to use
172 gulp.task('rtl:setup', function (callback) {
173 var uni = glob.sync(config.src.styles.style_uni);
174 var colors = glob.sync(config.src.styles.style_color);
175 config.all = uni.concat(colors);
177 // backup and update directional file
178 fs.copyFile(config.src.styles.directional, config.src.styles.directional + '.temp', (err) => {
181 files: config.src.styles.directional,
182 from: /ltr \!default/g,
188 gulp.task('rtl:teardown', function (callback) {
190 files: config.src.styles.directional,
191 from: /rtl \!default/g,
193 }).then(function () {
194 fs.unlink(config.src.styles.directional + '.temp', (err) => {
201 // END style task definitions
205 * Copies node_modules to ./public
207 gulp.task('install', function () {
209 // combine dependencies and napa sources into one object
210 var dependencies = packages.dependencies;
211 for (var key in packages.napa) {
212 if (packages.napa.hasOwnProperty(key)) {
213 dependencies[key] = packages.napa[key];
217 for (var key in dependencies) {
218 // check if the property/key is defined in the object itself, not in parent
219 if (dependencies.hasOwnProperty(key)) {
220 // only copy dist directory, if it exists
221 // skip this if for dwv dependency
222 if (key != 'dwv' && fs.existsSync('node_modules/' + key + '/dist')) {
223 gulp.src('node_modules/' + key + '/dist/**/*')
224 .pipe(gulp.dest(config.dist.assets + key + '/dist'));
226 gulp.src('node_modules/' + key + '/**/*')
227 .pipe(gulp.dest(config.dist.assets + key));
233 gulp.task('watch', function () {
234 // watch all changes and re-run styles
235 gulp.watch('./interface/**/*.scss', { interval: 1000, mode: 'poll' }, ['styles']);
237 // watch all changes to css/php files in themes and copy to public
238 return watch('./interface/themes/*.{css,php}', { ignoreInitial: false })
239 .pipe(gulp.dest(config.dest.themes));
242 gulp.task('sync-only', function () {
244 proxy: "127.0.0.1:" + config.proxy,
251 * - runs by default when `gulp` is called from CLI
253 if (config.install) {
254 gulp.task('default', ['install']);
255 } else if (config.syncOnly && config.proxy) {
256 gulp.task('default', ['sync-only', 'watch']);
258 gulp.task('default', function (callback) {
259 runSequence('clean', ['sync'], 'rtl:setup', 'rtl_styles', 'rtl:teardown', callback);