简体   繁体   English

无法让 gulp-jscs 在原件上写入固定文件

[英]Can't get gulp-jscs to write fixed files over originals

No problems running the gulp-jscs plugin to analyze my code using the following task.使用以下任务运行gulp-jscs插件来分析我的代码没有问题。 However when I add fix:true , nothing changes in the files and JSCS now acts like it isn't running at all... when I remove the object passed into the JSCS plugin and remove the gulp.dest() line, it will report code style issues.但是,当我添加fix:true ,文件中没有任何变化,JSCS 现在就像它根本没有运行一样......当我删除传递给 JSCS 插件的对象并删除gulp.dest()行时,它会报告代码风格问题。

So, it seems either passing in the fix:true or tying to write back the fixed files causes JSCS to short circuit internally... ideas?因此,似乎要么传入fix:true要么绑定回写固定文件导致 JSCS 在内部短路......想法?

var $ = require('gulp-load-plugins')({lazy: true});
gulp.task('vet', function () {
  log('Analyzing source with JSHint and JSCS');

  return gulp
    .src(config.allJs, {base: './'})
    .pipe($.jshint())
    .pipe($.jshint.reporter('jshint-stylish', {verbose: true}))
    .pipe($.jscs({fix:true}))
    .pipe(gulp.dest('./'));
});

Update 2更新 2

I have found a simpler/cleaner way to do, just specify the base parameter我找到了一种更简单/更清洁的方法,只需指定基本参数

gulp.src(files, {base: './'})

then file.name will contain the full path so you can output to the current directory然后 file.name 将包含完整路径,以便您可以输出到当前目录

gulp.dest('.')

so at the end:所以最后:

gulp.src(files, {base: './'})
    .pipe(jscs({fix: true}))
    .pipe(jscs.reporter())
    .pipe(jscs.reporter('fail'))
    .pipe(gulp.dest('.'))
    .on('error', console.error.bind(console));

Update 1更新 1

I have been tackling this problem with this piece of code:我一直在用这段代码解决这个问题:

var gulp = require('gulp');
var jscs = require('gulp-jscs');
var foreach = require('gulp-foreach');
var rimraf = require('gulp-rimraf');

gulp.task('jscs', () => {
return gulp.src(['file1.js', 'file2.js'])
    .pipe(foreach((stream, file) => {
        return stream
            .pipe(jscs({ fix: true }))
            .pipe(jscs.reporter())
            //.pipe(jscs.reporter('fail'))
            .pipe(rimraf())
            .pipe(gulp.dest(file.base));
    }));
});

dependencies:依赖:

npm install --save-dev gulp gulp-rimraf gulp-foreach gulp-jscs

Globally, gulp-foreach iterates over every src with its own stream, rimraf delete the file before writing on vinyl.base destination.在全局范围内,gulp-foreach 使用自己的流迭代每个 src,rimraf 在写入vinyl.base 目标之前删除文件。

Let me know if it helps.如果有帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM