Gulp: how to change file modification time

I have a gulp task below and every time I run this task (before browsersync). I want to change the last modification time of one file (nothing changes in this file, I just need to change the time of the last modification - an alternative to shell "touch"). Can someone tell me the easiest way to do this, please? Thanks!

gulp.task('sass', function() { return sass(pkg.sass.dir, {sourcemap: true, precision: 10}) .on('error', function (err) { onError(err); }) .pipe(autoprefixer({ browsers: ['last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'], cascade: true })) .pipe(gulp.dest(pkg.css.dir)) .pipe(browsersync.reload({stream:true})); }); 
+8
source share
4 answers

Use the core fs fs.utimes module , which is the analogue of node for the Unix touch command. You pass the path, and if you pass new Date() as the mtime parameter, this should do the trick.

+8
source

gulp-touch-cmd and gulp-touch don't seem to work with Gulp 4, so I wrote this little tube function that works. ( npm add through2 )

 // example .pipe( through2.obj( function( file, enc, cb ) { let date = new Date(); file.stat.atime = date; file.stat.mtime = date; cb( null, file ); }) ) .pipe( gulp.dest( '.' ) ) 
+1
source

just you can use gulp-touch-cmd

Change file access time and file modification time of files copied with gulp

 npm install --save-dev gulp-touch-cmd var gulp = require('gulp'); var touch = require('gulp-touch-cmd'); gulp.task('default', function() { gulp .src('./src/**/*') .pipe(gulp.dest('./dest')) .pipe(touch()); }); 
0
source

Gulp gulp-touch-fd plugin fixes Gulp 4 compatibility issue with gulp-touch

 npm install --save-dev gulp-touch-fd@funkedigital /gulp-touch-fd 

Example:

 const {src, dest} = require('gulp'); const touch = require('gulp-touch-fd'); function copyAndTouch() { return src('./src/**/*') .pipe(dest('./dest')) .pipe(touch()); }; exports.copyAndTouch = copyAndTouch 
0
source

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


All Articles