【问题标题】:Curl install binary to users path from nodejs从nodejs卷曲安装二进制文件到用户路径
【发布时间】:2017-05-12 07:05:13
【问题描述】:

各位书呆子和书呆子们,大家好,

我刚刚开始构建一个使用外部命令行界面的小应用程序。该应用程序首先检查二进制文件是否安装在用户路径中,如果没有提供为他们安装它。外部 cli bin 是 digitalocean cli,需要 curl、管道到 tar,然后将 bin 移动到用户路径中。我已经构建了检查是否已安装的功能,并且一直在读取子进程 api,但一直很难弄清楚如何控制 curl 命令的状态。我当前的咒语没有显示控制台输出。我的问题是这个。如何将 cURL 的输出通过管道传输到控制台以确认其工作?我该如何测试成功然后继续前进?

谢谢大家

const exec = require('child_process').exec

const curlScriptOSX = 'curl -L https://github.com/digitalocean/doctl/releases/download/v1.6.0/doctl-1.6.0-darwin-10.6-amd64.tar.gz | tar xz'

exec(curlScriptOSX, function(error, stdout, stderr) {

  console.log('stdout: ' + stdout);
  console.log('stderr: ' + stderr);

  if(error !== null) {
    console.log('exec error: ' + error);
  }
})

更新:我也在查看请求库。有没有可能

request(url).pipe(fs.createWriteStream('binary.tar.gz')).then(exec(extracting)).catch(err => console.error(err))

我想知道...我现在试试这个

【问题讨论】:

    标签: node.js curl child-process


    【解决方案1】:

    使用请求库:

    const fs = require('fs')
    const os = require('os')
    const request = require('request')
    const url = 'https://github.com/digitalocean/doctl/releases/download/v1.6.0/doctl-1.6.0-darwin-10.6-amd64.tar.gz'
    platform = os.platform()
    
    function getInstallerFile (url) {
        console.log("Getting tar")
        // Variable to save downloading progress
        var received_bytes = 0;
        var total_bytes = 0;
        const output = fs.createWriteStream('doctl.tar.gz')
    
        request
            .get(url)
                .on('error', function(err) {
                    console.log(err);
                })
                .on('response', function(data) {
                    total_bytes = parseInt(data.headers['content-length']);
                })
                .on('data', function(chunk) {
                    received_bytes += chunk.length;
                    showDownloadingProgress(received_bytes, total_bytes);
                })
                .pipe(output);
    };
    
    function showDownloadingProgress(received, total) {
        var percentage = ((received * 100) / total).toFixed(2);
        process.stdout.write((platform == 'win32') ? "\033[0G": "\r");
        process.stdout.write(percentage + "% | " + received + " bytes of " + total + " bytes.");
    }
    
    getInstallerFile(url)
    

    【讨论】:

      猜你喜欢
      • 2013-08-19
      • 2016-06-03
      • 2016-02-25
      • 1970-01-01
      • 2016-04-16
      • 2015-07-22
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      相关资源
      最近更新 更多