更改其他gulp的EventStream中的其他文件
我有一个有趣的问题。是否可以在gulp中更改OpenStream(EventStream)中的某些文件 我有那种东西。我想在打开的流中读取一些文件,然后将这些内容写入另一个文件。我怎么能做到?谢谢更改其他gulp的EventStream中的其他文件,gulp,Gulp,我有一个有趣的问题。是否可以在gulp中更改OpenStream(EventStream)中的某些文件 我有那种东西。我想在打开的流中读取一些文件,然后将这些内容写入另一个文件。我怎么能做到?谢谢 gulp.task('handleGlobalStorage', function () { return gulp.src('./global_storage.js') .pipe(setEnvHostAndProxy()) .pipe(gulp.dest('./built'))
gulp.task('handleGlobalStorage', function () {
return gulp.src('./global_storage.js')
.pipe(setEnvHostAndProxy())
.pipe(gulp.dest('./built'));
});
function setEnvHostAndProxy() {
return es.map(function(file, cb) {
var fileContent = file.contents.toString();
//some changes inside content
// if (!gutil.env.dev) return;
/* I have stuff that fetched from file and I modify it that I send it
down to pipe. But also I want to insert this stuff inside other
file. How I can do it? Should I create WritableStream of that file
and merge it ?*/
file.contents = new Buffer(fileContent);
// send the updated file down the pipe
cb(null, file);
});
}
我解决了这个问题,这里是解决方案(我不会显示所有代码,只显示一般代码)。主要概念是-打开文件并在管道中写入新内容:)仅此而已
function setEnvHostAndProxy() {
var bsConfigFileContent = fs.readFileSync('./bs-config.js', 'utf8'),
bsConfigProxyPattern = /(proxyUrl\s?=\s?)["|'](.*)["|']/;
return es.map(function (file, cb) {
var fileContent = file.contents.toString(),
prodHost = fileContent.match(generateRegExpForHosts('prodHost'))[1],
prodProxy = fileContent.match(generateRegExpForHosts('prodProxy'))[1],
devProxy = fileContent.match(generateRegExpForHosts('devProxy'))[1],
devHost = fileContent.match(generateRegExpForHosts('devHost'))[1],
changedBsConfigFileStream,
res;
if (!gutil.env.dev) {
res = prodHandler();
changedBsConfigFileStream = bsConfigHandler(prodHost);
} else {
res = devHandler();
changedBsConfigFileStream = bsConfigHandler(devProxy);
}
fs.writeFile('./bs-config.js', changedBsConfigFileStream, 'utf8', function (err) {
if (err) throw (err);
});
file.contents = new Buffer(res);
cb(null, file);
} });
我解决了这个问题,这里是解决方案(我不会显示所有代码,只显示一般代码)。主要概念是-打开文件并在管道中写入新内容:)仅此而已
function setEnvHostAndProxy() {
var bsConfigFileContent = fs.readFileSync('./bs-config.js', 'utf8'),
bsConfigProxyPattern = /(proxyUrl\s?=\s?)["|'](.*)["|']/;
return es.map(function (file, cb) {
var fileContent = file.contents.toString(),
prodHost = fileContent.match(generateRegExpForHosts('prodHost'))[1],
prodProxy = fileContent.match(generateRegExpForHosts('prodProxy'))[1],
devProxy = fileContent.match(generateRegExpForHosts('devProxy'))[1],
devHost = fileContent.match(generateRegExpForHosts('devHost'))[1],
changedBsConfigFileStream,
res;
if (!gutil.env.dev) {
res = prodHandler();
changedBsConfigFileStream = bsConfigHandler(prodHost);
} else {
res = devHandler();
changedBsConfigFileStream = bsConfigHandler(devProxy);
}
fs.writeFile('./bs-config.js', changedBsConfigFileStream, 'utf8', function (err) {
if (err) throw (err);
});
file.contents = new Buffer(res);
cb(null, file);
} });
你需要更具体一些。另一个文件是否已经存在,或者您只是想从头创建一个新文件?您想同时将另一个文件写入
/builded
文件夹还是其他地方?谢谢Sven的回答。实际上文件存在,我只是想在不导入它们的情况下对其进行更改,但应该导入主链。您需要更具体一些。另一个文件是否已经存在,或者您只是想从头创建一个新文件?您想同时将另一个文件写入/builded
文件夹还是其他地方?谢谢Sven的回答。实际上文件存在,我只是想在不导入它们的情况下对其进行更改,但应该导入main strem。