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

What is the recommended way to wait for a running task? #90

Open
Aghassi opened this issue Apr 11, 2018 · 5 comments
Open

What is the recommended way to wait for a running task? #90

Aghassi opened this issue Apr 11, 2018 · 5 comments

Comments

@Aghassi
Copy link

Aghassi commented Apr 11, 2018

Hello there 😄

I have a question after having read through the docs multiple times. Say I'd like to execute a function like https://github.com/sindresorhus/np or yarn via a task. There are many examples in the README of how to execute, but what I am unclear on is how to have it so it shows the progress while this task runs. The reason I ask is because say I do something like

execa('np patch') as the command for a task. The task will go through and "succeed" because the function executed correcly. However, the output/failure of said function is not displayed anywhere.

However, if I do execa.syncShell('np patch') as the task, Listr will proceed by running the task and not showing any progress until the task is complete. This is good because it doesn't progress until the step is done. This is bad because it doesn't render any progress while the task runs.

Granted, I may have a basic misunderstanding of how this tool works, so I am just looking for some clarity on the best way to wrap or use other javascript utilities. I assume I may be able to use Observable here, but the documentation isn't very clear on the best way to go about doing that.

So, I guess to sum up: What is the appropriate way to forward/watch a currently running task while also generating output while it runs?

@vysinsky
Copy link

vysinsky commented Apr 19, 2018

Hi, I've just needed this too. It is not ideal and it works best with verbose renderer (for default renderer, I output to a file). This is good enough for me.

task: () => new Promise((resolve, reject) => {
    {
        const cmd = execa('npm', ['run', 'test']);
        cmd.then(resolve)
            .catch(() => {
              reject(new Error('Failed'));
            });

        cmd.stdout.pipe(outputStream);
        cmd.stderr.pipe(outputStream);

        return cmd;
    }
})

^ outputStream is either process.stdout or WriteStream

@SamVerschueren
Copy link
Owner

SamVerschueren commented Apr 19, 2018

Hi. Yes you can use Observables for this as the documentation states. You can re-use a utility function that we use at np right here.

This does answer your question :)?

@Aghassi
Copy link
Author

Aghassi commented Apr 19, 2018

@SamVerschueren Yes that is a step in the right direction and much more clear! This is my first time working with Observables, so lots to learn. When I try that function, it works. However, if it fails I see

✖ Releasing via np...
(node:12700) UnhandledPromiseRejectionWarning: Error: Command failed: np prepatch --tag=next --any-branch --no-cleanup

Followed by the trace (in this case, unclean working branch).

✖ Unclean working tree. Commit or stash changes first.

[11:08:14] Prerequisite check [started]
[11:08:14] Ping npm registry [started]
[11:08:14] Ping npm registry [skipped]
[11:08:14] Verify user is authenticated [started]
[11:08:14] Verify user is authenticated [skipped]
[11:08:14] Check git remote [started]
[11:08:14] Check git remote [completed]
[11:08:14] Validate version [started]
[11:08:14] Validate version [completed]
[11:08:14] Check for pre-release version [started]
[11:08:14] Check for pre-release version [completed]
[11:08:14] Check npm version [started]
[11:08:14] Check npm version [completed]
[11:08:14] Check git tag existence [started]
[11:08:15] Check git tag existence [completed]
[11:08:15] Prerequisite check [completed]
[11:08:15] Git [started]
[11:08:15] Check local working tree [started]
[11:08:15] Check local working tree [failed]
[11:08:15] → Unclean working tree. Commit or stash changes first.
[11:08:15] Git [failed]
[11:08:15] → Unclean working tree. Commit or stash changes first.

I get this regardless of if I catch the function. What am I overlooking here? Ideally, I'd like to just bubble a clean error up that is readable to my users.

@Aghassi
Copy link
Author

Aghassi commented May 7, 2018

@SamVerschueren I've setup a repo with an example of what I mean by "unhandled promise error". https://github.com/Aghassi/promise-observables

If you run node transpiled.js you will see the error. I'm using Node v9.5.0. Doing some digging shows that execa.stdout is not a stream, but a promise? Just trying to figure out how to gracefully handle the failure. Otherwise, I'd like to look for a different way of doing this because this seems fragile.

@ghost
Copy link

ghost commented Sep 27, 2018

execa('np patch') as the command for a task. The task will go through and "succeed" because the function executed correcly. However, the output/failure of said function is not displayed anywhere.

This is the issue I'm having, the task is showing as completed straight away even though it's still being processed. Tried using the exec method from np which still does it.

Edit: Ignore my comment, figured it out using promises, example...

return new Promise((resolve, reject) => {
    try {
        execa('composer', ['install', '-d=public_html']).then(() => resolve());
    } catch(error) {
        reject(new Error(error));
    }
});

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

3 participants