Gulp - 复制并重命名一个文件 [英] Gulp - copy and rename a file
问题描述
我的(天真)尝试是这样的:
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%确定你的意思是什么
复制并重新命名...到位
但是,根据您当前的代码,如果您只想:
- 观察父目录中的所有
.js
文件并将它们复制到cwd
(当前工作目录)和 - 命名所有副本,不管源文件, 相同的东西 然后您可以使用 / package / gulp-renamerel =noreferrer> gulp-rename 即可: gulp = require('gulp');
- Watch all
.js
files in the parent directory and - Copy them to the
cwd
(current working directory) and - Name all copies, regardless of source file, the same thing
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:
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屋!