Javascript Gulp - 复制和重命名文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28593660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:03:38  来源:igfitidea点击:

Gulp - copy and rename a file

javascriptgulpgulp-watch

提问by Adam Rackis

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

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

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?

这会将修改后的文件成功复制到现在称为 foobar.js 的文件夹中。有什么简单的可以替换的东西gulp.dest('foobar.js'),它可以简单地复制并重命名 src 文件吗?



EDIT

编辑

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

通过就地复制,我的意思是我想获取修改后的文件,并用新名称将其复制到当前所在的位置。相当于单击文件(在 Windows 中)并点击 control-c control-v,然后重命名生成的文件。

回答by knksmith57

I'm not 100% certain what you mean by

我不是 100% 确定你的意思

copy and rename ... in place

复制并重命名......就地

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

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

  1. Watch all .jsfiles in the parentdirectory and
  2. Copy them to the cwd(current working directory) and
  3. Name allcopies, regardlessof source file, the same thing
  1. 监视目录.js中的所有文件并
  2. 将它们复制到cwd(当前工作目录)和
  3. 命名所有副本,无论源文件如何,都一样

Then you could use gulp-renameto do just that:

然后你可以使用gulp-rename来做到这一点:

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

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

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

为了使用该模块,您需要gulp-rename使用 npm(即:)安装该软件包npm install gulp-rename

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

在 npm @ https://www.npmjs.com/package/gulp-rename#usage上的包详细信息页面上提供了更多示例

回答by Adam Rackis

It wasn't pretty getting there, but in the end it appears this is what I want (with some ES6 transpiling in the middle).

到达那里并不是很顺利,但最终似乎这就是我想要的(中间有一些 ES6 转译)。

The key appears to be the options object with a base property in the call to src. That seems to be what's needed to maintain the path of the current file in the call to dest.

关键似乎是在调用中具有基本属性的选项对象src。这似乎是在调用dest.

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

gulp.task('default', function() {
    return gulp.watch('../**/$**.js', function(obj){
        if (obj.type === 'changed') {
            gulp.src(obj.path, { base: './' })
                .pipe(babel())
                .pipe(rename(function (path) {
                    path.basename = path.basename.replace('$', '');
                }))
                .pipe(gulp.dest(''));
        }
    });
});