Skip to content

Commit

Permalink
feat: plugin-temp-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayideyia committed Nov 28, 2024
1 parent c857074 commit c583edb
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 0 deletions.
151 changes: 151 additions & 0 deletions plugins/GFS/plugin-temp-alpha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/* 触发器 手动触发 */
const onRun = async () => {
await Rolling()
}

/* 触发器 启动APP时 */
const onStartup = async () => {
if (Plugin.AutoRollingMode === 'onStartup') {
// 延迟检测,确保内核已经启动
setTimeout(() => Rolling(false), (Plugin.AutoRollingDelay || 10) * 1000)
}
}

/* 触发器 APP就绪后 */
const onReady = async () => {
if (Plugin.AutoRollingMode === 'onReady') {
// 延迟检测,确保内核已经启动
setTimeout(() => Rolling(false), (Plugin.AutoRollingDelay || 10) * 1000)
}
}

/*
* 右键菜单 - 滚动版本
* params: confirm 是否进行交互式确认
*/
const Rolling = async (confirm = true) => {
await checkRollingReleaseEnabled()
await checkLatestVersion()

const GFC_URL = 'https://api.github.com/repos/GUI-for-Cores/GUI.for.Clash/releases/tags/rolling-release'
const GFS_URL = 'https://api.github.com/repos/GUI-for-Cores/GUI.for.SingBox/releases/tags/rolling-release-alpha'
const url = Plugins.APP_TITLE.includes('Clash') ? GFC_URL : GFS_URL

const { update, destroy, error } = Plugins.message.info(`[${Plugin.name}] 检测中...`, 999999)

const { body } = await Plugins.HttpGet(url, {
Authorization: Plugins.getGitHubApiAuthorization()
})

if (body.message) {
destroy()
throw body.message
}

const ZipFile = 'data/.cache/rolling-release.zip'
const BackupFile = 'data/.cache/rolling-release.backup'
const ZipUrl = body.assets.find((v) => v.name === 'rolling-release.zip')?.browser_download_url
const VersionUrl = body.assets.find((v) => v.name === 'version.txt')?.browser_download_url
const ChangelogUrl = body.assets.find((v) => v.name === 'changelog.md')?.browser_download_url

if (!ZipUrl || !VersionUrl) {
destroy()
throw '出现一些错误,无法找到更新资源包'
}

let localVersion = ''
let remoteVersion = ''

try {
const { body } = await Plugins.HttpGet(VersionUrl)
remoteVersion = body

const res = await fetch('/version.txt')
localVersion = await res.text()
} catch (err) {}

if (!remoteVersion) {
destroy()
throw '无法获取远程版本信息'
}

if (localVersion === remoteVersion) {
Plugins.message.success(`[${Plugin.name}] 当前版本已是最新(内测版)`)
destroy()
return
}

let changelog = '维护性更新'

if (ChangelogUrl && confirm) {
update('正在获取更新日志...')
const { body } = await Plugins.HttpGet(ChangelogUrl)
changelog = body
}
destroy()

confirm && (await Plugins.confirm(Plugin.name, changelog, { type: 'markdown' }))

const { update: update2, destroy: destroy2 } = Plugins.message.info('正在更新...')
try {
await Plugins.Download(ZipUrl, ZipFile, {}, (progress, total) => {
update2('正在更新...' + ((progress / total) * 100).toFixed(2) + '%')
})
await Plugins.ignoredError(Plugins.Movefile, 'data/rolling-release', BackupFile)
await Plugins.UnzipZIPFile(ZipFile, 'data')
await Plugins.Removefile(ZipFile)
await Plugins.Removefile(BackupFile)
destroy2()
const ok = await Plugins.confirm(Plugin.name, '更新成功,是否立即重载界面?').catch(() => 0)
ok && Plugins.WindowReloadApp()
} catch (err) {
error(err.message || err)
} finally {
Plugins.sleep(1500).then(() => destroy2())
}
}

/**
* 右键菜单 - 恢复版本
*/
const Recovery = async () => {
await checkRollingReleaseEnabled()
if (!(await Plugins.FileExists('data/rolling-release'))) {
Plugins.message.info('无需恢复,此版本已是默认版本。')
return
}
await Plugins.confirm(Plugin.name, '是否移除当前版本,恢复为默认版本?\n这将移除 data/rolling-release 目录。')
await Plugins.Removefile('data/rolling-release')
const ok = await Plugins.confirm(Plugin.name, '恢复成功,是否立即重载界面').catch(() => 0)
ok && (await Plugins.WindowReloadApp())
}

/**
* 右键菜单 - 更新日志
*/
const Changelog = async () => {
const url = `https://github.com/GUI-for-Cores/${Plugins.APP_TITLE}/releases/download/rolling-release/changelog.md`
const { body } = await Plugins.HttpGet(url)
await Plugins.alert(Plugin.name, body, { type: 'markdown' })
}

const checkRollingReleaseEnabled = async () => {
const appSettings = Plugins.useAppSettingsStore()
if (!appSettings.app.rollingRelease) {
throw '请在【设置】中,开启【启用滚动发行】功能。'
}
}

const checkLatestVersion = async () => {
const GFC_URL = 'https://api.github.com/repos/GUI-for-Cores/GUI.for.Clash/releases/latest'
const GFS_URL = 'https://api.github.com/repos/GUI-for-Cores/GUI.for.SingBox/releases/latest'
const url = Plugins.APP_TITLE.includes('Clash') ? GFC_URL : GFS_URL
const { body } = await Plugins.HttpGet(url, {
Authorization: Plugins.getGitHubApiAuthorization()
})
const { tag_name, message } = body
if (message) throw message
if (tag_name !== Plugins.APP_VERSION) {
throw '无法跨大版本升级,请通过 设置 - 关于,更新APP!'
}
}
38 changes: 38 additions & 0 deletions plugins/gfs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
[
{
"id": "plugin-temp-alpha",
"name": "滚动发行(内测版)",
"description": "此版本属于开发内测版,欢迎加入测试。",
"type": "Http",
"url": "https://raw.githubusercontent.com/GUI-for-Cores/Plugin-Hub/main/plugins/GFS/plugin-temp-alpha.js",
"path": "data/plugins/plugin-temp-alpha.js",
"triggers": ["on::manual", "on::ready", "on::startup"],
"menus": {
"滚动版本": "Rolling",
"恢复版本": "Recovery",
"更新日志": "Changelog"
},
"configuration": [
{
"id": "ID_wqicvdoc",
"title": "自动更新策略",
"description": "自动滚动至最新版本",
"key": "AutoRollingMode",
"component": "Radio",
"value": "disabled",
"options": ["不自动更新,disabled", "启动APP时,onStartup", "APP就绪时,onReady"]
},
{
"id": "ID_vognsfjh",
"title": "自动更新策略延迟",
"description": "延迟一段时间再执行更新检测,单位秒",
"key": "AutoRollingDelay",
"component": "Input",
"value": "10",
"options": []
}
],
"status": 0,
"disabled": false,
"install": false,
"installed": false
},
{
"id": "plugin-add-rulesets",
"name": "一键添加规则集",
Expand Down

0 comments on commit c583edb

Please sign in to comment.