Reputation: 1016
I'm trying to implement an abstraction layer over the Gulp API by allowing easy configuration over a dedicated file.
To sum it up, I have a function (builder
) that requires that file (gulpconfig.js
) and calls functions that return ReadableStream
s from an object the config file exports with parallel()
.
Here's the gist (simplified from my private source).
However, when I do gulp build
, it doesn't call the TestHandler
function as it should. After debugging for a while, I have identified that everything (including variable assignments etc) work perfectly fine, but it just won't execute the returned parallel()
instance.
How can I fix this behaviour?
Here are the code snippets from GitHub Gist:
gulpfile.js
:
/* REQUIRES */
const { parallel } = require('gulp');
const user = require('./gulpconfig');
let params = {};
let fns = [];
async function builder() {
for (let [name, props] of Object.entries(user.config)) {
let fnName = name;
let fn = user[fnName];
let src = props.src;
let dest = props.dest;
params[fnName] = { src, dest };
fns.push(fn);
}
user.paramCp(params);
return parallel(...fns);
}
exports.build = builder;
gulpconfig.js
:
/* REQUIRES */
const { src, dest, on, watch } = require('gulp');
const pipeline = require('readable-stream');
/* USER CONFIG */
exports.config = {
testHandler: {
src: "./input/*",
dest: "./output/",
},
};
/* LOCAL PARAM HANDLER. USED INTERNALLY. */
var params;
exports.paramCp = _params => {
params = _params;
};
var tSrc;
var tDest;
function getParams() {
let obj = params[getParams.caller.name];
tSrc = obj.src;
tDest = obj.dest;
}
/* HANDLERS. DEFINED BY handler KEY IN CONFIG */
exports.testHandler = function testHandler() {
getParams(); // You can now use tSrc and tDest
return pipeline(src(tSrc), dest(tDest));
};
Upvotes: 4
Views: 194
Reputation: 11397
This is expected for Gulp.
gulp.parallel
and gulp.series
return functions (which will concurrently and serially execute their arguments, respectively). They are meant to be used in task definitions (where a function is expected) or called directly.
See this GitHub issue for more details and explanation.
Execute the function returned by parallel
return parallel(...fns)();
pipeline
It looks like you're trying to use pipeline
here, which is not a bad idea, but your pipeline
variable is not correctly defined.
const pipeline = require('readable-stream');
makes pipeline
of type Readable
. This is the default export of the readable-stream
package.
You probably want this
const { pipeline } = require('readable-stream');
Upvotes: 1