Gulp:如何更改文件的修改时间

问题描述 投票:0回答:7

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

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
7个回答
9
投票
从 Gulp 4 开始,

gulp-touch-cmd
gulp-touch
似乎不起作用,所以我编写了这个可以起作用的小管道函数。 (
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 ) );
});

8
投票

使用核心

fs
模块的 fs.utimes 函数,该函数是类似于 Unix
touch
命令的节点。您传递路径,如果您传递
new Date()
作为
mtime
参数,那就可以了。


6
投票

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

3
投票

只需使用 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());
});

1
投票

如果我们只需要将日期设置为“现在”,则

gulp-touch-fd
可以工作,但如果我们需要将修改时间设置为更具体的时间(例如
touch -t
touch -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());
});

0
投票

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


0
投票

直接使用Vinyl,避免额外的依赖:

// 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));
});
© www.soinside.com 2019 - 2024. All rights reserved.