Skip to content
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

Pass callback to grunt.task.run #1184

Open
donaldpipowitch opened this issue Jul 24, 2014 · 42 comments
Open

Pass callback to grunt.task.run #1184

donaldpipowitch opened this issue Jul 24, 2014 · 42 comments

Comments

@donaldpipowitch
Copy link

grunt.task.run runs a task after another has finished. However I need to run a task from within a task and process things after the other task has finished. An API like this would be beautiful:

var done = this.async();
grunt.task.run('some-task', function() {
  // do things after 'some-task' finished
  done();
});

I couldn't find a way to do that with the current API.

EDIT:
What I currently do to solve this problem. I use another internal task which runs before/after my some-task. This internal task is only needed to set some dynamic config and shouldn't be called manually. Usage looks like this:

grunt.task.run('internal-task', 'some-task');
// or
grunt.task.run('some-task', 'internal-task');
@monolithed
Copy link

+1

@Florian-R
Copy link

FWIW, something similar is planned for v0.5. See #542

@madhums
Copy link

madhums commented Aug 22, 2014

+1

2 similar comments
@digitalmaster
Copy link

+1

@bzwheeler
Copy link

+1

@hemanth
Copy link

hemanth commented Sep 17, 2014

👍

//cc @cowboy

@hartatovich
Copy link

+1

2 similar comments
@nikolaygit
Copy link

+1

@tecfu
Copy link

tecfu commented Sep 21, 2014

+1

@s2young
Copy link

s2young commented Sep 29, 2014

+1

@bluecurio
Copy link

👍 +1

@beeplove
Copy link

+1

@z0r1k
Copy link

z0r1k commented Nov 10, 2014

Yes, please. Gimme two.

@soundstep
Copy link

+1

2 similar comments
@abelnation
Copy link

+1

@gregjacobs
Copy link

+1

@matthewstokeley
Copy link

+!

@aehlke
Copy link

aehlke commented Dec 3, 2014

Please just hit "Subscribe" rather than reply with "+1" which spams everyone following the thread, thanks.

@YuhangGe
Copy link

YuhangGe commented Dec 8, 2014

+1

2 similar comments
@aaccurso
Copy link

aaccurso commented Jan 7, 2015

+1

@elliot-a
Copy link

elliot-a commented Jan 8, 2015

+1

@kodmax
Copy link

kodmax commented Jan 13, 2015

there should also be an argument with the task completion status code in this "finalize callback" and a possitibility to to avoid interruption of the task flow;

grunt.task.run('some-task', function (status) {
  console.log('some-task completed with status: ' + status);

  return true; // force continue
});

@excitepv
Copy link

+1

1 similar comment
@perry-mitchell
Copy link

+1

@aehlke
Copy link

aehlke commented Jan 29, 2015

Please just click "Subscribe" on the right instead of replying to +1 (which pings all subscribers), thanks.

@aleclarson
Copy link

I just now finished making grunt-anon-tasks to solve this problem.

grunt.task
  .run("some-task")
  .then(function () {
    // do stuff after "some-task" completes, but before "other-task" begins
  })
  .run("other-task")

Check out the repo to learn more.

@nadav-dav
Copy link

+1

1 similar comment
@zsxsoft
Copy link

zsxsoft commented Jun 13, 2015

+1

@ghost
Copy link

ghost commented Jul 10, 2015

👍

@amitnabarro
Copy link

+1

5 similar comments
@knoefel
Copy link

knoefel commented Dec 3, 2015

+1

@ghost
Copy link

ghost commented Dec 7, 2015

+1

@felipeblini
Copy link

+1

@julkue
Copy link

julkue commented Jan 18, 2016

+1

@giano
Copy link

giano commented Feb 23, 2016

+1

@shama shama removed this from the 0.4.6 milestone Feb 23, 2016
@averybross
Copy link

+1

1 similar comment
@MusikAnimal
Copy link

👍

@julkue
Copy link

julkue commented May 10, 2016

Please just click the subscribe button and use the new emoji feature instead of commenting (@RickBRoss, @MusikAnimal).

@ArmorDarks
Copy link

I guess those days it would be better to have run method returning Promise, which than can be chained with then and so on.

@ArmorDarks
Copy link

Besides, mentioned workaround

grunt.task.run('internal-task', 'some-task');
// or
grunt.task.run('some-task', 'internal-task');

Will not work for tests well, because you don't have in internal-task ability to return anything, so you're forced to mangle with stdout or writing a temp file to get a value.

@ArmorDarks
Copy link

Well, after looking into

Task.prototype.run = function() {

I now see that this isn't that simple as simply passing a callback. task.run is just adding tasks to the queue and knows nothing about what and when will happen.

After some looking closer and testing, it turned out that

grunt.task.run(['youTask`']).start()

is synchronous. So, it is possible to do something like this

    grunt.task.run(['image_size:test']).start()
    console.log(grunt.config(buildPath.test.property))
    expect(readJSON(buildPath.test.dest)).toMatchSnapshot()

However, while it mostly works, it puts Jest in watch mode into an endless loop. I guess it has something to do with the way Grunt exits the process.

Besides, using start() will force Grunt to throw everything directly into the console and there is no straight way to capture the output.

So far I assume that it can be mitigated by introducing callback or promises to start() and passing out stdout as a resolve().

The alternative would be to use process spawning (as here), but while working mostly flawlessly, it has a major drawback — you can't mutate with tasks Grunt's context and access it afterwards. Which is crucial for some tests.

@Rey-Wang
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests