|
| 1 | +import fetch from 'node-fetch'; |
| 2 | +import { getOctokit, context } from '@actions/github'; |
| 3 | +import fs from 'fs'; |
| 4 | + |
| 5 | +import { resolveUpdateLog } from './updatelog.mjs'; |
| 6 | + |
| 7 | +const token = process.env.GITHUB_TOKEN; |
| 8 | +const version = process.env.TAG || '0.0.0'; |
| 9 | + |
| 10 | +async function updater() { |
| 11 | + if (!token) { |
| 12 | + console.log('GITHUB_TOKEN is required'); |
| 13 | + process.exit(1); |
| 14 | + } |
| 15 | + // const options = { owner: 'lencx', repo: 'OhMyBox' }; |
| 16 | + const options = { owner: context.repo.owner, repo: context.repo.repo }; |
| 17 | + const github = getOctokit(token); |
| 18 | + |
| 19 | + const { data: tags } = await github.rest.repos.listTags({ |
| 20 | + ...options, |
| 21 | + per_page: 10, |
| 22 | + page: 1, |
| 23 | + }); |
| 24 | + |
| 25 | + const tag = tags.find((t) => t.name.startsWith('v')); |
| 26 | + // console.log(`${JSON.stringify(tag, null, 2)}`); |
| 27 | + |
| 28 | + if (!tag) return; |
| 29 | + |
| 30 | + const { data: latestRelease } = await github.rest.repos.getReleaseByTag({ |
| 31 | + ...options, |
| 32 | + tag: tag.name, |
| 33 | + }); |
| 34 | + |
| 35 | + const updateData = { |
| 36 | + name: tag.name, |
| 37 | + notes: resolveUpdateLog(tag.name), // use UPDATE_LOG.md |
| 38 | + pub_date: new Date().toISOString(), |
| 39 | + version, |
| 40 | + platforms: { |
| 41 | + win64: { signature: '', url: '' }, // compatible with older formats |
| 42 | + linux: { signature: '', url: '' }, // compatible with older formats |
| 43 | + darwin: { signature: '', url: '' }, // compatible with older formats |
| 44 | + 'darwin-aarch64': { signature: '', url: '' }, |
| 45 | + 'darwin-intel': { signature: '', url: '' }, |
| 46 | + 'linux-x86_64': { signature: '', url: '' }, |
| 47 | + 'windows-x86_64': { signature: '', url: '' }, |
| 48 | + // 'windows-i686': { signature: '', url: '' }, // no supported |
| 49 | + }, |
| 50 | + }; |
| 51 | + |
| 52 | + const setAsset = async (asset, reg, platforms) => { |
| 53 | + let sig = ''; |
| 54 | + if (/.sig$/.test(asset.name)) { |
| 55 | + sig = await getSignature(asset.browser_download_url); |
| 56 | + } |
| 57 | + platforms.forEach((platform) => { |
| 58 | + if (reg.test(asset.name)) { |
| 59 | + // platform signature |
| 60 | + if (sig) { |
| 61 | + updateData.platforms[platform].signature = sig; |
| 62 | + return; |
| 63 | + } |
| 64 | + // platform url |
| 65 | + updateData.platforms[platform].url = asset.browser_download_url; |
| 66 | + } |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + const promises = latestRelease.assets.map(async (asset) => { |
| 71 | + // windows |
| 72 | + await setAsset(asset, /.msi.zip/, ['win64', 'windows-x86_64']); |
| 73 | + |
| 74 | + // darwin |
| 75 | + await setAsset(asset, /.app.tar.gz/, [ |
| 76 | + 'darwin', |
| 77 | + 'darwin-aarch64', |
| 78 | + 'darwin-intel', |
| 79 | + ]); |
| 80 | + |
| 81 | + // linux |
| 82 | + await setAsset(asset, /.AppImage.tar.gz/, ['linux', 'linux-x86_64']); |
| 83 | + }); |
| 84 | + await Promise.allSettled(promises); |
| 85 | + if (!fs.existsSync('updater')) { |
| 86 | + fs.mkdirSync('updater'); |
| 87 | + } |
| 88 | + fs.writeFileSync( |
| 89 | + './updater/install.json', |
| 90 | + JSON.stringify(updateData, null, 2) |
| 91 | + ); |
| 92 | + console.log('Generate updater/install.json'); |
| 93 | +} |
| 94 | + |
| 95 | +updater().catch(console.error); |
| 96 | + |
| 97 | +// get the signature file content |
| 98 | +async function getSignature(url) { |
| 99 | + try { |
| 100 | + const response = await fetch(url, { |
| 101 | + method: 'GET', |
| 102 | + headers: { 'Content-Type': 'application/octet-stream' }, |
| 103 | + }); |
| 104 | + return response.text(); |
| 105 | + } catch (_) { |
| 106 | + return ''; |
| 107 | + } |
| 108 | +} |
0 commit comments