Gulp task to delete empty files

When compiling TypeScript, I get a lot of empty JavaScript files generated because their TypeScript partners only contain interfaces. There is currently no option tscto suppress the generation of these files. I use the plugin gulp-tscto compile.

Is there a plugin or some other means to clean up empty files or even a more general purpose gulp that will allow me to delete files based on their name and contents? Or is there a way to do this in gulp without using plugins?

+4
source share
3 answers

, node-glob gulp, :

var glob = require('node-glob'),
    fs = require('fs);

gulp.task('delete-empty-files', function(cb) {
    glob('/path/to/generated/**/*.js', function(err, files) {
        files.forEach(function(file) {
            if(fs.statSync(file).size === 0) {
                fs.unlinkSync(file);
            }
        });
        // make sure the task runs asynchronously!
        cb();
    });
});

gulp-tap , :

var tap = require('gulp-tap'),
    fs = require('fs);

gulp.task('delete-empty-files', function() {
    return gulp.src('/path/to/generated/**/*.js')
        .pipe(tap(function(file) {
            if(file.stat.size === 0) {
                fs.unlinkSync(file);
            }
        });
    });
});

. , , gulp vinyl-fs.

, {read: false} gulp.src(), , .stat. , :

return gulp.src('/path/to/files/**/*.js', {read: false}).pipe(...)
+3

.d.ts .ts. .

+1

, , , gulp-clip-empty-files .

Delete empty files from the stream. This prevents errors on some other plugins like gulp-sass and can be useful for removing placeholders.

The use is pretty simple ...

var gulp = require('gulp');
var clip = require('gulp-clip-empty-files');

gulp.task('default', function () {
    return gulp.src('src/*.scss')
        .pipe(clip())
        .pipe(gulp.dest('dist'));
});

In this example, all empty files .scsswill be “trimmed” (ignored?) Before recording

0
source

Source: https://habr.com/ru/post/1530338/


All Articles