Ad

Check If File Exists Before Calling A Task On Laravel Elixir 5.2

- 1 answer

I need to check if a file exists before trying to combine all the pre-compiled frontend libraries that I'm using on my website.

That's my gulpfile.js so far:

var elixir     = require('laravel-elixir'),
    fs = require('fs'),
    assetsPath = './resources/assets/',
    bowerPath = assetsPath + 'bower/',
    cssPath = assetsPath + 'css/';

elixir(function(mix) {
    var cssLibraries = [
        bowerPath + 'bootstrap/dist/css/bootstrap.min.css',
        bowerPath + 'font-awesome/css/font-awesome.min.css',
        bowerPath + 'Ionicons/css/ionicons.min.css',
        bowerPath + 'AdminLTE/dist/css/AdminLTE.min.css',
        bowerPath + 'AdminLTE/dist/css/skins/skin-black-light.css',
        bowerPath + 'iCheck/skins/flat/blue.css',
        bowerPath + 'morris.js/morris.css',
        bowerPath + 'AdminLTE/plugins/jvectormap/jquery-jvectormap-1.2.2.css',
        bowerPath + 'bootstrap-datepicker/dist/css/bootstrap-datepicker3.min.css',
    ];
    fs.access(cssPath + '000-processed.css', function(err) {
        // if libraries are already combined I don't neet to do it again
        if (!err) {
            console.log('I will *NOT* combine pre-compiled CSS.');
            return;
        }
        console.log('I will combine pre-compiled CSS.');
        mix.combine(cssLibraries, cssPath + '000-processed.css');
    });
    mix.sass('**', cssPath + '001-sass.css');
});

The mix.combine() function is never executed, although mix is a valid object and the console.log function is executed.

Can somebody tell me if this is a valid strategy and why it is not working?

Ad

Answer

I'm using statSync because mix.combine() is not working inside a callback.

I know is not right to catch an exception and take a valid action after, but so far I have not found another solution.

elixir(function(mix) {
    try {
        fs.statSync(tmpPath + '000-compiledsss.css');
    } catch (err) {
        console.log("File do not exists, I will combine!");
    }
}
Ad
source: stackoverflow.com
Ad