【问题标题】:Gulp: How to change modification time of fileGulp:如何更改文件的修改时间
【发布时间】:2015-04-04 06:14:24
【问题描述】:

我在下面有 gulp 任务,每次运行此任务时(在 browsersync 之前)我想更改一个文件的最后修改时间(该文件中没有任何更改,我只需要更改最后修改时间 - 替代 shell )触摸”命令)。有人可以告诉我最简单的方法是什么吗?谢谢!

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}));
});

【问题讨论】:

    标签: node.js gulp


    【解决方案1】:

    使用核心fs 模块的fs.utimes 函数,它是类似于Unix touch 命令的节点。您传递路径,如果您将 new Date() 作为 mtime 参数传递,那应该可以解决问题。

    【讨论】:

    • 关于如何在pipe() 中实现这一点的任何提示?
    • @WraithKenny 我建议在 gulp-touch-cmd github 存储库上打开一个问题,要求更新以支持 gulp v4。似乎该项目是 2 年前的一项一次性工作。
    【解决方案2】:

    你可以使用gulp-touch-cmd

    更改使用 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());
    });
    

    【讨论】:

    • 不喜欢这是使用默认任务,而是将 .pipe(touch()) 移动到我想要的任何文件!
    • 任务“默认”仅用于演示 :)
    【解决方案3】:

    gulp-touch-cmdgulp-touch 似乎在 Gulp 4 中不起作用,所以我编写了这个可行的小管道函数。 (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( '.' ) )
    

    编辑:一个更好的、可重复使用的示例

    ...
    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 ) );
    });
    

    【讨论】:

    • 这里的优点是它可以让 gulp 在写入文件时自己设置正确的时间,而不是事后手动触摸。
    • 这个解决方案还解决了我在切换到 Gulp 4.x 后的 PHPStorm 问题。 PHPStorm 不会显示由 Gulp 任务更改的文件的任何更新。将此代码添加到 Gulp 任务后,PHPStorm 已正确显示更新的文件。
    • 为什么file.statfile 有时可以是undefined?我不得不用if 包裹起来。否则工作正常
    【解决方案4】:

    Gulp 插件gulp-touch-fd 修复了gulp-touch 的 Gulp 4 兼容性问题

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

    例子:

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

    【讨论】:

    • 谢谢!在 Foundation 中使用它我只需要在 .pipe(browser.reload({ stream: true })); 之前的 gul-touch-fd 插件和 const touch = require('gulp-touch-fd');'. I then added it to the sass` 函数@
    【解决方案5】:

    如果我们只需要将日期设置为“现在”,gulp-touch-fd 可以工作,但如果我们需要将修改时间设置为更具体的内容(如touch -ttouch -r),则不行。

    为此我们可以使用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());
    });
    

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-15
        • 2022-07-02
        • 2014-03-11
        • 2017-03-30
        • 1970-01-01
        • 2010-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多