user2595304
user2595304

Reputation: 236

Gulp: How to change modification time of file

I have gulp task below and with every run of this task (before browsersync) I want to change last modification time of one file (nothing changes in that file, I just need to change time of last modification - alternative to shell "touch" command). Can somebody tell me what's the easiest way to do that 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}));
});

Upvotes: 15

Views: 6437

Answers (7)

Jason Parker
Jason Parker

Reputation: 21

Using Vinyl directly, to avoid extra dependencies:

// Install the 'vinyl' package.
// Install the '@types/vinyl' package (TypeScript).
// Replace 'import' syntax with 'require' syntax if not using ESM.

import { Transform } from 'stream';
import vinyl from 'vinyl';

const touch = () => new Transform({
    readableObjectMode: true,
    writableObjectMode: true,
    transform(chunk, encoding, callback) {
        if (vinyl.isVinyl(chunk) && chunk.stat) {
            chunk.stat.mtime = chunk.stat.ctime = new Date();
        }
        callback(null, chunk);
    },
});

gulp.task('example', () => {
    gulp.src(pathSrc)
        .pipe(touch())
        .pipe(gulp.dest(pathDest));
});

Upvotes: 1

James Andino
James Andino

Reputation: 25779

fs.writeFileSync('sporks','','utf8','a')

Upvotes: 0

WraithKenny
WraithKenny

Reputation: 1041

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

const through2 = require( '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( '.' ) )

Edit: A better, reusable example

...
const through2 = require( 'through2' );    

const touch = () => through2.obj( function( file, enc, cb ) {
    if ( file.stat ) {
        file.stat.atime = file.stat.mtime = file.stat.ctime = new Date();
    }
    cb( null, file );
});

...

gulp.task( 'sass', function() {
    return gulp.src( pkg.sass.dir )
        .pipe( sourcemaps.init() )
        .pipe( sass() )
        .pipe( postcss() )
        .pipe( sourcemaps.write() )
        .pipe( touch() )
        .pipe( gulp.dest( pkg.css.dir ) );
});

Upvotes: 9

Jimb Esser
Jimb Esser

Reputation: 11

If we only need the date set to "now", gulp-touch-fd works, but not if we need to set the modification time to something more specific (like touch -t or touch -r).

For that we can use gulp-touch-custom:

const gulp = require('gulp');
const touch = require('gulp-touch-custom');
const through = require('through2');
const Vinyl = require('vinyl');

function exampleproc() {
  return through.obj(function (file, encoding, cb) {
    // example: push a new output file that is timestamped to match its input
    this.push(new Vinyl({
      path: `${file.relative}.out`,
      contents: file.contents.reverse(),
      touch: file.stat.mtime, // Specify an optional Date to use here (copy from input file)
    }));
    // example: Set an explicit date
    file.touch = new Date('2000-01-01');
    cb(null, file);
  });
}

gulp.task('example', function () {
  return gulp.src('./**/*.in')
    .pipe(exampleproc())
    .pipe(gulp.dest('./dest'))
    .pipe(touch());
});

Upvotes: 1

UnSeulT
UnSeulT

Reputation: 73

The Gulp plugin gulp-touch-fd fix the Gulp 4 compatibility issue of 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

Upvotes: 7

Peter Lyons
Peter Lyons

Reputation: 146064

Use the core fs module's fs.utimes function, which is the node analog to the Unix touch command. You pass the path and if you pass a new Date() as the mtime parameter, that should do the trick.

Upvotes: 8

xiaoyu2er
xiaoyu2er

Reputation: 166

simply you can use gulp-touch-cmd

Change file access and modification times of files copied using 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());
});

Upvotes: 3

Related Questions