gulp task to process files that are writable

北城以北 提交于 2019-12-10 19:43:24

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/31841664/gulp-task-to-process-files-that-are-writable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!