Skip to content

Commit

Permalink
fix: Allow downloading of custom versions of mksnapshots (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
John Kleinschmidt committed May 26, 2020
1 parent 4c31a01 commit 9b1abfa
Show file tree
Hide file tree
Showing 4 changed files with 488 additions and 695 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -41,6 +41,16 @@ ELECTRON_MIRROR="https://npm.taobao.org/mirrors/electron/"
# Example of requested URL: http://localhost:8080/1.2.0/mksnapshot-v1.2.0-darwin-x64.zip
ELECTRON_MIRROR="http://localhost:8080/"
```

## Overriding the version downloaded

The version downloaded can be overriden by setting the `ELECTRON_CUSTOM_VERSION` environment variable.

```sh
# Install mksnapshot for Electron v8.3.0
ELECTRON_CUSTOM_VERSION=8.3.0 npm install
```

## Generating snapshots for ARM hardware

If you need to generate snapshots for Linux on 32 bit ARM, Linux on ARM64, or Windows on ARM64 you will need to install a cross arch mksnapshot on an Intel x64 machine. To do so, set the npm config `arch` to the proper arch and then run `npm install --save-dev electron-mksnapshot`. For example:
Expand Down
61 changes: 33 additions & 28 deletions download-mksnapshot.js
@@ -1,9 +1,9 @@
var fs = require('fs')
var path = require('path')
var electronDownload = require('electron-download')
var extractZip = require('extract-zip')
var versionToDownload = require('./package').version
var archToDownload = process.env.npm_config_arch
const fs = require('fs')
const path = require('path')
const { downloadArtifact } = require('@electron/get')
const extractZip = require('extract-zip')
const versionToDownload = require('./package').version
let archToDownload = process.env.npm_config_arch

if (process.arch.indexOf('arm') === 0) {
console.log(`WARNING: mksnapshot does not run on ${process.arch}. Download
Expand All @@ -23,38 +23,43 @@ if (archToDownload && archToDownload.indexOf('arm') === 0) {
}
}

function download (version, callback) {
electronDownload({
function download (version) {
return downloadArtifact({
version: version,
mksnapshot: true,
artifactName: 'mksnapshot',
platform: process.env.npm_config_platform,
arch: archToDownload,
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
extractZip(zipPath, { dir: path.join(__dirname, 'bin') }, function (error) {
if (error != null) throw error
if (process.platform !== 'win32') {
var mksnapshotPath = path.join(__dirname, 'bin', 'mksnapshot')
async function attemptDownload (version) {
try {
const targetFolder = path.join(__dirname, 'bin')
const zipPath = await download(version)
await extractZip(zipPath, { dir: targetFolder })
const platform = process.env.npm_config_platform || process.platform
if (platform !== 'win32') {
const mksnapshotPath = path.join(__dirname, 'bin', 'mksnapshot')
if (fs.existsSync(mksnapshotPath)) {
fs.chmod(path.join(__dirname, 'bin', 'mksnapshot'), '755', function (error) {
fs.chmod(mksnapshotPath, '755', function (error) {
if (error != null) throw error
})
}
}
})
}
} catch (err) {
// attempt to fall back to semver minor
const parts = version.split('.')
const baseVersion = `${parts[0]}.${parts[1]}.0`

download(versionToDownload, function (err, zipPath) {
if (err) {
var versionSegments = versionToDownload.split('.')
var baseVersion = versionSegments[0] + '.' + versionSegments[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 9b1abfa

Please sign in to comment.