Electron无法下载问题
Electron打包,我把npm
切换成yarn
时,导致electron二进制包
下载不了。
问题1:
履行:yarn install
报错,electron二进制文件下载不了。因为被墙了
我运用的yarn是4.0版别。正常经过npm指令装置的是1.0版别
原因
之前一向运用 yarn1
版别,但发现低版别对 TS
言语支持的不是很好,编译的时分会报过错信息。查了材料今后说是要升级到 yarn3
版别以上 TS
就不会报错了。现在最新的是4.0版别,升到最新今后发现之前的 Electron
装备文件 config
不能运用了
ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/
ELECTRON_BUILDER_BINARIES_MIRROR=https://npmmirror.com/mirrors/electron-builder-binaries/
导致上面的环境变量装备不上去,一向下载 electron
时报错。
处理方案
在工程根目录下创立 shell
脚本,yarn_install.sh
文件:
#!/bin/bash
rm -rf node_modules && rm -rf yarn.lock && rm -rf dist
export ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
export ELECTRON_BUILDER_BINARIES_MIRROR="https://npmmirror.com/mirrors/electron-builder-binaries/"
yarn cache clean --all && yarn install
经过指令行上下文中指定ELECTRON_MIRROR和ELECTRON_BUILDER_BINARIES_MIRROR变量。能够让指令:
yarn install
正常履行
问题2
当我运用 electron-builder
工具打包 windows/macos/linux
平常的包时报错:
• electron-builder version=24.13.3 os=22.6.0
• loaded configuration file=/Users/yuhang/yuyue/project/webstorm/electron-vite-template/build.json
• writing effective config file=build/builder-effective-config.yaml
• packaging platform=darwin arch=arm64 electron=29.1.2 appOutDir=build/mac-arm64
⨯ Get "https://github.com/electron/electron/releases/download/v29.1.2/electron-v29.1.2-darwin-arm64.zip": Method Not Allowed
github.com/develar/app-builder/pkg/download.(*Downloader).follow.func1
/Volumes/data/Documents/app-builder/pkg/download/downloader.go:206
github.com/develar/app-builder/pkg/download.(*Downloader).follow
/Volumes/data/Documents/app-builder/pkg/download/downloader.go:234
github.com/develar/app-builder/pkg/download.(*Downloader).DownloadNoRetry
/Volumes/data/Documents/app-builder/pkg/download/downloader.go:128
github.com/develar/app-builder/pkg/download.(*Downloader).Download
/Volumes/data/Documents/app-builder/pkg/download/downloader.go:112
github.com/develar/app-builder/pkg/electron.(*ElectronDownloader).doDownload
/Volumes/data/Documents/app-builder/pkg/electron/electronDownloader.go:192
github.com/develar/app-builder/pkg/electron.(*ElectronDownloader).Download
/Volumes/data/Documents/app-builder/pkg/electron/electronDownloader.go:177
github.com/develar/app-builder/pkg/electron.downloadElectron.func1.1
/Volumes/data/Documents/app-builder/pkg/electron/electronDownloader.go:73
github.com/develar/app-builder/pkg/util.MapAsyncConcurrency.func2
/Volumes/data/Documents/app-builder/pkg/util/async.go:68
runtime.goexit
/usr/local/Cellar/go/1.17/libexec/src/runtime/asm_arm64.s:1133
⨯ /Users/yuhang/yuyue/project/webstorm/electron-vite-template/node_modules/app-builder-bin/mac/app-builder_arm64 process failed ERR_ELECTRON_BUILDER_CANNOT_EXECUTE
Exit code:
1 failedTask=build stackTrace=Error: /Users/yuhang/yuyue/project/webstorm/electron-vite-template/node_modules/app-builder-bin/mac/app-builder_arm64 process failed ERR_ELECTRON_BUILDER_CANNOT_EXECUTE
Exit code:
1
at ChildProcess.<anonymous> (/Users/yuhang/yuyue/project/webstorm/electron-vite-template/node_modules/builder-util/src/util.ts:252:14)
at Object.onceWrapper (node:events:629:26)
at ChildProcess.emit (node:events:514:28)
at maybeClose (node:internal/child_process:1105:16)
at Process.ChildProcess._handle.onexit (node:internal/child_process:305:5)
Process finished with exit code 1
这个过错提示,electron二进制文件下载不了
原因
我查看了 electron下载部分的源代码有问题,需求改 node_modules
的代码。这种方法如同不太好
我查了许多材料 npm
如同能够装备文件里加参数处理。但 yarn
4.0版别,没什么材料。官方文档上查了今后发现,也不能装备 ELECTRON_MIRROR
这些变量。
我查了electron的官网信息,说是会把这些缓存文件放在下面的途径:
- Linux:
$XDG_CACHE_HOME
or~/.cache/electron/
- macOS:
~/Library/Caches/electron/
- Windows:
$LOCALAPPDATA/electron/Cache
or~/AppData/Local/electron/Cache/
没有完美处理方案,现在我的方法是自己去下载对应的 electron
版别到缓存途径中。这样打包的时分,直接读缓存不用去下载了。
处理方案
我自己用golang
写了脚本。轻量的脚手架,主动读取 package.json
文件中的 electron
版别号。去第三方镜像网站主动下载。写到不同平台的缓存途径下。运用 golang
是因为能够跨平台,所以体系都能够直接运用。
github地址:https://github.com/yuhanghate/electron-download.git
electron-download.go
脚本代码:
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/cheggaaa/pb/v3"
"io"
"log"
"net/http"
"os"
"os/user"
"path"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
func main() {
arch := [3]string{"x64", "i32", "arm64"}
baseURL := "https://npmmirror.com/mirrors/electron/"
var version, _ = readPackageFile()
// 生成需求下载的electron二进制包
var downloadUrls []string
for _, s := range arch {
url, err := buildDownloadURL(baseURL, version, s)
if err == nil {
downloadUrls = append(downloadUrls, url)
}
}
downloadElectron(downloadUrls)
}
// 获取缓存目录
func getCacheDir() string {
usr, _ := user.Current()
platform := runtime.GOOS
var cacheDir string
if platform == "linux" {
cacheDir = filepath.Join(usr.HomeDir, ".cache", "electron")
} else if platform == "darwin" {
cacheDir = filepath.Join(usr.HomeDir, "Library", "Caches", "electron")
} else if platform == "windows" {
cacheDir = filepath.Join(usr.HomeDir, "AppData", "Local", "electron", "Cache")
}
return cacheDir
}
// 生成下载地址
func buildDownloadURL(baseURL string, version string, arch string) (string, error) {
platform := runtime.GOOS
filename := ""
if platform != "windows" && arch == "i32" {
return "", errors.New("")
} else {
filename = fmt.Sprintf("electron-v%s-%s-%s.zip", version, platform, arch)
}
url := baseURL + version + "/" + filename
return url, nil
}
// 下载Electron
func downloadElectron(downloadUrls []string) {
cacheDir := getCacheDir()
for _, url := range downloadUrls {
// 运用 path 包获取文件名
filename := path.Base(url)
// 假如 URL 中包括查询参数,需求额定处理
if strings.ContainsRune(filename, '?') {
filename = strings.Split(filename, "?")[0]
}
savePath := filepath.Join(cacheDir, filename)
file := downloadFile(url, savePath)
if file {
fmt.Printf("下载完成,保存至: %sn", savePath)
}
}
}
// 下载文件
func downloadFile(url string, savePath string) bool {
// 检查本地文件是否已存在
if _, err := os.Stat(savePath); err == nil {
fmt.Printf("n文件已存在,无需下载:%sn本地文件途径:%s nn", url, savePath)
return false
}
client := http.DefaultClient
client.Timeout = 60 * 10 * time.Second
reps, err := client.Get(url)
if err != nil {
log.Panic(err.Error())
}
if reps.StatusCode == http.StatusOK {
//保存文件
file, err := os.Create(savePath)
if err != nil {
log.Panic(err.Error())
}
defer file.Close() //关闭文件
//获取下载文件的巨细
length := reps.Header.Get("Content-Length")
size, _ := strconv.ParseInt(length, 10, 64)
body := reps.Body //获取文件内容
bar := pb.Full.Start64(size)
bar.SetWidth(120) //设置进度条宽度
bar.SetRefreshRate(10 * time.Millisecond) //设置改写速率
defer bar.Finish()
// create proxy reader
barReader := bar.NewProxyReader(body)
//写入文件
writer := io.Writer(file)
io.Copy(writer, barReader)
//defer fmt.Printf("n下载完成,保存至: %sn", savePath)
}
return true
}
// 读取当时目录下的文件
func readPackageFile() (string, error) {
// 读取 package.json 文件
filePath := "package.json"
jsonData, err := os.ReadFile(filePath)
if err != nil {
panic(err)
}
var parsedData map[string]interface{}
err = json.Unmarshal(jsonData, &parsedData)
if err != nil {
return "", err
}
electronVersion := parsedData["devDependencies"].(map[string]interface{})["electron"].(string)
re := regexp.MustCompile(`d+.d+.d+`)
versionNumbers := re.FindString(electronVersion)
return versionNumbers, nil
}
能够去我的 github
上下载对应的履行文件放到 package.json
同级目录就能够了。
进入到项目根目录,履行指令:
yarn install && ./electron-download-mac-arm64 // mac m1版别
yarn install && ./electron-download-mac-amd64 // mac x86版别
yarn install && ./electron-download-linux-amd64 // linux x86版别
yarn install && ./electron-download-linux-arm64 // linux x86版别
yarn install && ./electron-download-linux-arm64 // windows x86版别
yarn install && ./electron-download-windows-amd64.exe // mac x86版别
yarn install && ./electron-download-windows-arm64.exe // mac x86版别
当时环境已经装置好了
- mac履行打包脚本:
export ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
export ELECTRON_BUILDER_BINARIES_MIRROR="https://npmmirror.com/mirrors/electron-builder-binaries/"
yarn run build:mac
- windows32位打包:
export ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
export ELECTRON_BUILDER_BINARIES_MIRROR="https://npmmirror.com/mirrors/electron-builder-binaries/"
yarn run build:win32
- windows64位打包:
export ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
export ELECTRON_BUILDER_BINARIES_MIRROR="https://npmmirror.com/mirrors/electron-builder-binaries/"
yarn run build:win64
都需求装备环境变量