方法一
使用Electron-builder官方提供的接口,在你的electron-builder.yml配置文件里配置afterAllArtifactBuild属性.
afterAllArtifactBuild: scripts/afterBuild.js
afterBuild.js:
exports.default = function () {
console.log('------------------------------------afterBuild------------------------------------------');
return ['upgrade.json']; // 这个返回的数组就是你想要上传的所有文件路径。
}
方法二
思路
通过调用Github rest API接口进行查询、上传文件操作。
上传文件
创建一个本地文件,这里就叫update.js,代码如下:
const fs = require('fs');
const { Octokit } = require("@octokit/rest");
const yaml = require('js-yaml');
const { version } = require('./package.json');
let uploadUpgradeAsset = async function () {
const octokit = new Octokit({
auth: process.env.GH_TOKEN, //GH_TOKEN是你的github的personal access token,需要提前在本地环境中配置,不建议直接把token字符串放到这里,不安全。
});
// get Owner & Repository name
const asset = 'electron-builder.yml';
const { publish } = yaml.load(fs.readFileSync(asset, 'utf8'));
console.log('version:', version);
// upload
const assetName = 'upgrade.json'; //你要上传的文件名。the asset you want to upload to your release.
octokit.repos.listReleases({
owner: publish[0].owner,
repo: publish[0].repo,
}).then(res => {
// console.log('listReleases data:', res.data);
// 获取id,upload_url,
const { id, upload_url } = res.data.filter(obj => obj.name === version)[0];
console.log('id, upload_url', id, upload_url);
let data = fs.readFileSync(assetName);
let param = {
owner: publish[0].owner,
repo: publish[0].repo,
release_id: id,
name: assetName,
data: data,
origin: upload_url,
headers: {
'content-type': 'application/json',
},
};
console.log('param:', param);
// 上传
octokit.repos.uploadReleaseAsset(param).then(response => {
console.log('uploadAsset success:', response);
}).catch(error => {
console.log('uploadAsset error:', error);
});
}).catch(err => {
console.log('listReleases error:', err);
});
}
uploadUpgradeAsset();
配置package.json
"scripts": {
"pack:win": "run-s build:win upload",
"build:win": "electron-builder --win -p always",
"upload": "node update.js",
}