|
| 1 | +import path from 'path' |
| 2 | +import { fileURLToPath } from 'url' |
| 3 | +import fs from 'node:fs/promises' |
| 4 | +import child_process from 'child_process' |
| 5 | +import { promisify } from 'util' |
| 6 | +import * as github from '@actions/github' |
| 7 | + |
| 8 | +const exec = promisify(child_process.exec) |
| 9 | + |
| 10 | +declare global { |
| 11 | + namespace NodeJS { |
| 12 | + interface ProcessEnv { |
| 13 | + CLIENT_SECRET: string |
| 14 | + CLIENT_ID: string |
| 15 | + GITHUB_TOKEN: string |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 21 | +const envPath = path.join(dirname, '..', '..', '.env') |
| 22 | +const tauriConfPath = path.join(dirname, '..', '..', 'src-tauri', 'tauri.conf.json') |
| 23 | +const packageJsonPath = path.join(dirname, '..', '..', 'package.json') |
| 24 | +const token = process.env.GITHUB_TOKEN |
| 25 | +const secret = process.env.CLIENT_SECRET |
| 26 | +const id = process.env.CLIENT_ID |
| 27 | + |
| 28 | +const envFileContent = `\ |
| 29 | +VITE_CLIENT_SECRET=${secret} |
| 30 | +VITE_CLIENT_ID=${id} |
| 31 | +` |
| 32 | + |
| 33 | +async function run() { |
| 34 | + await fs.writeFile(envPath, envFileContent, 'utf-8') |
| 35 | + await exec('pnpm tauri build --target universal-apple-darwin') |
| 36 | + |
| 37 | + const tauriConf = JSON.parse( |
| 38 | + await fs.readFile(tauriConfPath, 'utf-8'), |
| 39 | + ) as typeof import('../../src-tauri/tauri.conf.json') |
| 40 | + |
| 41 | + const packageJSON = JSON.parse( |
| 42 | + await fs.readFile(packageJsonPath, 'utf-8'), |
| 43 | + ) as typeof import('../../package.json') |
| 44 | + |
| 45 | + if (packageJSON.version !== tauriConf.package.version) |
| 46 | + throw new Error('Tauri config and Package JSON versions are not the same, update both of them!') |
| 47 | + |
| 48 | + const dmgFileName = `Gitification_${packageJSON.version}_universal.dmg` |
| 49 | + |
| 50 | + const dmgPath = path.join( |
| 51 | + dirname, |
| 52 | + '..', |
| 53 | + '..', |
| 54 | + 'src-tauri', |
| 55 | + 'target', |
| 56 | + 'universal-apple-darwin', |
| 57 | + 'release', |
| 58 | + 'bundle', |
| 59 | + 'dmg', |
| 60 | + dmgFileName, |
| 61 | + ) |
| 62 | + |
| 63 | + const octokit = github.getOctokit(token) |
| 64 | + |
| 65 | + const remoteRelease = await octokit.rest.repos.getLatestRelease({ |
| 66 | + owner: 'Gitification-App', |
| 67 | + repo: 'gitification', |
| 68 | + }) |
| 69 | + |
| 70 | + if (remoteRelease.data.name === packageJSON.version.toString()) { |
| 71 | + console.log('No version change detected, skipping build.') |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + const release = await octokit.rest.repos.createRelease({ |
| 76 | + owner: 'Gitification-App', |
| 77 | + repo: 'gitification', |
| 78 | + tag_name: packageJSON.version.toString(), |
| 79 | + name: packageJSON.version.toString(), |
| 80 | + }) |
| 81 | + |
| 82 | + octokit.rest.repos.uploadReleaseAsset({ |
| 83 | + owner: 'Gitification-App', |
| 84 | + repo: 'gitification', |
| 85 | + name: dmgFileName, |
| 86 | + release_id: release.data.id, |
| 87 | + // @ts-expect-error type |
| 88 | + data: (await fs.readFile(dmgPath)).buffer, |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +run() |
0 commit comments