gongxiangp2/scripts/publish-xiaoyi-client.ts

88 lines
3.5 KiB
TypeScript

/**
* 使用已配置的 S3 环境变量运行。默认预演;确认后在命令末尾追加 --apply。
* npx tsx scripts/publish-xiaoyi-client.ts --zip <安装包路径> --release <release.json> --channel stable
* npx tsx scripts/publish-xiaoyi-client.ts --set-latest 1.0.0 --channel stable
*/
import { createHash } from 'node:crypto'
import { createReadStream } from 'node:fs'
import { readFile, stat } from 'node:fs/promises'
import { parseArgs } from 'node:util'
import { PutObjectCommand } from '@aws-sdk/client-s3'
import { s3Client } from '../lib/s3'
import { parseRelease, readRelease, releasePrefix, validChannel, validVersion, verifyReleaseFile } from '../lib/desktop-client/client-release'
export async function publishXiaoyiClient(args: string[]) {
const { values } = parseArgs({
args,
options: {
zip: { type: 'string' },
release: { type: 'string' },
channel: { type: 'string', default: 'stable' },
'set-latest': { type: 'string' },
apply: { type: 'boolean', default: false },
},
})
const { zip, release: manifestPath, channel, 'set-latest': rollback, apply } = values
if (!validChannel(channel)) throw new Error('发布通道无效')
const prefix = releasePrefix(channel)
let version: string
if (rollback !== undefined) {
if (zip || manifestPath || !validVersion(rollback)) throw new Error('请单独指定合法的 --set-latest 版本')
version = rollback
if (apply) {
const release = await readRelease(channel, version)
if (!release) throw new Error('回滚版本尚未发布')
await verifyReleaseFile(channel, release)
}
} else {
if (!zip || !manifestPath) throw new Error('请同时指定 --zip 和 --release')
const manifest = JSON.parse(await readFile(manifestPath, 'utf8'))
const release = parseRelease(manifest, channel, manifest?.version)
version = release.version
const file = await stat(zip)
if (!file.isFile() || file.size !== release.file.size) throw new Error('安装包大小与清单不一致')
const hash = createHash('sha256')
for await (const chunk of createReadStream(zip)) hash.update(chunk)
if (hash.digest('hex') !== release.file.sha256.toLowerCase()) throw new Error('安装包 SHA-256 与清单不一致')
if (apply) {
if (await readRelease(channel, version)) throw new Error('该版本已发布,可用 --set-latest 切换指针')
const body = createReadStream(zip)
try {
await s3Client.send(new PutObjectCommand({
Bucket: process.env.S3_BUCKET,
Key: `${prefix}/${version}/${release.file.name}`,
Body: body,
ContentLength: file.size,
ContentType: 'application/zip',
}))
} finally {
body.destroy()
}
await putJson(`${prefix}/${version}/release.json`, release)
}
}
if (apply) await putJson(`${prefix}/latest.json`, { version })
return { channel, version, applied: apply }
}
async function putJson(key: string, value: object) {
await s3Client.send(new PutObjectCommand({
Bucket: process.env.S3_BUCKET,
Key: key,
Body: JSON.stringify(value),
ContentType: 'application/json',
}))
}
if (require.main === module) {
publishXiaoyiClient(process.argv.slice(2)).then(({ channel, version, applied }) => {
console.log(`${applied ? '已切换最新版本' : '预演'} ${channel}/${version}`)
}).catch(() => {
console.error('发布失败,请检查参数、发布清单、安装包和存储配置。')
process.exitCode = 1
})
}