From 689cbdc58e3aeecdb43f37e6f938799eafe83c7b Mon Sep 17 00:00:00 2001 From: Shigma Date: Thu, 8 Sep 2022 11:12:32 +0800 Subject: [PATCH] refa: migrate to node-gocqhttp --- install.js | 21 ---------------- package.json | 26 +++++++++----------- src/index.ts | 13 +++++----- src/install.ts | 66 -------------------------------------------------- 4 files changed, 17 insertions(+), 109 deletions(-) delete mode 100644 install.js delete mode 100644 src/install.ts diff --git a/install.js b/install.js deleted file mode 100644 index 1b27e90..0000000 --- a/install.js +++ /dev/null @@ -1,21 +0,0 @@ -const { existsSync } = require('fs') - -/** @type {import('./lib/install')} */ -let utils - -try { - utils = require('./lib/install') -} catch {} - -async function install() { - let version = process.env.GOCQHTTP_VERSION || 'v1.0.0-rc3' - if (version === 'latest') { - version = await utils.getLatestRelease() - } - - await utils.downloadRelease(version) -} - -if (utils && !existsSync(__dirname + '/bin')) { - install() -} diff --git a/package.json b/package.json index 8e1ce6a..112a494 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-gocqhttp", "description": "go-cqhttp launcher for Koishi", - "version": "3.0.7", + "version": "3.1.0", "main": "lib/index.js", "typings": "lib/index.d.ts", "files": [ @@ -13,8 +13,7 @@ "scripts": { "build:server": "tsc -b", "build:client": "koishi-console build", - "build": "yarn build:server && yarn build:client", - "postinstall": "node install" + "build": "yarn build:server && yarn build:client" }, "author": "Shigma ", "packageManager": "yarn@1.22.19", @@ -46,23 +45,20 @@ } }, "peerDependencies": { - "@koishijs/plugin-adapter-onebot": "^5.0.9", - "koishi": "^4.8.5" + "@koishijs/plugin-adapter-onebot": "^5.1.0", + "koishi": "^4.9.0" }, "devDependencies": { - "@koishijs/client": "^4.3.10", - "@koishijs/plugin-adapter-onebot": "^5.0.9", - "@koishijs/plugin-console": "^4.3.10", - "@koishijs/plugin-market": "^1.2.4", - "@octokit/openapi-types": "^11.2.0", + "@koishijs/client": "^4.5.1", + "@koishijs/plugin-adapter-onebot": "^5.1.0", + "@koishijs/plugin-console": "^4.5.1", + "@koishijs/plugin-market": "^1.3.1", "@types/node": "^18.6.4", - "@types/tar": "^6.1.1", - "koishi": "^4.8.5", + "koishi": "^4.9.0", "typescript": "^4.7.4" }, "dependencies": { - "axios": "^0.26.1", - "strip-ansi": "^6.0.1", - "tar": "^6.1.11" + "go-cqhttp": "^1.1.3", + "strip-ansi": "^6.0.1" } } diff --git a/src/index.ts b/src/index.ts index 0903c26..754cfc9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,12 +2,13 @@ import { Context, Dict, interpolate, Logger, Schema } from 'koishi' import OneBotBot from '@koishijs/plugin-adapter-onebot' import { DataService } from '@koishijs/plugin-console' import {} from '@koishijs/plugin-market' -import { ChildProcess, spawn } from 'child_process' +import { ChildProcess } from 'child_process' import { resolve } from 'path' import { createReadStream, promises as fsp } from 'fs' +import gocqhttp from 'go-cqhttp' import strip from 'strip-ansi' -const { mkdir, copyFile, readFile, writeFile } = fsp +const { mkdir, readFile, writeFile } = fsp declare module '@koishijs/plugin-console' { namespace Console { @@ -138,9 +139,7 @@ class Launcher extends DataService> { async connect(bot: OneBotBot) { // create working folder const cwd = resolve(bot.ctx.baseDir, this.config.root, bot.selfId) - const file = '/go-cqhttp' + (process.platform === 'win32' ? '.exe' : '') await mkdir(cwd, { recursive: true }) - await copyFile(resolve(__dirname, '../bin/go-cqhttp'), cwd + file) // create config.yml await writeFile(cwd + '/config.yml', await this.getConfig(bot)) @@ -149,7 +148,7 @@ class Launcher extends DataService> { this.setData(bot, { status: 'init' }) // spawn go-cqhttp process - bot.process = spawn('.' + file, ['-faststart'], { cwd }) + bot.process = gocqhttp({ cwd, faststart: true }) bot.process.stdout.on('data', async (data: string) => { data = strip(data.toString()).trim() @@ -182,13 +181,13 @@ class Launcher extends DataService> { this.payload[bot.sid].phone = phone } } else if (text.includes('captcha.jpg')) { - const buffer = await fsp.readFile(cwd + '/captcha.png') + const buffer = await readFile(cwd + '/captcha.png') this.setData(bot, { status: 'captcha', image: 'data:image/png;base64,' + buffer.toString('base64'), }) } else if (text.includes('qrcode.png')) { - const buffer = await fsp.readFile(cwd + '/qrcode.png') + const buffer = await readFile(cwd + '/qrcode.png') this.setData(bot, { status: 'qrcode', image: 'data:image/png;base64,' + buffer.toString('base64'), diff --git a/src/install.ts b/src/install.ts deleted file mode 100644 index e57fca2..0000000 --- a/src/install.ts +++ /dev/null @@ -1,66 +0,0 @@ -import axios from 'axios' -import { createWriteStream, existsSync, promises as fsp } from 'fs' -import { components } from '@octokit/openapi-types' -import { resolve } from 'path' -import { extract } from 'tar' - -type Release = components['schemas']['release'] - -function getArch() { - switch (process.arch) { - // @ts-ignore - case 'x32': return '386' - case 'x64': return 'amd64' - case 'arm64': return 'arm64' - case 'arm': return 'armv7' - } - - throw new Error(`architecture "${process.arch}" is not supported`) -} - -function getPlatform() { - switch (process.platform) { - case 'darwin': return 'darwin' - case 'linux': return 'linux' - case 'win32': return 'windows' - } - - throw new Error(`platform "${process.platform}" is not supported`) -} - -export async function getLatestRelease(repo: string) { - const { data } = await axios.get(`https://api.github.com/repos/${repo}/releases`) - return data[0].tag_name -} - -export async function downloadRelease(tag: string) { - const arch = getArch() - const platform = getPlatform() - const outDir = resolve(__dirname, '../bin') - - if (existsSync(outDir)) return - - const name = `go-cqhttp_${platform}_${arch}.${platform === 'windows' ? 'exe' : 'tar.gz'}` - const mirror = process.env.GITHUB_MIRROR || 'https://github.com' - const url = `${mirror}/Mrs4s/go-cqhttp/releases/download/${tag}/${name}` - - try { - const [{ data: stream }] = await Promise.all([ - axios.get(url, { responseType: 'stream' }), - fsp.mkdir(outDir, { recursive: true }), - ]) - - return await new Promise((resolve, reject) => { - stream.on('end', resolve) - stream.on('error', reject) - if (platform === 'windows') { - stream.pipe(createWriteStream(outDir + '/go-cqhttp')) - } else { - stream.pipe(extract({ cwd: outDir, newer: true }, ['go-cqhttp'])) - } - }) - } catch (error) { - console.warn(error) - return fsp.rm(outDir, { force: true, recursive: true }) - } -}