-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9fbad4e
commit 1dfaf3b
Showing
5 changed files
with
124 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Update, plugin, common } from 'node-karin' | ||
import { Restart, makeForwardMsg } from '#lib' | ||
import { Version } from '#components' | ||
import _ from 'lodash' | ||
|
||
let uping = false | ||
export class MusicUpdate extends plugin { | ||
constructor () { | ||
super({ | ||
name: '更新', | ||
event: 'message', | ||
priority: 1000, | ||
rule: [ | ||
{ | ||
reg: '^#?铃(插件)?(强制)?更新$', | ||
fnc: 'update', | ||
permission: 'master' | ||
}, | ||
{ | ||
reg: '^#?铃(插件)?更新日志$', | ||
fnc: 'update_log', | ||
permission: 'master' | ||
} | ||
] | ||
}) | ||
} | ||
|
||
async update (e = this.e) { | ||
if (uping) { | ||
e.reply(`正在更新${Version.pluginName},请稍后...`) | ||
return false | ||
} | ||
uping = true | ||
setTimeout(() => { | ||
uping = false | ||
}, 300 * 1000) | ||
|
||
let [ name, cmd ] = [ Version.pluginName, 'git pull' ] | ||
if (e.msg.includes('强制')) cmd = 'git reset --hard && git pull --allow-unrelated-histories' | ||
try { | ||
const { data } = await Update.update(Version.pluginPath, cmd) | ||
const msg = `更新${name}...${_.isObject(data) ? `${data.message}\n${data.stderr}` : data}` | ||
await this.replyForward(common.makeForward(msg)) | ||
if (!data.includes('更新成功')) return true | ||
try { | ||
await this.reply(`\n更新完成,开始重启 本次运行时间:${common.uptime()}`, { at: true }) | ||
const restart = new Restart(e) | ||
restart.e = e | ||
await restart.CmdRestart() | ||
return true | ||
} catch (error) { | ||
return e.reply(`${Version.pluginName}重启失败,请手动重启以应用更新!`) | ||
} | ||
} catch (error) { | ||
return e.reply(`更新失败:${error.message}`, { at: true }) | ||
} finally { | ||
uping = false | ||
} | ||
return true | ||
} | ||
|
||
async update_log (e = this.e) { | ||
try { | ||
const data = (await Update.getCommit({ path: Version.pluginPath, count: 10 })).replace(/\n\s*\n/g, '\n') | ||
const commitlist = data | ||
.split('\n') | ||
.filter(Boolean) | ||
.map((item) => item.trimEnd()) | ||
e.reply(await makeForwardMsg(e, commitlist)) | ||
return true | ||
} catch { | ||
await e.reply(`\n获取更新日志失败:\n${e.msg}`, { at: true }) | ||
return true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import puppeteer from './puppeteer.js' | ||
import Restart from './restart.js' | ||
import makeForwardMsg from './makeForwardMsg.js' | ||
|
||
export { puppeteer } | ||
export { puppeteer, Restart, makeForwardMsg } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Version } from '#components' | ||
import common from 'node-karin' | ||
|
||
const makeForwardMsg = await (async () => { | ||
return async (e, elements) => { | ||
return common.makeForward(elements, e.self_id, e.sender.name || e.sender.nick) | ||
} | ||
})() | ||
|
||
export default makeForwardMsg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { pathToFileURL } from 'url' | ||
import { Version } from '#components' | ||
|
||
const Restart = await (async () => { | ||
const baseRestartPath = path.join(process.cwd(), 'plugins/karin-plugin-basic/apps/restart.js') | ||
if (fs.existsSync(baseRestartPath)) { | ||
const restartUrl = pathToFileURL(baseRestartPath).href | ||
const { Restart } = await import(restartUrl) | ||
return Restart | ||
} else { | ||
logger.error('未安装karin-plugin-basic (https://github.com/KarinJS/karin-plugin-basic),无法提供重启支持,请安装后重试!') | ||
return false | ||
} | ||
})() | ||
|
||
export default Restart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters