Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

feat: print progress #36

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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 })
}

Expand All @@ -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')
Expand All @@ -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()
Expand Down