export async function downloadWithProgress()

in packages/cli/src/utils/index.ts [98:127]


export async function downloadWithProgress(url: string, fileName: string): Promise<void> {
  await fs.ensureFile(fileName);
  const bar = new CliProgress.SingleBar({
    format: '{bar} {percentage}% {value}/{total}',
    formatValue: (v, _, type): string => {
      if (type === 'value' || type === 'total') {
        return prettyBytes(v);
      } else {
        return v.toString();
      }
    }
  }, CliProgress.Presets.shades_classic);
  const file = fs.createWriteStream(fileName);
  let receivedBytes = 0;
  const downloadStream = (await bent(url)('')) as bent.NodeResponse;
  const totalBytes = downloadStream.headers['content-length'];
  bar.start(Number(totalBytes), 0);
  downloadStream.on('data', (chunk: any) => {
    receivedBytes += chunk.length;
    bar.update(receivedBytes);
  });
  try {
    await pipelineAsync(downloadStream, file);
    bar.stop();
  } catch (err) {
    fs.unlink(fileName);
    bar.stop();
    throw err;
  }
}