3

I'm using Gulp in a VS2015 project to run jscs on JavaScript files with the fix option set. The intention is to modify the same file that is read (viz., source and destination are the same).

var gulp = require('gulp');
var jscs = require('gulp-jscs');
var chmod = require('gulp-chmod');
var exec = require('gulp-exec');

var ourJsFiles = // an array of files and globbed paths 

gulp.task('jscs', function (callback) {
   ourJsFiles.forEach(function (fn) {
      gulp.src(fn, { base: './' })
         .pipe(jscs({
            "preset": "google",
            "maximumLineLength": 160,
            "validateIndentation": 3,
            "fix": true
         }))
         .pipe(gulp.dest('./'));
   });
   callback();
});

But I do not want to process any files that are read-only. Is there already a way to detect this in Gulp on Windows?

jltrem
  • 12,124
  • 4
  • 40
  • 50

1 Answers1

0

There is a plugin which allows you to work with subset of files: gulp-filter. One of options is to pass filter function which will receive vinyl file object, so for e.g. you could use stat.mode property of that object which holds permissions and do something like:

var filter = require('gulp-filter');
...
var writableFiles = filter(function (file) {
        //https://github.com/nodejs/node-v0.x-archive/issues/3045
        var numericPermission = '0'+(e.stat.mode & parseInt('777', 8)).toString(8);
        return numericPermission[1]==='6'
    });
...
gulp.src(....)
    .pipe(writableFiles)
lukbl
  • 1,763
  • 1
  • 9
  • 13