Gulp - 复制并重命名一个文件 [英] Gulp - copy and rename a file

查看:403
本文介绍了Gulp - 复制并重命名一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Gulp非常陌生。我基本上试图观察一个修改后的JavaScript文件,然后用一个新名称制作一个新的副本。 (最终会有一些处理,但罗马不是一天建成的)。



我的(天真)尝试是这样的:

  gulp.task('default',function(){

return gulp.watch('../** /**.js',function(obj){
gulp.src(obj.path)
.pipe(gulp.dest('foobar.js'));
});

});

这将修改后的文件成功复制到一个名为foobar.js的文件夹中。有什么简单的,我可以替换 gulp.dest('foobar.js'),那会简单地复制和重命名src文件到位?






编辑

通过复制,我的意思是我想要修改文件,并用新名称在目前的地方复制一份。相当于单击文件(在Windows中)并按下control-c control-v,然后重命名生成的文件。 解决方案

我不是100%确定你的意思是什么


复制并重新命名...到位


但是,根据您当前的代码,如果您只想:


  1. 观察目录中的所有 .js 文件并将它们复制到 cwd (当前工作目录)和

  2. 命名所有副本,不管源文件, 相同的东西
  3. 然后您可以使用 / package / gulp-renamerel =noreferrer> gulp-rename 即可:

    gulp = require('gulp');
    var rename = require('gulp-rename');

    gulp.task('default',function(){
    return gulp.watch('../**/**。js',function(obj){
    gulp.src(obj.path)
    .pipe(rename('newFileName.js'))
    .pipe(gulp.dest('。'));
    });
    });

    在这种情况下,输出文件名是 newFileName.js $ b

    为了使用这个模块,你需要安装 gulp-rename 软件包npm(即: npm install gulp-rename )。



    更多示例可在npm @ https://www.npmjs.com/package/gulp-rename#usage


    I'm extremely new to Gulp. I'm basically trying to watch for a modified JavaScript file, and then make a new copy of it with a new name. (eventually there'll be some processing on it, but Rome wasn't built in a day).

    My (naive) attempt is this:

    gulp.task('default', function() {
    
        return gulp.watch('../**/**.js', function(obj){
            gulp.src(obj.path)
                .pipe(gulp.dest('foobar.js'));
        });
    
    });
    

    This takes the modified file and successfully copies it into a folder now called foobar.js. Is there anything simple I can replace gulp.dest('foobar.js') with that will simply copy and rename the src file in place?


    EDIT

    By copy in place, I mean I want to take the modified file, and make a copy of it right where it currently is with a new name. The equivalent of clicking the file (in windows) and hitting control-c control-v, then renaming the resulting file.

    解决方案

    I'm not 100% certain what you mean by

    copy and rename ... in place

    But, based on your current code, if you simply wish to:

    1. Watch all .js files in the parent directory and
    2. Copy them to the cwd (current working directory) and
    3. Name all copies, regardless of source file, the same thing

    Then you could use gulp-rename to do just that:

    var gulp = require('gulp');
    var rename = require('gulp-rename');
    
    gulp.task('default', function() {
      return gulp.watch('../**/**.js', function(obj) {
        gulp.src(obj.path)
          .pipe(rename('newFileName.js'))
          .pipe(gulp.dest('.'));
      });
    });
    

    In this case, the output filename is newFileName.js

    In order to use the module, you'll need to install the gulp-rename package with npm (ie: npm install gulp-rename).

    More examples are available on the package details page on npm @ https://www.npmjs.com/package/gulp-rename#usage

    这篇关于Gulp - 复制并重命名一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆