-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invoking the Gulp tool programmatically #770
Comments
Try this: var gulp = require('gulp');
function myFunc(done) {
// eg: copy *.js files into `./dist`
gulp.src('./src/**/*.js')
.pipe(gulp.dest('./dist')
.on('end', function () {
if (done) {
done(); // callback to signal end of build
}
});
}
// run our custom build
myFunc(function () {
console.log('Done!');
}); Nothing special I think, but I think this is what you're looking for? |
yep, there is repl and more, feel free to go to the chat and ask me anything you would want to do. I say that mainly because the documentation has to be expanded |
Hmm no, that was not exactly what I was trying achieve ... the var gulp = require('gulp').parse('/my/project/.gulpfile.js');
if (gulp.hasTask('test')) {
console.log('gulpfile contains task!');
gulp.runTask('test');
} It's a bit rough but it might clarify what I am trying to get at. |
Ok, vanilla gulp var gulp = require('gulp');
process.nextTick(function(){
var tasks = gulp.tasks;
if( gulp.myTask ){ gulp.start('myTask'); }
}); in [email protected] Using the plugin I wrote var runtime = require('gulp-runtime');
process.nextTick(function(){
runtime.input.write('myTask\n');
}) which is the same as you wrote in the shell the task and pressed enter. The repl is something more of a feature. It can work without Anyhow the first option will serve you good if your aim is simpler. |
sorry there was a typo, gulp.myTask; /* -> to */ gulp.tasks.myTasks on that if |
This comment has been minimized.
This comment has been minimized.
Why this then? |
@stringparser Why what? Theres a lot of stuff going on in those lines, specifically what thing? The problem with the sample solution you posted was you never required the gulpfile - requiring the |
haha, sorry about that @contra just the part of |
@stringparser It isn't necessary, just done because it seemed like a good idea to let async stuff finish on require before running start |
interesting :) thanks a lot for the explanation ;) |
@contra yup, that was the one I was looking for, thanks! |
welcome! |
Logs get suppressed when triggered with |
afaik, logging is added when you run gulp from the cli so if you are running it programmatically you will have to add them |
@contra Is there an equivalent functionality for I also couldn't able to invoke a task programmatically with |
@dacodekid |
@contra In the 3.9 version, |
Here is solution for gulp 3.9: |
Ok, in the spirit of using Gulp programmatically instead of at the command line, what if we want to get the "return" values from Gulp tasks? Maybe someone could help me with my SO question? https://stackoverflow.com/questions/40496855/retrieving-result-of-gulp-4-task-programmatically basically, I have a Gulp task like so: const t = gulp.task('foo', function(){
return 5;
}); how can I capture the result of the task? t.on('done', function(val){
// this doesn't work TMK
}); |
var gulp = require('gulp')
require('./gulpfile') // import the gulp file
const result = gulp.task('foo')() Nothing fancy |
oh shazam, but what about a curve ball like this Terminator sequence: const t1 = gulp.task('one',['two'], function(){
return Promise.resolve(5)
});
const t2 = gulp.task('two', function(){
return Promise.resolve(11);
}): surely I can't just do const result1 = t1(); I must have to listen to an event or something...? ? apologies, because I have only used Gulp 3 and am planning on using Gulp 4 heavily but still new to it, please correct syntax if it's wrong. |
@ORESoftware Yeah, you need gulp 4 for that case gulpfile.js: import gulp from 'gulp'
const two = () => Promise.resolve(11)
const one = gulp.series(two, () => Promise.resolve(5))
gulp.task('one', one)
gulp.task('two', two) code.js: import gulp from 'gulp'
import './gulpfile'
const result = gulp.task('one')() With 4, you can also export the tasks from your gulpfile if you're trying to test them. The CLI picks these up too. Here's what that would look like - gulpfile.js: import gulp from 'gulp'
export const two = () => Promise.resolve(11)
export const one = gulp.series(two, () => Promise.resolve(5)) code.js: import { one, two } from './gulpfile'
const result = one() |
thanks let me try that out |
another question I have, just for context => the main reason I am interested in using Gulp programmatically is that it may offer some sophisticated ways to only run tasks once and cache the result. In fact, that is the primary reason I am interested in using it programmatically - I can compose tasks and if two different tasks (tasks 2 and 3) both depend on task 1, I can configure Gulp so that task 1 only runs once, and caches the result. I assume this is possible with Gulp 4, if not then I might not use it. Sorry for sidetracking the conversation. |
@ORESoftware Wrap your functions with a module like http://npm.im/once or https://github.com/gulpjs/async-once |
ok, cool, I can do that, but I think it would be really cool if Gulp did that natively |
Tried this, I am getting undefined as a result const util = require('util');
const gulp = module.exports = require('gulp');
const two = () => Promise.resolve(11);
const one = gulp.series(two, () => Promise.resolve(5));
gulp.task('one', one);
gulp.task('two', two);
const prelim = gulp.task('one');
const result = prelim();
console.log(util.inspect(prelim));
console.log(util.inspect(result)); very confused :/ |
@ORESoftware Maybe series isn't returning the value - @phated would have more info on that since he built the task system |
Btw, all of this is documented in the dependencies (https://github.com/gulpjs/bach in this case) but haven't been surfaced to the 4.0 docs yet (always looking for people to help!) |
Trying to do this with a babel-transpiled Gulpfile,
Somebody experiencing the same issues? |
For anyone bumping into this thread and still running into @squidfunk's issue, make sure you've installed gulp-babel and gone through the other Gulp-specific setup steps on Babel's site (including making the .babelrc config file). I just went through this and I had to setup a .babelrc config file using a babel preset to get mine to work. Go here and click on "Gulp" under build systems for more info: |
In case someone stumbles over this. It seems like the provided solution is deprecated.
|
I am trying to invoke Gulp within another Node.js script. Is there any non-hackish way to do this? e.g. by not manually requiring
bin/gulp.js
or setting up anew Liftoff({ ... })
, or running the command in the shell as is done at the moment in this plugin I'm currently working on?To be more general I'm asking if there's an official way to run Gulp as a part of another tool, without the extra overhead of spawning a new child process. A meta-API, if you like.
I think this is somewhat related to issue #755, except that no
gulpfile
has been loaded yet.The text was updated successfully, but these errors were encountered: