Completed
Push — master ( f69ac7...8beccd )
by Megh
05:55
created

gulpfile.js (1 issue)

1
var gulp = require('gulp');
2
var autoprefixer = require('gulp-autoprefixer');
3
var sass = require('gulp-sass');
4
var plumber = require('gulp-plumber');
5
var notify = require('gulp-notify');
6
var path = require('path');
7
var del = require('del');
8
var gutil = require('gulp-util');
9
var args = require('yargs').argv;
10
var gulpif = require('gulp-if');
11
var minifyCss = require('gulp-minify-css');
12
var uglifyjs = require('gulp-uglify');
13
14
var paths = {
15
    styles: 'src/static/styles/**/*.{scss,css}',
16
    scripts: 'src/static/scripts/**/*.js',
17
    images: 'src/static/images/**/*.{jpg,jpeg,png,svg,gif}',
18
    fonts: 'src/static/fonts/*',
19
    php: 'src/**/*.{php,json,pem}',
20
    stuff: ['src/.htaccess', 'src/humans.txt', 'src/robots.txt', 'src/favicon.ico'],
21
    locale: ['src/locale/**/*'],
22
    vendor: ['vendor/**/*'],
23
};
24
25
var destination = args.deploy ? 'deploy' : 'build';
26
27
var reportError = function(error) {
28
    if ('CI' in process.env && process.env.CI === 'true') {
29
        process.exit(1);
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
30
    }
31
    notify({
32
        title: error.plugin + ' failed!',
33
        message: error.message
34
    }).write(error);
35
36
    // Prevent the 'watch' task from stopping
37
    this.emit('end');
38
};
39
40
gulp.task('clean', function() {
41
    // Must be synchronous if we're going to use this task as a dependency
42
    del.sync(destination + '/');
43
});
44
45
gulp.task('styles', function() {
46
    return gulp.src(paths.styles)
47
        .pipe(plumber({
48
            errorHandler: reportError
49
        }))
50
        .pipe(sass())
51
        .on('error', reportError)
52
        .pipe(autoprefixer())
53
        .pipe(gulpif(args.deploy, minifyCss()))
54
        .pipe(gulp.dest(destination + '/static/styles'));
55
});
56
57
gulp.task('scripts', function() {
58
    return gulp.src('src/static/scripts/**/*.js')
59
        .pipe(gulpif(args.deploy, uglifyjs()))
60
        .pipe(gulp.dest(destination + '/static/scripts'));
61
});
62
63
gulp.task('images', function() {
64
    return gulp.src(paths.images)
65
        .pipe(gulp.dest(destination + '/static/images'));
66
});
67
68
gulp.task('fonts', function() {
69
    return gulp.src(paths.fonts)
70
        .pipe(gulp.dest(destination + '/static/fonts'));
71
});
72
73
gulp.task('php', function() {
74
    return gulp.src(paths.php)
75
        .pipe(gulp.dest(destination + '/'));
76
});
77
78
gulp.task('stuff', function() {
79
    return gulp.src(paths.stuff)
80
        .pipe(gulp.dest(destination + '/'));
81
});
82
83
gulp.task('locale', function() {
84
    return gulp.src(paths.locale, {dot: true})
85
        .pipe(gulp.dest(destination + '/locale'));
86
});
87
88
gulp.task('vendor', function() {
89
    return gulp.src(paths.vendor, {dot: true})
90
        .pipe(gulp.dest(destination + '/vendor/'));
91
});
92
93
gulp.task('watch', ['default'], function() {
94
    function deleter() {
95
        var replaceFunc = null;
96
        if (typeof arguments[0] === 'string') {
97
            var before = arguments[0];
98
            var after = arguments[1];
99
            replaceFunc = function(file) {
100
                return file.replace(before, after);
101
            };
102
        }
103
        else if (typeof arguments[0] === 'function') {
104
            replaceFunc = arguments[0];
105
        }
106
        return function(event) {
107
            if (event.type == 'deleted') {
108
                var file = path.relative('./', event.path);
109
                if (typeof replaceFunc === 'function') {
110
                    file = replaceFunc(file);
111
                }
112
                del(file);
113
                gutil.log('Deleted file', '\'' + file + '\'');
114
            }
115
        };
116
    }
117
118
    var srcToBuildDeleter = deleter('src/', destination + '/');
119
120
    gulp.watch(paths.styles, ['styles'])
121
        .on('change', deleter(function(file) {
122
            return file.replace('src/', destination + '/')
123
                .replace(/\.scss$/, '.css');
124
        }));
125
    gulp.watch(paths.scripts, ['scripts'])
126
        .on('change', srcToBuildDeleter);
127
    gulp.watch(paths.images, ['images'])
128
        .on('change', srcToBuildDeleter);
129
    gulp.watch(paths.fonts, ['fonts'])
130
        .on('change', srcToBuildDeleter);
131
    gulp.watch(paths.php, ['php'])
132
        .on('change', srcToBuildDeleter);
133
    gulp.watch(paths.stuff, ['stuff'])
134
        .on('change', srcToBuildDeleter);
135
    gulp.watch(paths.locale, ['locale'])
136
        .on('change', srcToBuildDeleter);
137
    gulp.watch(paths.vendor, {dot: true})
138
        .on('change', deleter('vendor/', destination + '/vendor/'));
139
});
140
141
gulp.task('build', ['styles', 'scripts', 'images', 'fonts', 'php', 'stuff', 'locale', 'vendor']);
142
143
gulp.task('default', ['clean'], function() {
144
    gulp.run('build', function(){
145
        if (args.deploy) {
146
            del.sync(['deploy/.htaccess', 'deploy/index.php', 'deploy/app/config.php']);
147
        }
148
    });
149
});
150