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

Using outputHandler to monitor progress events #529

Closed
xiaoxian521 opened this issue Nov 24, 2020 · 18 comments
Closed

Using outputHandler to monitor progress events #529

xiaoxian521 opened this issue Nov 24, 2020 · 18 comments
Labels
enhancement more-info-needed More information is required in order to investigate

Comments

@xiaoxian521
Copy link

Can I add a callback method when I download so that I can add the download progress

@steveukx
Copy link
Owner

steveukx commented Nov 24, 2020

Until v3 is released, you can use outputHandler to receive updates as git tasks are run.

@steveukx steveukx added more-info-needed More information is required in order to investigate question labels Nov 24, 2020
@xiaoxian521
Copy link
Author

well,Is there any way to monitor the download progress

@no-response no-response bot removed the more-info-needed More information is required in order to investigate label Nov 24, 2020
@steveukx
Copy link
Owner

That would depend on which command you are running through git and whether the response from the git binary contains progress information.

In order to suggest something more specific, please include your code and any use of outputHandler you have already tried.

@steveukx steveukx added the more-info-needed More information is required in order to investigate label Nov 24, 2020
@xiaoxian521
Copy link
Author

Code:

const download = require('simple-git')
var time = 0
module.exports.clone = async (repo) => {
    const ora = require('ora')
    const process = ora(`DownLoad.....${repo}${time}`)
    process.start()
    console.log(download)
    await
        download()
            .outputHandler(() => {
                time += 1
            })
            .silent(true)
            .clone(repo)
            .then(status => {
                time = 100
                return
            })
            .catch(err => { console.log("err::", err) })
    process.succeed()
}

Question:
Git download does not always execute the outputhandler function

@no-response no-response bot removed the more-info-needed More information is required in order to investigate label Nov 24, 2020
@xiaoxian521
Copy link
Author

@steveukx The above method is that I simulate the progress. I hope to use a method to execute the download all the time

@xiaoxian521
Copy link
Author

@steveukx hi,Is there any solution

@steveukx steveukx changed the title this is a demand Using outputHandler to monitor progress events Nov 25, 2020
@steveukx
Copy link
Owner

The outputHandler is called once per task, and is passed the stdout / stderr streams from the git binary. To check when those streams receive data, you would need something along these lines:

(async function () {
    const simpleGit = require('simple-git');
    const ora = require('ora');

    const remote = {
        name: 'simple',
        repo: 'https://github.com/steveukx/git-js.git'
    };
    const spinner = ora('Source Control').start();

    const git = simpleGit('repo');
    git.outputHandler((bin, stdOut, stdErr) => {
        stdErr.on('data', d => spinner.text = `Received: ${d.length}b`);
        stdOut.on('data', d => spinner.text = `Received: ${d.length}b`);
    });
    try {
        spinner.prefixText = 'git.init';
        await git.init();

        spinner.prefixText = 'git.addRemote';
        await git.addRemote(remote.name, remote.repo);

        spinner.prefixText = 'git.fetch';
        await git.fetch([remote.name]);

        spinner.prefixText = 'git.checkout';
        await git.checkout('v2.9.0');

        spinner.prefixText = '';
        spinner.text = 'Complete.';
        spinner.stopAndPersist();
    }
    catch (e) {
        spinner.text = 'Error: ' + e;
        spinner.stopAndPersist();
    }

}());

@steveukx steveukx added the more-info-needed More information is required in order to investigate label Nov 25, 2020
@xiaoxian521
Copy link
Author

ok. thanks i am try

@no-response no-response bot removed the more-info-needed More information is required in order to investigate label Nov 25, 2020
@xiaoxian521
Copy link
Author

I think git -js can combine with the progress module to make a progress bar

@xiaoxian521
Copy link
Author

@steveukx v3 edition Is there any way to monitor the download progress?

@xiaoxian521
Copy link
Author

such as:

const download = require('download');
const Progress = require('progress');


// Global variables.
const FORMAT = '[:bar] :percent :etas';
const OPTIONS = {
  complete: '=',
  incomplete: ' ',
  width: 20,
  total: 0
};


/**
 * Download and extract files (with progress).
 * @param {string} url URL to download.
 * @param {string} [dst] Path to where your file will be written.
 * @param {object} [opt] Options for "download", progress, onresponse.
 */
function edownload(url, dst, opt) {
  var o = (opt || typeof dst==='string'? opt:dst)||{};
  var bar = o.progress===undefined? new Progress(FORMAT, OPTIONS):o.progress;
  return download(url, dst, opt).on('response', o.onresponse||(res => {
    if(bar==null) return;
    bar.total = res.headers['content-length'];
    res.on('data', dat => bar.tick(dat.length));
  }));
};
module.exports = edownload;

@steveukx
Copy link
Owner

Hi, you can now configure simple-git with a progress handler to receive progress updates on any git operation that supports them - https://github.com/steveukx/git-js#progress-events

If you have any problems using the new api, please do open another issue.

@steveukx steveukx added enhancement more-info-needed More information is required in order to investigate and removed question labels Feb 16, 2021
@no-response no-response bot removed the more-info-needed More information is required in order to investigate label Feb 18, 2021
@xiaoxian521
Copy link
Author

@steveukx
version:"simple-git": "^2.35.2"
bug:The progress bar reaches 100% before downloading

@steveukx
Copy link
Owner

Hi, in order to look into this for you please also include the code you are using to respond to progress events. Thanks

@steveukx steveukx added the more-info-needed More information is required in order to investigate label Feb 25, 2021
@xiaoxian521
Copy link
Author

const SimpleGit = require('simple-git')
const ora = require('ora')
const process = require("process")

const spinner = ora('开始下载').start();

const progress = ({ stage, progress }) => {
    let proText = `当前下载进度: ${progress}%`
    spinner.text = proText
    if (progress === 100) {
        spinner.text = proText + ' 下载完成'
        spinner.stopAndPersist()
        process.exit()
    }
}

module.exports.clone = async (repo) => {
    const git = SimpleGit({ progress: progress })
    await git.clone(repo)
}

@no-response no-response bot removed the more-info-needed More information is required in order to investigate label Feb 25, 2021
@steveukx
Copy link
Owner

Your progress handler is terminating the node process on the first 100% complete stage rather than when the clone task is complete - note that there are potentially several stages of any one command (for example a clone will have stages for both counting and receiving assuming you're in english locale).

A more robust solution would be:

const SimpleGit = require('simple-git')
const ora = require('ora')
const process = require("process")

const spinner = ora('开始下载').start();

const progress = ({ stage, progress }) => {
    let proText = `当前下载进度: ${stage} ${progress}%`
    spinner.text = proText
    if (progress === 100) {
        spinner.text = proText + ' 下载完成'
    }
}

module.exports.clone = async (repo) => {
    const git = SimpleGit({ progress: progress })
    await git.clone(repo);
    process.exit();
}

@xiaoxian521
Copy link
Author

Wow, thank you for your advice
It's perfect

@steveukx steveukx added the more-info-needed More information is required in order to investigate label Feb 25, 2021
@steveukx
Copy link
Owner

No problem, please do open a new issue if you have any further issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement more-info-needed More information is required in order to investigate
Projects
None yet
Development

No branches or pull requests

2 participants