'use strict'; var minimist = require('minimist'); var fs = require('fs'); var lazypipe = require('lazypipe'); var babel = require('gulp-babel'); var apidoc = require('gulp-apidoc'); // command line arguments var varg = minimist(process.argv.slice(2), { string: 'env', default: {env: process.env.NODE_ENV || 'debug'} }); var options = { opt: varg.env !== 'debug' || varg.release, // optimize inc: !varg.force, // incremental build beauty: varg.beauty || varg.rainbow, dst: varg.dst }; // gulp // common var gulp = require('gulp'); var changed = require('gulp-changed'); var through = require('through'); var empty = function () { return through(function (data) { this.emit('data', data); }, function () { this.emit('end'); }); }; // var plumber = require('gulp-plumber'); // var gulpif = require('gulp-if'); // var rename = require('gulp-rename'); //переименовывание // var watch = require('gulp-watch'); // var filter = require('gulp-filter'); // css/less var less = require('gulp-less'); var prefix = require('gulp-autoprefixer'); //префиксы var minifyCss = require('gulp-minify-css'); //минификация // var concatCss = require('gulp-concat-css'); //конкатенация // js var uglify = require('gulp-uglify'); var prettify = options.beauty ? require('gulp-jsbeautifier') : empty; var paths = new (function () { var self = this; self.root = '.'; var src = '.'; var dst = options.dst || self.root + '/~dev_rating'; var app = self.root + '/~dev_rating'; var media = src + '/media'; var stat = dst + '/static'; self.src = { css: media + '/css/**/*.css', less: media + '/less/**/*.less', js: media + '/js/**/*.js', config: src + '/deploy/phpConfig/**/*', css_components: media + '/components/**/*.css', js_components: media + '/components/**/*.js', twig_components: media + '/components/**/*.twig' }; self.dst = { css: stat + '/css/', less: stat + '/css/', js: stat + '/js/', config: dst + '/application/config/', css_components: stat + '/components/', js_components: stat + '/components/', twig_components: stat + '/components/' }; })(); // ============================= // pipes // ============================= var pipes = (function () { var cssPipe = lazypipe() .pipe(prefix, 'last 2 versions', '> 1%', 'ie9') //.pipe(rename, {suffix: '.min'}) .pipe(options.opt ? minifyCss : prettify); var lessPipe = lazypipe() .pipe(less) .pipe(cssPipe); var jsPipe = lazypipe() .pipe(babel, { presets: ['es2015-without-strict'], ignore: /js\/lib/ }) .pipe(options.opt ? uglify : prettify); var twigPipe = empty; return { css: cssPipe, less: lessPipe, js: jsPipe, css_components: cssPipe, js_components: jsPipe, twig_components: twigPipe } })(); var ext = { css: '.css', less: '.less', js: '.js', 'css_components': '.css', 'js_components': '.js', 'twig_components': '.twig' }; // ============================= // helpers // ============================= var constructHandler = function (msg) { return function () { process.stdout.write('>>> ' + msg + '\n'); }; }; var getConfig = function (type, src, dst, pipe) { var _src = src || paths.src[type]; var _dst = dst || paths.dst[type]; var _pipe = pipe || pipes[type]; return { src: _src, dst: _dst, pipe: _pipe, changed: options.inc, ext: ext[type], error: constructHandler(type + ' error') }; }; var constructTask = function (config) { var dump = function (e) { throw e; }; var errorHandler = config.error || dump; var midPipe = config.pipe || empty; var ext = config.ext ? {extension: config.ext} : {}; var chgPipe = config.changed ? lazypipe().pipe(changed, config.dst, ext) : empty; return gulp.src(config.src) .pipe(chgPipe()) .pipe(midPipe()) .on('error', errorHandler) .pipe(gulp.dest(config.dst)); }; var constructWatch = function (config) { gulp.watch(config.src, function (event) { constructTask(config); }); }; // ============================= // rules // ============================= gulp.task('less:compile', function () { return constructTask(getConfig('less')); }); gulp.task('css:copy', function () { return constructTask(getConfig('css')); }); gulp.task('js:copy', function () { return constructTask(getConfig('js')); }); gulp.task('config:copy', function () { return constructTask({ src: paths.src.config, dst: paths.dst.config }); }); gulp.task('components:copy', function () { constructTask(getConfig('css_components')); constructTask(getConfig('twig_components')); return constructTask(getConfig('js_components')); }); // todo: gulp.task('folders:create', function () { var app = paths.root + '/~dev_rating/application'; var dirs = [ app + '/logs/', app + '/static/', app + '/static/components/' ]; dirs.forEach(function (dir) { try { fs.mkdirSync(dir); } catch (err) { if (err.code !== 'EEXIST') { throw err; } } }); }); // ============================= // watch // ============================= gulp.task('watch', function (event) { constructWatch(getConfig('less', event.path)); constructWatch(getConfig('css', event.path)); constructWatch(getConfig('js', event.path)); constructWatch(getConfig('css_components', event.path)); constructWatch(getConfig('js_components', event.path)); constructWatch(getConfig('twig_components', event.path)); }); // ============================= // common tasks // ============================= gulp.task('config', [ 'config:copy' ]); gulp.task('default', [ 'less:compile', 'css:copy', 'js:copy', 'components:copy' ]); gulp.task('install', [ 'folders:create', 'config', 'default' ]); gulp.task('apidoc', function(done){ apidoc({ src: "~dev_rating/application/classes", dest: "apidoc/", config: "./" },done); });