Skip to content

Commit

Permalink
refactor: migrate to @electron/get
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzhao committed Apr 1, 2020
1 parent d25a3ac commit 3c22216
Show file tree
Hide file tree
Showing 3 changed files with 463 additions and 677 deletions.
39 changes: 24 additions & 15 deletions download-chromedriver.js
@@ -1,22 +1,21 @@
const fs = require('fs')
const path = require('path')
const electronDownload = require('electron-download')
const { downloadArtifact } = require('@electron/get')
const extractZip = require('extract-zip')
const versionToDownload = require('./package').version

function download (version, callback) {
electronDownload({
function download (version) {
return downloadArtifact({
version,
chromedriver: true,
artifactName: 'chromedriver',
platform: process.env.npm_config_platform,
arch: process.env.npm_config_arch,
strictSSL: process.env.npm_config_strict_ssl === 'true',
rejectUnauthorized: process.env.npm_config_strict_ssl === 'true',
quiet: ['info', 'verbose', 'silly', 'http'].indexOf(process.env.npm_config_loglevel) === -1
}, callback)
})
}

function processDownload (err, zipPath) {
if (err != null) throw err
function processDownload (zipPath) {
extractZip(zipPath, { dir: path.join(__dirname, 'bin') }, error => {
if (error != null) throw error
if (process.platform !== 'win32') {
Expand All @@ -27,12 +26,22 @@ function processDownload (err, zipPath) {
})
}

download(versionToDownload, (err, zipPath) => {
if (err) {
const parts = versionToDownload.split('.')
async function attemptDownload (version) {
try {
const zipPath = await download(version)
processDownload(zipPath)
} catch (err) {
// attempt to fall back to semver minor
const parts = version.split('.')
const baseVersion = `${parts[0]}.${parts[1]}.0`
download(baseVersion, processDownload)
} else {
processDownload(err, zipPath)

// don't recurse infinitely
if (baseVersion === version) {
throw err
} else {
await attemptDownload(baseVersion)
}
}
})
}

attemptDownload(versionToDownload)

0 comments on commit 3c22216

Please sign in to comment.