I have one npm package containing several files with several gulp task definitions.
What I want is in the main gulpfile, be able to copy these gulp files (from the package) and execute the gulp tasks defined in them.
Follows an example:
const gulp = require('gulp'); const fs = require('fs'); const path = require('path'); const gulpFolder = path.join(__dirname.replace('gulpfile.js', ''), 'src', 'generated-code', 'gulp'); const cleanGulpFiles = (callback) => { ... } const copyGulpFiles = (callback) => { gulp.src(`${nodeModulesFolder}/@primavera/client-app-core/gulp/**/*`) .pipe(chmod(666)) .pipe(gulp.dest(gulpFolder)); callback(); } exports.debug = gulp.series( cleanGulpFiles, copyGulpFiles, require('../src/generated-code/gulp/gulp.debug'));
The problem is: When I try to execute gulp debug
, it is retrieved an error saying require('../src/generated-code/gulp/gulp.debug')
does not exists. And it is right because this file will be only available when the task copyGulpFiles
is done.
Anyone knows a workaround to do what I want to accomplish?
Advertisement
Answer
The only workaround that I found was to combine fs.readFileSync and eval functions in order to read the gulp file content as a string and then evaluate that code in run time:
const gulp = require('gulp'); const fs = require('fs'); const path = require('path'); const gulpFolder = path.join(__dirname.replace('gulpfile.js', ''), 'src', 'generated-code', 'gulp'); const cleanGulpFiles = (callback) => { ... } const copyGulpFiles = (callback) => { gulp.src(`${nodeModulesFolder}/@primavera/client-app-core/gulp/**/*`) .pipe(chmod(666)) .pipe(gulp.dest(gulpFolder)); callback(); } const executeGulpFiles = (callback) => { const fileContent = fs.readFileSync('../src/generated-code/gulp/gulp.debug'); const contentEvaluated = eval(fileContent); contentEvaluated(callback); } exports.debug = gulp.series( cleanGulpFiles, copyGulpFiles, executeGulpFiles);