Search code examples
javascriptgulp

Gulp- Make sure file has comments in the beginning


I have multiple javascript files in a folder and I want to make sure that every file has comment in the beginning (that will explain the summary of file).

/*
 This file will......
*/
function test () {
....
}

So is this possible using gulp-contains or something else?


Solution

  • By using the regex provided by @Sajjad in previous answer. I have managed to achieve my goal. I have used gulp-if and gulp-fail instead (I find it more flexible).

    Here is how I do that:

    var condition = function (file) {
        sFile = require('path').parse(file.path).name;
        var startWithComment = file.contents.toString().replace(/\n|\r/g, "").trim().startsWith("/*");
        return (!startWithComment);
    }
    
    
    gulp.task('taskName',
        function() {
            gulp.src('files/*.js')
                .pipe(gulpIf(condition, fail(function () {
                    var message = 'Some message';
                    return message;
                })));
        });