【问题标题】:Check if file exists before calling a task on Laravel Elixir 5.2在 Laravel Elixir 5.2 上调用任务之前检查文件是否存在
【发布时间】:2016-01-14 12:26:17
【问题描述】:

在尝试合并我在网站上使用的所有预编译前端库之前,我需要检查文件是否存在。

到目前为止,这是我的 gulpfile.js:

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');
});

mix.combine() 函数永远不会执行,尽管 mix 是一个有效的对象并且执行了 console.log 函数。

谁能告诉我这是否是一个有效的策略以及为什么它不起作用?

【问题讨论】:

    标签: laravel gulp laravel-elixir


    【解决方案1】:

    您可以使用 stat 来检查 Gulp 中是否存在文件:

     fs.stat('file.txt', function(err, stat) {
        if(err == null) {
            console.log('File exists');
        } else {
            console.log(err.code);
        }
     });
    

    【讨论】:

    • 问题是 mix.combine 在回调中不起作用。
    【解决方案2】:

    我正在使用 statSync,因为 mix.combine() 在回调中不起作用。

    我知道捕获异常并在之后采取有效措施是不对的,但到目前为止我还没有找到其他解决方案。

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

    【讨论】:

    • 尝试了各种各样的东西,这似乎是让事情顺利进行的唯一方法......棘手,但工作完成了!
    猜你喜欢
    • 2012-02-10
    • 2017-01-02
    • 2016-11-19
    • 2019-03-07
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    相关资源
    最近更新 更多