最简单的copy
var gulp = require("gulp")
gulp.src(source_path).pipe(gulp.dest(desc_path));
或
require("gulp").src(source_path).pipe(gulp.dest(desc_path));
代码说明
- source_path是从哪里拷贝,可以是数组
- desc_path 是拷贝到哪里,只能是字符串
说明
gulp是基于stream的,所以性能非常好,而且可以检测copy进度哦
4 回复
如果單純只是需要「copy」不需要用到 gulp 的喔:
fs.createReadStream(source_path).pipe(fs.createWriteStream(dest_path))
@chinghanho 被拆穿了,哈哈
我有1点不确定,createReadStream可以传数组么?
node.js 內建的 fs.createReadStream
這組不行。
gulp
var vfs = require('vinyl-fs');
Gulp.prototype.src = vfs.src;
vinyl-fs
function src(glob, opt) {
var options = assign({
read: true,
buffer: true
}, opt);
var pass = through.obj();
if (!isValidGlob(glob)) {
throw new Error('Invalid glob argument: ' + glob);
}
// return dead stream if empty array
if (Array.isArray(glob) && glob.length === 0) {
process.nextTick(pass.end.bind(pass));
return pass;
}
var globStream = gs.create(glob, options);
// when people write to use just pass it through
var outputStream = globStream
.pipe(through.obj(createFile))
.pipe(getStats(options));
if (options.since) {
outputStream = outputStream
.pipe(filterSince(options.since));
}
if (options.read !== false) {
outputStream = outputStream
.pipe(getContents(options));
}
return outputStream.pipe(pass);
}
var gs = require('glob-stream');
glob-stream
see
https://github.com/wearefractal/glob-stream
Glob
var stream = gs.create(['./**/*.js', '!./node_modules/**/*']);
Globs are executed in order, so negations should follow positive globs. For example:
index.js
// creates a stream for multiple globs or filters
create: function(globs, opt) {
if (!opt) opt = {};
if (typeof opt.cwd !== 'string') opt.cwd = process.cwd();
if (typeof opt.dot !== 'boolean') opt.dot = false;
if (typeof opt.silent !== 'boolean') opt.silent = true;
if (typeof opt.nonull !== 'boolean') opt.nonull = false;
if (typeof opt.cwdbase !== 'boolean') opt.cwdbase = false;
if (opt.cwdbase) opt.base = opt.cwd;
// only one glob no need to aggregate
if (!Array.isArray(globs)) globs = [globs];
var positives = [];
var negatives = [];
globs.forEach(function(glob, index) {
if (typeof glob !== 'string' && !(glob instanceof RegExp)) {
throw new Error('Invalid glob at index ' + index);
}
var globArray = isNegative(glob) ? negatives : positives;
// create Minimatch instances for negative glob patterns
if (globArray === negatives && typeof glob === 'string') {
glob = new Minimatch(unrelative(opt.cwd, glob), opt);
}
globArray.push({
index: index,
glob: glob
});
});
if (positives.length === 0) throw new Error('Missing positive glob');
// only one positive glob no need to aggregate
if (positives.length === 1) return streamFromPositive(positives[0]);
// create all individual streams
var streams = positives.map(streamFromPositive);
// then just pipe them to a single unique stream and return it
var aggregate = new Combine(streams);
var uniqueStream = unique('path');
return aggregate.pipe(uniqueStream);
function streamFromPositive(positive) {
var negativeGlobs = negatives.filter(indexGreaterThan(positive.index)).map(toGlob);
return gs.createStream(positive.glob, negativeGlobs, opt);
}
}
};
// then just pipe them to a single unique stream and return it
var aggregate = new Combine(streams);
and
var Combine = require('ordered-read-streams');
ordered-read-streams
https://github.com/armed/ordered-read-streams
Combines array of streams into one read stream in strict order
这才是根源
哈哈