我下面有 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}));
});
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 ) );
});
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
只需使用 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());
});
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());
});
fs.writeFileSync('sporks','','utf8','a')
直接使用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));
});