From 2873dcc4e4c28d84cee26ca3a5485c70453ca9ad Mon Sep 17 00:00:00 2001 From: Igor Klopov Date: Fri, 2 Jun 2017 23:41:15 +0300 Subject: [PATCH] Download from GitHub directly (#625) * directly downloads the version specified in package.json.version * catch 'error' event from resp.body --- download/src/index.js | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/download/src/index.js b/download/src/index.js index d51c5ee..a844e54 100644 --- a/download/src/index.js +++ b/download/src/index.js @@ -17,7 +17,7 @@ const targetWin32 = path.join(__dirname, 'now.exe') const target = process.platform === 'win32' ? targetWin32 : now const partial = target + '.partial' -const packagePath = path.join(__dirname, '..', '..', 'package.json') +const packagePath = path.join(__dirname, '../../package.json') const packageJSON = JSON.parse(fs.readFileSync(packagePath, 'utf8')) const platformToName = { @@ -43,15 +43,6 @@ async function main() { process.exit(); }) - info('Retrieving the latest CLI version...') - - const downloadURL = `https://api.github.com/repos/zeit/now-cli/releases/tags/${packageJSON.version}` - const responseLatest = await fetch(downloadURL) - const latest = await responseLatest.json() - - const name = platformToName[process.platform] - const asset = latest.assets.find(a => a.name === name) - info('For the sources, check out: https://github.com/zeit/now-cli') // Print an empty line @@ -60,7 +51,8 @@ async function main() { enableProgress('Downloading now CLI ' + packageJSON.version) showProgress(0) - const url = asset.browser_download_url + const name = platformToName[process.platform] + const url = `https://github.com/zeit/now-cli/releases/download/${packageJSON.version}/${name}` const resp = await fetch(url) if (resp.status !== 200) { @@ -77,20 +69,21 @@ async function main() { resp.body.on('data', chunk => { bytesRead += chunk.length showProgress(100 * bytesRead / size) + }).on('error', error => { + disableProgress() + reject(error) }) resp.body.pipe(ws) - ws - .on('close', () => { - showProgress(100) - disableProgress() - resolve() - }) - .on('error', error => { - disableProgress() - reject(error) - }) + ws.on('close', () => { + showProgress(100) + disableProgress() + resolve() + }).on('error', error => { + disableProgress() + reject(error) + }) }) fs.renameSync(partial, target)