Vinyl-ftp上传成功事件?

时间:2019-06-05 11:09:59

标签: node.js gulp

我们当前在没有服务器的情况下遇到了一个奇怪的问题,即有时文件不是第一次上传。所以我要在我们的gulpfile中添加一个后备,以在失败时重试。

但是,我想显示一条消息,指示该消息何时成功执行,我通常使用.on('env', fn)进行操作,但是即使出现错误,它也会触发。

gulp是否可以检测文件是否成功上传?

const files = [
    { glob: 'templates/*.tpl', base: 'templates/', dest: '/templates/' + theme.folder },
    { glob: 'css-src/*.css', base: 'templates/', dest: '/templates/' + theme.folder },
    { glob: 'js-src/min/**/*-min.js', base: 'templates/', dest: '/templates/' + theme.folder },
    {
        glob: [
            'img-src/theme-compressed/*.png', 'img-src/theme-compressed/*.jpg',
            'img-src/theme-compressed/*.gif', 'img-src/theme-compressed/*.jpeg',
            'img-src/theme-compressed/*.svg'
        ],
        base: 'img-src/theme-compressed/', dest: '/img-src/' + theme.folder + '/theme'
    },
    {
        glob: [ 'img-src/cars-compressed/*.png', 'img-src/cars-compressed/*.jpg', 'img-src/cars-compressed/*.jpeg' ],
        base: 'img-src/theme-compressed/', dest: '/img-src/' + theme.folder + '/theme'
    },
];

function uploadFiles(event, index, group) {
    gulp.src([ event.path ], { base: group.base, buffer: false })
    .pipe(ftp.dest(group.dest)
        .on('error', function () {
            retry[index]++;
            if (retry[index] <= 3) {
                console.log('Failed to upload template, retrying... (' + retry[index] + ')');
                uploadFiles(event, index, group);
            }
        })
        .on('end', function() {
            console.log('file uploaded?');
        })
    )
}

files.forEach((group, index) => {
    retry[index] = 0;
    $.gulpWatch(group.glob, { awaitWriteFinish: true }, (event) => {
        $.fsExtra.pathExists(event.path, (err, exists) => {
            if (exists) {
                uploadFiles(event, index, group);
            }
        });
    });
});

您可以看到,我只允许它运行3次,但无论如何它通常第二次都可以运行。问题是即使失败,我也要击中.on('end', fn)

0 个答案:

没有答案