postcat/scripts/releaseWindows.ts
2023-02-15 14:02:54 +08:00

73 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Octokit } from 'octokit';
import { readFileSync, writeFileSync } from 'fs';
import path from 'path';
const version = process.env.npm_package_version;
// https://github.com/octokit/core.js#readme
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN
});
const commonInfo = {
owner: 'Postcatlab',
repo: 'postcat'
};
const setup = async () => {
const { data: releaseList } = await octokit.rest.repos.listReleases(commonInfo);
const latestPath = path.join(__dirname, '../release/latest.yml');
const file = readFileSync(latestPath, 'utf8');
// @ts-ignore
writeFileSync(latestPath, file.replaceAll(`Postcat-Setup-${version}.exe`, `Postcat Setup ${version}.exe`));
const assetNames = [`Postcat-Setup-${version}.exe`, 'latest.yml'];
// 获取idupload_url
let targetRelease = releaseList.find(n => n.name === version)!;
if (!targetRelease) {
const { data } = await octokit.rest.repos.createRelease({
...commonInfo,
tag_name: `v${version}`,
name: version,
draft: true
});
targetRelease = data;
}
const { id, upload_url, assets } = targetRelease;
for await (const assetName of assetNames) {
const targetAsset = assets.find(n => n.name === assetName);
if (targetAsset) {
await octokit.rest.repos.deleteReleaseAsset({
...commonInfo,
asset_id: targetAsset.id
});
}
const data = readFileSync(`./release/${assetName}`);
let param = {
...commonInfo,
release_id: id,
name: assetName,
data: data,
origin: upload_url,
headers: {
'content-type': 'application/octet-stream'
}
};
// @ts-ignore
const res = await octokit.rest.repos.uploadReleaseAsset(param);
}
console.log('uploadReleaseAsset success!');
};
setup();