Run package's gulp task within my gulp task - gulp

Is there a way to run a packages gulp task from within my gulp task? Here's my file structure:
Root
|
|-- Gulpfile.js (task: get-files)
|
|-- node_modules
|
|-- packageA
|
|-- gulpfile.js (task: compile-custom)
I want to run the compile-custom task from packageA from within my get-files task in my own gulpfile. Ideally, I could also get a callback from the compile-custom task to know when it is complete.

I would advise against running a task from another gulpfile as you want, especially from a node_modules package. A reason to not wanting to do so is due to the fact that the package can change/update and then break your code because of it.
Preferably you should just copy the gulpfile task you want into either your gulpfile and incorporate it into your project or just create a new file where you can easily distinguish that it is something separate from your gulpfile and just call it by requiring it in your own gulfile just like you would any other package and/or JS function. With either of these options do not forget to install the dependencies into your package.json files and then run npm install.
Note: To run a gulp task from within a gulp task you can use
gulp.start('taskname')
Although the following should be used only if completely necessary. In addition will be deprecated in the newer version of Gulp v4 (refer to this). The way to call a task is through just creating a function as mentioned above and calling that function in your gulpfile.
Example of gulp function calling:
var someFile = require('./someFile.js');
gulp.task('my-custom-task', function () {
someFile.doSomething('foo', 'bar');
});
If your function does something asynchronously, it should call a callback at the end, so gulp is able to know when it’s done:
var someFile = require('./someFile.js');
gulp.task('my-custom-task', function (callback) {
someFile.doSomething('foo', 'bar', callback);
});

Related

Semantic UI - Error While Importing Gulp Tasks

I am trying to import Semantic UI's Gulp tasks into my own Gulpfile. I want my parent Gulp file to run Semantic's 'gulp build' command, then have my parent Gulp copy the files Semantic's build command outputs to another folder. Everything was moving along until I hit this error:
Error: Task build-javascript is not configured as a task on gulp. If this is a submodule, you may need to use require('run-sequence').use(gulp).
I have a folder /semantic/ which includes all the tasks and everything. And my gulpfile is setup just like Semantic's docs recommend. http://semantic-ui.com/introduction/advanced-usage.html
var watchUI = require('./semantic/tasks/watch'),
buildUI = require('./semantic/tasks/build')
Has anybody else run into this problem? Any help greatly appreciated.
Thanks
There is an open issue on github. But this instructions helped me:
https://github.com/Semantic-Org/Semantic-UI/issues/4374#issuecomment-236729805
Open the file node_modules/semantic-ui/tasks/install.js in your preferred text editor.
On line 26, you should see runSequence = require('run-sequence'),. Please replace it with runSequence = require('run-sequence').use(gulp),
Run in the console cd node_modules/semantic-ui && gulp install.

Running gulp produces no output

Just getting started with gulp, going through some tutorials. I'm in a mac terminal...
My extremely simple gulpfile:
var gulp = require('gulp');
var scripts = 'scripts/*.js';
gulp.task('copy', function() {
// place code for your default task here
return gulp.src(scripts)
.pipe(gulp.dest('build/js'));
});
I run 'gulp copy' on the command line and get some output that looks like it runs, but no files are copied:
Richards-MBP:gulp-test richardlovejoy$ gulp copy
[19:30:38] Using gulpfile /Work/gulp-test/gulpfile.js
[19:30:38] Starting 'copy'...
[19:30:38] Finished 'copy' after 27 ms
I have 2 js files in the 'scripts' folder.
I originally started with a more complex gulp file which also failed to produce any output.
I inserted gulp-debug but it just shows me that 0 files are in the pipe.
Running the latest version of node (5.2.0) as of this writing.
Tried gulp --verbose but it gives me nothing.
How can I at least see what gulp is doing behind the scenes to debug this?
Thanks

Gulp 4 watcher is running but does not detect changes

I'm having the following file structure:
/ src
-- app.less
/ gulp
-- index.js
-- gulpfile.js
This file structure is mounted in a vagrant box in /vagrant which means the path to app.less becomes /vagrant/src/app.less. Yes, I've checked this.
gulpfile.js
require('./gulp');
index.js
var paths = {
less: '/vagrant/src/app.less'
};
gulp.task('less', function () {
console.log('less function running');
return gulp.src(paths.less)
.pipe(less());
});
gulp.task('watch:styles', function () {
console.log('watch function running');
gulp.watch(paths.less, gulp.series('less'));
});
gulp.task('watch', gulp.parallel('watch:styles'));
gulp -v returns:
[10:02:05] CLI version 0.4.0
[10:02:05] Local version 4.0.0-alpha.1
gulp watch returns:
[09:45:20] Using gulpfile /vagrant/gulpfile.js
[09:45:20] Starting 'watch'...
[09:45:20] Starting 'watch:styles'...
watch function running
I've been using Gulp 4 for over 2 months without problems with the watcher. Since last week the watcher is not responding to files that are being changed. I've tried several editors, I've tried multiple paths like '/vagrant/**/*.less' and '../src/*.less' and even the absolute path to app.less '/vagrant/src/app.less', none of them worked.
After some research I found several issues on the github repo of Gulp 4 about the watcher. Yet, I can't figure out what the problem is. Maybe I'm overlooking an error in my code or something new in the docs, but I'm trying to solve this since yesterday morning without any luck.
It appears you're using Vagrant. If you have Gulp running on your Vagrant machine instead of on the host it won't detect any changes to files that you make on the host. This is because the events that notify the OS about filesystem changes don't propagate into the VM.
If this is the case, the solution is to simply run Gulp wherever you actually make changes to the files (i.e. if you make the changes on the VM, run it on the VM, if you make changes on the host, run Gulp on the host).
Also maybe make the path relative, instead of tying your implementation to your Vagrant box. i.e. less: './src/app.less'.

Using gulp to run a asset download script

I have an isolated npm module with assets. I want to add gulp to this. Inside the project aside from my assets i have a bash script that downloads and updates my assets. I have been trying to find a way to create a gulp task that can run this script but i have not yet been able to get it to work. I tried the following and although it ran it did not run the script.
gulp.task('update-json', function(){
gulp.src('assets/update_json.sh');
});

Run Yeoman generator in debug using gulp

I'm trying to create a gulp task that will execute Yeoman generator I'm developing. I've got this working using the following task, but I'm trying to find a way to not pass in the fully qualified path to the location of my globally installed NPM modules.
The gulp plugins I've seen (gulp-shell & gulp-run) execute a command (such as npm root -g) but I can't figure out how to read the text into a variable or if there's another / easier way to get this value.
gulp.task('run-yo', function () {
spawn('node', [
'--debug',
'/Users/ac/.npm-packages/lib/node_modules/yo/lib/cli.js',
'nodehttps'], { stdio: 'inherit' });
});
You can use node which
var which = require('which');
which.sync('yo');