diff --git a/package.json b/package.json index e9ddf91..e16fd99 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "go-platform": "^1.0.0", "gunzip-maybe": "^1.4.1", "node-fetch": "^2.3.0", + "node-fetch-progress": "^1.0.2", "pkg-conf": "^3.1.0", "tar-fs": "^2.0.0", "unzip-stream": "^0.3.0" diff --git a/src/index.js b/src/index.js index 2f7adfa..d165b5f 100644 --- a/src/index.js +++ b/src/index.js @@ -25,6 +25,7 @@ const path = require('path') const tarFS = require('tar-fs') const unzip = require('unzip-stream') const fetch = require('node-fetch') +const Progress = require('node-fetch-progress') const pkgConf = require('pkg-conf') const pkg = require('./../package.json') @@ -50,9 +51,30 @@ function unpack ({ url, installPath, stream }) { }) } +function progressFetch (res) { + const progress = new Progress(res, { throttle: 100 }) + + return new Promise((resolve) => { + progress.on('progress', (p) => { + if (process.stdout.clearLine) { + process.stdout.clearLine() + process.stdout.cursorTo(0) + } + + const prog = Math.floor(p.progress * 100) + process.stdout.write(`${prog}% ${prog === 100 ? '\n' : ''}`) + + if (prog === 100) { + resolve() + } + }) + }) +} + async function download ({ installPath, url }) { const res = await fetch(url) if (!res.ok) throw new Error(`Unexpected status: ${res.status}`) + await progressFetch(res) return unpack({ url, installPath, stream: res.body }) } @@ -74,6 +96,7 @@ function cleanArguments (version, platform, arch, installPath) { } async function ensureVersion ({ version, distUrl }) { + process.stdout.write('Fetching go-ipfs version list\n') const res = await fetch(`${distUrl}/go-ipfs/versions`) if (!res.ok) throw new Error(`Unexpected status: ${res.status}`) const versions = (await res.text()).trim().split('\n') @@ -86,6 +109,7 @@ async function ensureVersion ({ version, distUrl }) { async function getDownloadURL ({ version, platform, arch, distUrl }) { await ensureVersion({ version, distUrl }) + process.stdout.write(`Fetching go-ipfs ${version} information\n`) const res = await fetch(`${distUrl}/go-ipfs/${version}/dist.json`) if (!res.ok) throw new Error(`Unexpected status: ${res.status}`) const data = await res.json()