Perhaps it's something wrong with my approach but I have a following situation:
component-a
that has a gulpfile. One of its tasks (eg. build) builds the component and creates a combined js file in dist foldercomponent-b
that has a gulpfile. One of its tasks (eg. build) builds the component and creates a combined js file in dist folderWhat I don't know is how to execute the build task from /components/component-?/gulpfile.js. Is it even possible or I should deal with this situation otherwise?
Run the gulp command in your project directory: gulp. To run multiple tasks, you can use gulp <task> <othertask>.
Although a few utilities are provided to simplify working with the filesystem and command line, everything else you write is pure JavaScript. A gulpfile is a file in your project directory titled gulpfile.js (or capitalized as Gulpfile.js, like Makefile), that automatically loads when you run the gulp command.
Gulp allows you to use existing JavaScript knowledge to write gulpfiles or to use your experience with gulpfiles to write plain JavaScript. Although a few utilities are provided to simplify working with the filesystem and command line, everything else you write is pure JavaScript.
Within this file, you'll often see gulp APIs, like src (), dest (), series (), or parallel () but any vanilla JavaScript or Node modules can be used. Any exported functions will be registered into gulp's task system.
Running a Gulpfile from a different directory is quite simple with Node's child_process#spawn
module.
Try adapting the following to your needs:
// Use `spawn` to execute shell commands with Node const { spawn } = require('child_process') const { join } = require('path') /* Set the working directory of your current process as the directory where the target Gulpfile exists. */ process.chdir(join('tasks', 'foo')) // Gulp tasks that will be run. const tasks = ['js:uglify', 'js:lint'] // Run the `gulp` executable const child = spawn('gulp', tasks) // Print output from Gulpfile child.stdout.on('data', function(data) { if (data) console.log(data.toString()) })
Although using gulp-chug
is one way to go about this, it has been blacklisted by gulp
's maintainers for being...
"execing, too complex and is just using gulp as a globber"
The official blacklist states...
"no reason for this to exist, use the require-all module or node's require"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With