node-karin 0.6.14 → 0.6.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/adapter/input/index.d.ts +2 -2
- package/lib/adapter/input/index.js +1 -1
- package/lib/adapter/kritor/grpc.js +1 -1
- package/lib/adapter/onebot/onebot11.d.ts +4 -4
- package/lib/adapter/onebot/onebot11.js +3 -3
- package/lib/cli/init.js +29 -0
- package/lib/cli/log.d.ts +2 -0
- package/lib/cli/log.js +12 -0
- package/lib/cli/postinstall.d.ts +2 -0
- package/lib/cli/postinstall.js +25 -0
- package/lib/core/plugin.d.ts +2 -4
- package/lib/core/plugin.js +3 -5
- package/lib/core/plugin.loader.d.ts +4 -1
- package/lib/core/plugin.loader.js +13 -9
- package/lib/event/message.d.ts +2 -2
- package/lib/event/message.js +1 -1
- package/lib/types/adapter.d.ts +2 -1
- package/lib/types/api.d.ts +1 -1
- package/lib/types/event.d.ts +1 -1
- package/lib/types/logger.d.ts +3 -2
- package/lib/types/plugin.d.ts +1 -0
- package/lib/utils/ffmpeg.d.ts +1 -1
- package/lib/utils/ffmpeg.js +1 -1
- package/lib/utils/init.d.ts +48 -0
- package/lib/utils/init.js +205 -0
- package/package.json +11 -10
- package/lib/tools/init.js +0 -65
- /package/lib/{tools → cli}/init.d.ts +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { KarinAdapter } from '../../types/adapter.js';
|
|
2
|
-
import { contact, KarinElement } from '../../types/index.js';
|
|
2
|
+
import { contact, KarinElement, LoggerLevel } from '../../types/index.js';
|
|
3
3
|
/**
|
|
4
4
|
* - 标准输入输出适配器
|
|
5
5
|
*/
|
|
@@ -14,7 +14,7 @@ export declare class AdapterInput implements KarinAdapter {
|
|
|
14
14
|
get self_id(): string;
|
|
15
15
|
stdin(): this;
|
|
16
16
|
init(): void;
|
|
17
|
-
logger(level:
|
|
17
|
+
logger(level: LoggerLevel, ...args: any[]): void;
|
|
18
18
|
GetVersion(): Promise<{
|
|
19
19
|
name: string;
|
|
20
20
|
app_name: string;
|
|
@@ -80,7 +80,7 @@ export class KritorGrpc {
|
|
|
80
80
|
user_id: sender.uid + '',
|
|
81
81
|
time: kritorData.time,
|
|
82
82
|
message_id: kritorData.message_id + '',
|
|
83
|
-
message_seq: kritorData.message_seq
|
|
83
|
+
message_seq: Number(kritorData.message_seq),
|
|
84
84
|
sender: {
|
|
85
85
|
...sender,
|
|
86
86
|
uid: sender.uid + '',
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
2
|
import { IncomingMessage } from 'http';
|
|
3
|
-
import { KarinAdapter } from '../../types/index.js';
|
|
3
|
+
import { KarinAdapter, LoggerLevel } from '../../types/index.js';
|
|
4
4
|
import { contact, OneBot11Api, KarinElement, OneBot11Segment, CustomNodeSegment, OneBot11ApiParamsType, GroupInfo, KarinNodeElement } from '../../types/index.js';
|
|
5
5
|
/**
|
|
6
6
|
* @class OneBot11
|
|
@@ -44,7 +44,7 @@ export declare class AdapterOneBot11 implements KarinAdapter {
|
|
|
44
44
|
/**
|
|
45
45
|
* 专属当前Bot的日志打印方法
|
|
46
46
|
*/
|
|
47
|
-
logger(level:
|
|
47
|
+
logger(level: LoggerLevel, ...args: any[]): void;
|
|
48
48
|
/**
|
|
49
49
|
* 获取头像url
|
|
50
50
|
* @param 头像大小,默认`0`
|
|
@@ -277,14 +277,14 @@ export declare class AdapterOneBot11 implements KarinAdapter {
|
|
|
277
277
|
* @param name - 文件名称 必须提供
|
|
278
278
|
* @param folder - 父目录ID 不提供则上传到根目录
|
|
279
279
|
*/
|
|
280
|
-
|
|
280
|
+
UploadGroupFile(group_id: string, file: string, name: string, folder?: string): Promise<any>;
|
|
281
281
|
/**
|
|
282
282
|
* 上传私聊文件
|
|
283
283
|
* @param user_id - 用户ID
|
|
284
284
|
* @param file - 本地文件绝对路径
|
|
285
285
|
* @param name - 文件名称 必须提供
|
|
286
286
|
*/
|
|
287
|
-
|
|
287
|
+
UploadPrivateFile(user_id: string, file: string, name: string): Promise<any>;
|
|
288
288
|
SetEssenceMessage(): Promise<void>;
|
|
289
289
|
DeleteEssenceMessage(): Promise<void>;
|
|
290
290
|
SetFriendApplyResult(): Promise<void>;
|
|
@@ -157,7 +157,7 @@ export class AdapterOneBot11 {
|
|
|
157
157
|
user_id: data.sender.user_id + '',
|
|
158
158
|
time: data.time,
|
|
159
159
|
message_id: data.message_id + '',
|
|
160
|
-
message_seq: data.message_id
|
|
160
|
+
message_seq: data.message_id,
|
|
161
161
|
sender: {
|
|
162
162
|
...data.sender,
|
|
163
163
|
uid: data.sender.user_id + '',
|
|
@@ -1223,7 +1223,7 @@ export class AdapterOneBot11 {
|
|
|
1223
1223
|
* @param name - 文件名称 必须提供
|
|
1224
1224
|
* @param folder - 父目录ID 不提供则上传到根目录
|
|
1225
1225
|
*/
|
|
1226
|
-
async
|
|
1226
|
+
async UploadGroupFile(group_id, file, name, folder) {
|
|
1227
1227
|
return await this.SendApi('upload_group_file', { group_id, file, name, folder });
|
|
1228
1228
|
}
|
|
1229
1229
|
/**
|
|
@@ -1232,7 +1232,7 @@ export class AdapterOneBot11 {
|
|
|
1232
1232
|
* @param file - 本地文件绝对路径
|
|
1233
1233
|
* @param name - 文件名称 必须提供
|
|
1234
1234
|
*/
|
|
1235
|
-
async
|
|
1235
|
+
async UploadPrivateFile(user_id, file, name) {
|
|
1236
1236
|
return await this.SendApi('upload_private_file', { user_id, file, name });
|
|
1237
1237
|
}
|
|
1238
1238
|
async SetEssenceMessage() { }
|
package/lib/cli/init.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { select } from '@inquirer/prompts'
|
|
3
|
+
import { KarinInit } from '../utils/init.js'
|
|
4
|
+
async function main () {
|
|
5
|
+
/** 捕获错误 打印日志 */
|
|
6
|
+
process.on('uncaughtException', err => console.error(err))
|
|
7
|
+
process.on('unhandledRejection', err => console.error(err))
|
|
8
|
+
const init = new KarinInit()
|
|
9
|
+
init.init()
|
|
10
|
+
const prompt = await select({
|
|
11
|
+
message: '请选择npm源 中国大陆服务器一定要更换!!!',
|
|
12
|
+
choices: [
|
|
13
|
+
{ name: '淘宝源(推荐)', value: 'https://registry.npmmirror.com' },
|
|
14
|
+
{ name: '官方源', value: 'https://registry.npmjs.org' },
|
|
15
|
+
],
|
|
16
|
+
})
|
|
17
|
+
/** 结果 */
|
|
18
|
+
await init.changeRegistry(prompt)
|
|
19
|
+
const pkg = await select({
|
|
20
|
+
message: '请选择包管理器 如果不知道怎么选 请选pnpm',
|
|
21
|
+
choices: [
|
|
22
|
+
{ name: 'pnpm', value: 'pnpm' },
|
|
23
|
+
{ name: 'yarn', value: 'yarn' },
|
|
24
|
+
],
|
|
25
|
+
})
|
|
26
|
+
/** 结果 */
|
|
27
|
+
await init.install(pkg)
|
|
28
|
+
}
|
|
29
|
+
main().then(() => process.exit(0)).catch(() => process.exit(0))
|
package/lib/cli/log.d.ts
ADDED
package/lib/cli/log.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import yaml from 'yaml'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
import { spawn } from 'child_process'
|
|
6
|
+
const cwd = process.cwd()
|
|
7
|
+
const _path = path.resolve(cwd + '/config/config/pm2.yaml')
|
|
8
|
+
const data = yaml.parse(fs.readFileSync(_path, 'utf8'))
|
|
9
|
+
const name = data.apps[0].name
|
|
10
|
+
const lines = data.lines || 1000
|
|
11
|
+
const cmd = process.platform === 'win32' ? 'pm2.cmd' : 'pm2'
|
|
12
|
+
spawn(cmd, ['logs', '--lines', lines, name], { stdio: 'inherit', shell: true, cwd })
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import { KarinInit } from '../utils/init.js'
|
|
4
|
+
async function main () {
|
|
5
|
+
/** 捕获错误 打印日志 */
|
|
6
|
+
process.on('uncaughtException', err => console.error(err))
|
|
7
|
+
process.on('unhandledRejection', err => console.error(err))
|
|
8
|
+
/** 在src目录说明为开发环境 不执行任何初始化操作 */
|
|
9
|
+
if (fs.existsSync('./src')) { return }
|
|
10
|
+
const init = new KarinInit()
|
|
11
|
+
init.init()
|
|
12
|
+
/** 判断锁文件 优先度: pnpm > yarn > cnpm > npm */
|
|
13
|
+
let pkg
|
|
14
|
+
if (fs.existsSync('pnpm-lock.yaml')) {
|
|
15
|
+
pkg = 'pnpm'
|
|
16
|
+
} else if (fs.existsSync('yarn.lock')) {
|
|
17
|
+
pkg = 'yarn'
|
|
18
|
+
} else if (fs.existsSync('cnpm-lock.yaml')) {
|
|
19
|
+
pkg = 'cnpm'
|
|
20
|
+
} else {
|
|
21
|
+
pkg = 'npm'
|
|
22
|
+
}
|
|
23
|
+
await init.install(pkg)
|
|
24
|
+
}
|
|
25
|
+
main().then(() => process.exit(0)).catch(() => process.exit(0))
|
package/lib/core/plugin.d.ts
CHANGED
|
@@ -3,10 +3,6 @@ import { PluginType, KarinElement, KarinNodeElement, EventType, KarinNoticeEvent
|
|
|
3
3
|
* 插件基类
|
|
4
4
|
*/
|
|
5
5
|
export declare class Plugin implements PluginType {
|
|
6
|
-
e: EventType<this>;
|
|
7
|
-
init?: () => Promise<any>;
|
|
8
|
-
accept?: (e: any) => Promise<any>;
|
|
9
|
-
replyCallback: PluginType['replyCallback'];
|
|
10
6
|
/**
|
|
11
7
|
* @param name - 插件名称
|
|
12
8
|
*/
|
|
@@ -159,6 +155,8 @@ export declare class Plugin implements PluginType {
|
|
|
159
155
|
* 清除上下文状态
|
|
160
156
|
*/
|
|
161
157
|
finish(): void;
|
|
158
|
+
e: EventType<this>;
|
|
159
|
+
replyCallback: PluginType['replyCallback'];
|
|
162
160
|
}
|
|
163
161
|
/**
|
|
164
162
|
* 上下文状态
|
package/lib/core/plugin.js
CHANGED
|
@@ -2,11 +2,6 @@
|
|
|
2
2
|
* 插件基类
|
|
3
3
|
*/
|
|
4
4
|
export class Plugin {
|
|
5
|
-
// 类型 需要根据e中的event类型来确定
|
|
6
|
-
e
|
|
7
|
-
init
|
|
8
|
-
accept
|
|
9
|
-
replyCallback
|
|
10
5
|
/**
|
|
11
6
|
* @param name - 插件名称
|
|
12
7
|
*/
|
|
@@ -136,6 +131,9 @@ export class Plugin {
|
|
|
136
131
|
delete stateArr[key]
|
|
137
132
|
}
|
|
138
133
|
}
|
|
134
|
+
|
|
135
|
+
e
|
|
136
|
+
replyCallback
|
|
139
137
|
}
|
|
140
138
|
/**
|
|
141
139
|
* 上下文状态
|
|
@@ -85,7 +85,10 @@ declare class PluginLoader {
|
|
|
85
85
|
* @param path - 插件路径
|
|
86
86
|
* @param lang - 语言环境
|
|
87
87
|
*/
|
|
88
|
-
getIndex(path: string, lang: 'js' | 'ts'):
|
|
88
|
+
getIndex(path: string, dir: dirName, lang: 'js' | 'ts'): {
|
|
89
|
+
dir: dirName;
|
|
90
|
+
name: fileName;
|
|
91
|
+
} | false;
|
|
89
92
|
/**
|
|
90
93
|
* 获取指定文件夹下的所有插件
|
|
91
94
|
* @param dir - 插件包名称
|
|
@@ -126,10 +126,10 @@ class PluginLoader {
|
|
|
126
126
|
/** package */
|
|
127
127
|
const pack = common.readJson(`${PluginPath}/package.json`)
|
|
128
128
|
/** 旧版本入口文件 */
|
|
129
|
-
const index = this.getIndex(PluginPath, process.env.karin_app_lang)
|
|
129
|
+
const index = this.getIndex(PluginPath, dir, process.env.karin_app_lang)
|
|
130
130
|
if (index) {
|
|
131
|
-
this.FileList.push({ dir, name: index })
|
|
132
|
-
this.isDev && this.watchList.push({ dir, name: index })
|
|
131
|
+
this.FileList.push({ dir: index.dir, name: index.name })
|
|
132
|
+
this.isDev && this.watchList.push({ dir: index.dir, name: index.name })
|
|
133
133
|
}
|
|
134
134
|
/** 新版本入口 */
|
|
135
135
|
if (pack.main) {
|
|
@@ -175,11 +175,15 @@ class PluginLoader {
|
|
|
175
175
|
* @param path - 插件路径
|
|
176
176
|
* @param lang - 语言环境
|
|
177
177
|
*/
|
|
178
|
-
getIndex (path, lang) {
|
|
178
|
+
getIndex (path, dir, lang) {
|
|
179
179
|
const isJS = common.exists(`${path}/index.js`)
|
|
180
|
-
if (isJS && lang === 'js') {
|
|
181
|
-
|
|
182
|
-
|
|
180
|
+
if (isJS && lang === 'js') {
|
|
181
|
+
return { dir, name: 'index.js' }
|
|
182
|
+
}
|
|
183
|
+
const isTS = common.exists(`${path}/src/index.ts`)
|
|
184
|
+
if (isTS && lang === 'ts') {
|
|
185
|
+
return { dir: `${dir}/src`, name: 'index.ts' }
|
|
186
|
+
}
|
|
183
187
|
return false
|
|
184
188
|
}
|
|
185
189
|
|
|
@@ -386,7 +390,7 @@ class PluginLoader {
|
|
|
386
390
|
* 新增accept、handler
|
|
387
391
|
*/
|
|
388
392
|
async addAccept (index, Class) {
|
|
389
|
-
if (
|
|
393
|
+
if ('accept' in Class && typeof Class.accept === 'function') {
|
|
390
394
|
this.PluginList[index].accept = true
|
|
391
395
|
this.acceptIds.push(index)
|
|
392
396
|
}
|
|
@@ -410,7 +414,7 @@ class PluginLoader {
|
|
|
410
414
|
* 执行初始化
|
|
411
415
|
*/
|
|
412
416
|
async addInit (Class) {
|
|
413
|
-
Class.init && await Class.init()
|
|
417
|
+
'init' in Class && typeof Class.init === 'function' && await Class.init()
|
|
414
418
|
}
|
|
415
419
|
|
|
416
420
|
/**
|
package/lib/event/message.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export declare class KarinMessage extends KarinEvent {
|
|
|
32
32
|
/**
|
|
33
33
|
* - 消息序列号
|
|
34
34
|
*/
|
|
35
|
-
message_seq?:
|
|
35
|
+
message_seq?: number;
|
|
36
36
|
/**
|
|
37
37
|
* - 原始消息文本
|
|
38
38
|
*/
|
|
@@ -61,7 +61,7 @@ export declare class KarinMessage extends KarinEvent {
|
|
|
61
61
|
/**
|
|
62
62
|
* - 消息序列号
|
|
63
63
|
*/
|
|
64
|
-
message_seq?:
|
|
64
|
+
message_seq?: number;
|
|
65
65
|
/**
|
|
66
66
|
* - 原始消息文本
|
|
67
67
|
*/
|
package/lib/event/message.js
CHANGED
|
@@ -3,7 +3,7 @@ import { KarinEvent } from './event.js'
|
|
|
3
3
|
* - 消息事件基类
|
|
4
4
|
*/
|
|
5
5
|
export class KarinMessage extends KarinEvent {
|
|
6
|
-
constructor ({ event = 'message', event_id, self_id, user_id, time, message_id, message_seq =
|
|
6
|
+
constructor ({ event = 'message', event_id, self_id, user_id, time, message_id, message_seq = 0, raw_message = '', contact, sender, elements, group_id = '' }) {
|
|
7
7
|
super({ event, event_id: event_id || message_id, self_id, user_id, group_id, time, contact, sender, sub_event: contact.scene === 'group' ? 'group_message' : 'private_message' })
|
|
8
8
|
this.message_id = message_id
|
|
9
9
|
this.message_seq = message_seq
|
package/lib/types/adapter.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { IncomingMessage } from 'http';
|
|
|
3
3
|
import { contact } from './event.js';
|
|
4
4
|
import { KarinElement, KarinNodeElement } from './element.js';
|
|
5
5
|
import { PushMessageBody, EssenceMessageBody, FriendInfo, GroupInfo, GroupMemberInfo, GroupHonorInfo } from './api.js';
|
|
6
|
+
import { LoggerLevel } from './logger.js';
|
|
6
7
|
export interface KarinAdapter {
|
|
7
8
|
/**
|
|
8
9
|
* - 适配器版本信息
|
|
@@ -82,7 +83,7 @@ export interface KarinAdapter {
|
|
|
82
83
|
* @param level - 日志等级
|
|
83
84
|
* @param args - 日志内容
|
|
84
85
|
*/
|
|
85
|
-
logger(level:
|
|
86
|
+
logger(level: LoggerLevel, ...args: any[]): void;
|
|
86
87
|
/**
|
|
87
88
|
* - 可选方法 仅ws有效 在注册适配器后 如果有符合的path则会调用此方法
|
|
88
89
|
* - @param socket - WebSocket实例
|
package/lib/types/api.d.ts
CHANGED
package/lib/types/event.d.ts
CHANGED
package/lib/types/logger.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
|
+
export type LoggerLevel = 'trace' | 'debug' | 'mark' | 'info' | 'mark' | 'warn' | 'error' | 'fatal';
|
|
2
3
|
export interface Logger {
|
|
3
4
|
/**
|
|
4
5
|
* 颜色模块
|
|
@@ -50,7 +51,7 @@ export interface Logger {
|
|
|
50
51
|
* @param id 机器人ID
|
|
51
52
|
* @param args 参数
|
|
52
53
|
*/
|
|
53
|
-
bot: (level:
|
|
54
|
+
bot: (level: LoggerLevel, id: string, ...args: string[]) => void;
|
|
54
55
|
}
|
|
55
56
|
declare module 'log4js' {
|
|
56
57
|
interface Logger {
|
|
@@ -104,6 +105,6 @@ declare module 'log4js' {
|
|
|
104
105
|
* @param id 机器人ID
|
|
105
106
|
* @param args 参数
|
|
106
107
|
*/
|
|
107
|
-
bot: (level:
|
|
108
|
+
bot: (level: LoggerLevel, id: string, ...args: string[]) => void;
|
|
108
109
|
}
|
|
109
110
|
}
|
package/lib/types/plugin.d.ts
CHANGED
package/lib/utils/ffmpeg.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 执行 ffmpeg 命令
|
|
3
3
|
*/
|
|
4
|
-
export
|
|
4
|
+
export declare function ffmpeg(): Promise<false | ((cmd: string, log?: boolean, options?: {
|
|
5
5
|
cwd: string;
|
|
6
6
|
encoding: string;
|
|
7
7
|
}) => Promise<{
|
package/lib/utils/ffmpeg.js
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
export declare class KarinInit {
|
|
3
|
+
/**
|
|
4
|
+
* 基础目录
|
|
5
|
+
*/
|
|
6
|
+
baseDir: string[];
|
|
7
|
+
constructor();
|
|
8
|
+
init(): void;
|
|
9
|
+
/**
|
|
10
|
+
* 递归创建目录
|
|
11
|
+
* @param dirname - 路径
|
|
12
|
+
*/
|
|
13
|
+
mkdir(dirname: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* 解析json
|
|
16
|
+
*/
|
|
17
|
+
readJson(file: string): any;
|
|
18
|
+
/**
|
|
19
|
+
* 获取插件列表
|
|
20
|
+
*/
|
|
21
|
+
getPlugins(): string[];
|
|
22
|
+
/**
|
|
23
|
+
* 从npm包内复制默认配置出来
|
|
24
|
+
*/
|
|
25
|
+
copyFile(): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* 修改package.json以支持运行
|
|
28
|
+
*/
|
|
29
|
+
modifyPackage(): void;
|
|
30
|
+
/**
|
|
31
|
+
* 更换镜像源为淘宝源
|
|
32
|
+
*/
|
|
33
|
+
changeRegistry(host: string): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* 安装pnpm或者yarn
|
|
36
|
+
* @param type - 包管理器名称
|
|
37
|
+
*/
|
|
38
|
+
install(type: 'pnpm' | 'yarn' | 'cnpm' | 'npm'): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* 执行命令
|
|
41
|
+
* @param cmd - 命令
|
|
42
|
+
*/
|
|
43
|
+
shell(cmd: string): Promise<unknown>;
|
|
44
|
+
/**
|
|
45
|
+
* 获取当前的包管理器
|
|
46
|
+
*/
|
|
47
|
+
getRegistry(): 'pnpm' | 'cnpm' | 'yarn' | 'npm';
|
|
48
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import { exec } from 'child_process'
|
|
5
|
+
import { karinDir } from '../core/dir.js'
|
|
6
|
+
export class KarinInit {
|
|
7
|
+
/**
|
|
8
|
+
* 基础目录
|
|
9
|
+
*/
|
|
10
|
+
baseDir
|
|
11
|
+
constructor () {
|
|
12
|
+
this.baseDir = [
|
|
13
|
+
'./lib',
|
|
14
|
+
'./config/config',
|
|
15
|
+
'./config/defSet',
|
|
16
|
+
'./config/view',
|
|
17
|
+
'./config/plugin',
|
|
18
|
+
'./temp/input',
|
|
19
|
+
'./temp/html',
|
|
20
|
+
'./plugins/karin-plugin-example',
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
init () {
|
|
25
|
+
this.baseDir.forEach(dir => this.mkdir(dir))
|
|
26
|
+
/** 非src源代码环境才创建 */
|
|
27
|
+
if (!fs.existsSync('./src')) {
|
|
28
|
+
this.copyFile()
|
|
29
|
+
this.modifyPackage()
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 递归创建目录
|
|
35
|
+
* @param dirname - 路径
|
|
36
|
+
*/
|
|
37
|
+
mkdir (dirname) {
|
|
38
|
+
if (fs.existsSync(dirname)) { return true }
|
|
39
|
+
if (this.mkdir(path.dirname(dirname))) { fs.mkdirSync(dirname) }
|
|
40
|
+
return true
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 解析json
|
|
45
|
+
*/
|
|
46
|
+
readJson (file) {
|
|
47
|
+
return JSON.parse(fs.readFileSync(file, 'utf-8'))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 获取插件列表
|
|
52
|
+
*/
|
|
53
|
+
getPlugins () {
|
|
54
|
+
const pluginsDir = './plugins'
|
|
55
|
+
const plugins = fs.readdirSync(pluginsDir)
|
|
56
|
+
return plugins.filter(dir => dir.startsWith('karin-plugin-'))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 从npm包内复制默认配置出来
|
|
61
|
+
*/
|
|
62
|
+
async copyFile () {
|
|
63
|
+
/** 配置 */
|
|
64
|
+
const list = ['config/defSet', 'config/view']
|
|
65
|
+
list.forEach(dir => {
|
|
66
|
+
const pkgDir = path.join(karinDir, dir)
|
|
67
|
+
const projDir = path.join(process.cwd(), dir)
|
|
68
|
+
/** 清空projDir目录下的文件 保留目录 */
|
|
69
|
+
if (fs.existsSync(projDir)) { fs.rmdirSync(projDir, { recursive: true }) }
|
|
70
|
+
/** 读取pkgDir目录下的所有文件 复制到projDir下 */
|
|
71
|
+
const files = fs.readdirSync(pkgDir).filter(file => file.endsWith('.yaml'))
|
|
72
|
+
files.forEach(file => {
|
|
73
|
+
const destPath = path.join(projDir, file)
|
|
74
|
+
const srcPath = path.join(pkgDir, file)
|
|
75
|
+
fs.copyFileSync(srcPath, destPath)
|
|
76
|
+
})
|
|
77
|
+
})
|
|
78
|
+
/** 创建pnpm工作区配置 */
|
|
79
|
+
fs.copyFileSync(path.join(karinDir, 'pnpm-workspace.yaml'), './pnpm-workspace.yaml')
|
|
80
|
+
/** 为每个插件包创建统一存储的文件夹 */
|
|
81
|
+
const plugins = this.getPlugins()
|
|
82
|
+
const DataList = [
|
|
83
|
+
'data',
|
|
84
|
+
'temp',
|
|
85
|
+
'resources',
|
|
86
|
+
'temp/html',
|
|
87
|
+
'config/plugin',
|
|
88
|
+
]
|
|
89
|
+
DataList.forEach(_path => {
|
|
90
|
+
_path = path.join(process.cwd(), _path)
|
|
91
|
+
plugins.forEach(plugin => this.mkdir(path.join(_path, plugin)))
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 修改package.json以支持运行
|
|
97
|
+
*/
|
|
98
|
+
modifyPackage () {
|
|
99
|
+
const pkg = this.readJson(path.join(karinDir, 'package.json'))
|
|
100
|
+
const projPkg = this.readJson('./package.json')
|
|
101
|
+
delete pkg.bin
|
|
102
|
+
pkg.main = './node_modules/node-karin/lib/index.js'
|
|
103
|
+
pkg.dependencies['node-karin'] = 'latest'
|
|
104
|
+
pkg.dependencies['kritor-proto'] = 'latest'
|
|
105
|
+
pkg.dependencies = { ...projPkg.dependencies, ...pkg.dependencies }
|
|
106
|
+
pkg.devDependencies = { ...projPkg.devDependencies, ...pkg.devDependencies }
|
|
107
|
+
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 更换镜像源为淘宝源
|
|
112
|
+
*/
|
|
113
|
+
async changeRegistry (host) {
|
|
114
|
+
const cmd = `npm config set registry ${host}`
|
|
115
|
+
// 检查当前镜像源是否为淘宝源
|
|
116
|
+
const registry = await this.shell('npm config get registry')
|
|
117
|
+
if (registry === host) {
|
|
118
|
+
console.log(`当前npm源已经是 ${host} ~`)
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
if (!await this.shell(cmd)) {
|
|
122
|
+
console.log('更换npm源失败,请手动更换npm源!')
|
|
123
|
+
console.log(`可尝试手动执行 【 npm config set registry ${host} 】 更换镜像源~`)
|
|
124
|
+
} else {
|
|
125
|
+
console.log('更换npm源成功~')
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 安装pnpm或者yarn
|
|
131
|
+
* @param type - 包管理器名称
|
|
132
|
+
*/
|
|
133
|
+
async install (type) {
|
|
134
|
+
/** 检查是否已经安装对应的包管理器 */
|
|
135
|
+
if (!this.shell(`${type} -v`)) {
|
|
136
|
+
console.log(`检测到未安装${type},开始安装...`)
|
|
137
|
+
if (!await this.shell(`npm install -g ${type}`)) {
|
|
138
|
+
console.log(`${type}安装失败,请手动安装${type}!`)
|
|
139
|
+
console.log(`可尝试手动执行 【 npm install -g ${type} 】 安装${type}~`)
|
|
140
|
+
return
|
|
141
|
+
} else {
|
|
142
|
+
console.log(`${type}安装完成~`)
|
|
143
|
+
}
|
|
144
|
+
} else {
|
|
145
|
+
console.log('检测到已安装pnpm,开始安装依赖...')
|
|
146
|
+
}
|
|
147
|
+
/** 安装依赖 */
|
|
148
|
+
if (!this.shell(`${type} install -P`)) {
|
|
149
|
+
console.log('安装依赖失败,请手动安装依赖!')
|
|
150
|
+
console.log(`可尝试手动执行 【 ${type} install -P 】 安装依赖~`)
|
|
151
|
+
console.log('如中国大陆用户安装失败,请尝试执行换源 【 npm config set registry https://registry.npmmirror.com 】后再安装依赖~')
|
|
152
|
+
} else {
|
|
153
|
+
console.log('依赖安装完成~')
|
|
154
|
+
}
|
|
155
|
+
/** 检查安装pm2 */
|
|
156
|
+
if (!this.shell('pm2 -v')) {
|
|
157
|
+
console.log('检测到未安装pm2,开始安装pm2...')
|
|
158
|
+
if (!this.shell(`${type} install -g pm2`)) {
|
|
159
|
+
console.log('安装pm2失败,请手动安装pm2!')
|
|
160
|
+
console.log('可尝试手动执行 【 npm install -g pm2 】 安装pm2~')
|
|
161
|
+
} else {
|
|
162
|
+
console.log('pm2安装完成~')
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
console.log('检测到已安装pm2~')
|
|
166
|
+
}
|
|
167
|
+
console.log('依赖环境初始化完成~')
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 执行命令
|
|
172
|
+
* @param cmd - 命令
|
|
173
|
+
*/
|
|
174
|
+
shell (cmd) {
|
|
175
|
+
return new Promise(resolve => {
|
|
176
|
+
exec(cmd, { env: process.env, cwd: process.cwd() }, (err, stdout, stderr) => {
|
|
177
|
+
if (stdout) { return resolve(stdout.trim()) }
|
|
178
|
+
if (err) {
|
|
179
|
+
console.error(err)
|
|
180
|
+
return resolve(false)
|
|
181
|
+
}
|
|
182
|
+
if (stderr) {
|
|
183
|
+
console.error(stderr)
|
|
184
|
+
return resolve(false)
|
|
185
|
+
}
|
|
186
|
+
})
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 获取当前的包管理器
|
|
192
|
+
*/
|
|
193
|
+
getRegistry () {
|
|
194
|
+
const { npm_config_user_agent: agent, npm_config_userconfig: userconfig } = process.env
|
|
195
|
+
if (agent && agent.includes('pnpm')) {
|
|
196
|
+
return 'pnpm'
|
|
197
|
+
} else if (userconfig && userconfig.includes('cnpm')) {
|
|
198
|
+
return 'cnpm'
|
|
199
|
+
} else if (agent && agent.includes('yarn')) {
|
|
200
|
+
return 'yarn'
|
|
201
|
+
} else {
|
|
202
|
+
return 'npm'
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-karin",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.17",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "基于 Kritor 进行开发的nodejs机器人框架",
|
|
6
6
|
"homepage": "https://github.com/KarinJS/Karin",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"main": "./lib/index.js",
|
|
18
18
|
"types": "./lib/index.d.ts",
|
|
19
19
|
"bin": {
|
|
20
|
-
"init": "./lib/
|
|
20
|
+
"init": "./lib/cli/init.js",
|
|
21
|
+
"log": "./lib/cli/log.js"
|
|
21
22
|
},
|
|
22
23
|
"files": [
|
|
23
24
|
"/lib/**/*.js",
|
|
@@ -38,32 +39,32 @@
|
|
|
38
39
|
"app": "node .",
|
|
39
40
|
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json && npm run cp && npm run fix",
|
|
40
41
|
"cp": "cp ./lib/modules.d.ts ./modules.d.ts && cp ./lib/modules.js ./modules.js",
|
|
42
|
+
"debug": "node . --dev",
|
|
41
43
|
"delete": "pm2 delete ./config/config/pm2.yaml",
|
|
42
44
|
"dev": "tsx ./lib/index.js --dev",
|
|
43
|
-
"debug": "tsx watch ./lib/index.js --dev",
|
|
44
45
|
"fix": "eslint lib/**/*.js --fix",
|
|
45
46
|
"fix:all": "eslint lib/**/*.js --fix && eslint lib/**/*.ts --fix",
|
|
46
|
-
"
|
|
47
|
-
"init
|
|
48
|
-
"
|
|
49
|
-
"log": "node lib/
|
|
47
|
+
"hook": "node lib/cli/hook.js",
|
|
48
|
+
"init": "node lib/cli/install.js",
|
|
49
|
+
"postinstall": "node lib/cli/postinstall.js",
|
|
50
|
+
"log": "node lib/cli/log.js",
|
|
50
51
|
"monit": "pm2 monit",
|
|
51
52
|
"pub": "npm publish --access public",
|
|
52
53
|
"restart": "pm2 restart ./config/config/pm2.yaml",
|
|
53
54
|
"sort:pack": "npx sort-package-json",
|
|
54
55
|
"start": "pm2 start ./config/config/pm2.yaml && pm2 monit",
|
|
55
|
-
"stop": "pm2 delete ./config/config/pm2.yaml"
|
|
56
|
-
"uninstall": "node lib/tools/uninstall.js"
|
|
56
|
+
"stop": "pm2 delete ./config/config/pm2.yaml"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"@grpc/grpc-js": "1.10.10",
|
|
60
60
|
"@grpc/proto-loader": "0.7.13",
|
|
61
|
+
"@inquirer/prompts": "5.1.2",
|
|
61
62
|
"art-template": "4.13.2",
|
|
62
63
|
"axios": "1.7.2",
|
|
63
64
|
"chalk": "5.3.0",
|
|
64
65
|
"chokidar": "3.6.0",
|
|
65
66
|
"express": "4.19.2",
|
|
66
|
-
"kritor-proto": "
|
|
67
|
+
"kritor-proto": "latest",
|
|
67
68
|
"level": "8.0.1",
|
|
68
69
|
"lodash": "4.17.21",
|
|
69
70
|
"log4js": "6.9.1",
|
package/lib/tools/init.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import fs from 'fs'
|
|
3
|
-
import path from 'path'
|
|
4
|
-
import { karinDir } from '../core/dir.js'
|
|
5
|
-
import { execSync } from 'child_process'
|
|
6
|
-
const pathList = ['./plugins', './config/config', './lib']
|
|
7
|
-
// 创建目录
|
|
8
|
-
pathList.forEach(dir => mkdir(dir))
|
|
9
|
-
function mkdir (dirname) {
|
|
10
|
-
if (fs.existsSync(dirname)) { return true }
|
|
11
|
-
if (mkdir(path.dirname(dirname))) { fs.mkdirSync(dirname) }
|
|
12
|
-
return true
|
|
13
|
-
}
|
|
14
|
-
// 创建 lib 目录并导出 node-karin 模块
|
|
15
|
-
fs.writeFileSync('./lib/index.js', `export * from 'node-karin'`)
|
|
16
|
-
// 删除指定目录并重新创建
|
|
17
|
-
const delList = ['./config/defSet', './config/view']
|
|
18
|
-
delList.forEach(dir => {
|
|
19
|
-
if (fs.existsSync(dir)) { fs.rmdirSync(dir, { recursive: true }) }
|
|
20
|
-
mkdir(dir)
|
|
21
|
-
const root = path.join(karinDir, dir.replace('.', ''))
|
|
22
|
-
const files = fs.readdirSync(root).filter(file => file.endsWith('.yaml'))
|
|
23
|
-
files.forEach(file => {
|
|
24
|
-
const destPath = path.join(dir, file)
|
|
25
|
-
const srcPath = path.join(root, file)
|
|
26
|
-
if (!fs.existsSync(destPath)) { fs.copyFileSync(srcPath, destPath) }
|
|
27
|
-
})
|
|
28
|
-
})
|
|
29
|
-
// 修改 package.json 为 ESM 模块
|
|
30
|
-
const pack = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
|
|
31
|
-
const npmPack = JSON.parse(fs.readFileSync(path.join(karinDir, 'package.json'), 'utf-8'))
|
|
32
|
-
npmPack.main = './node_modules/node-karin/lib/index.js'
|
|
33
|
-
delete npmPack.bin
|
|
34
|
-
delete npmPack.types
|
|
35
|
-
npmPack.dependencies['node-karin'] = pack.dependencies['node-karin']
|
|
36
|
-
fs.writeFileSync('./package.json', JSON.stringify(npmPack, null, 2))
|
|
37
|
-
// 复制 pnpm-workspace.yaml 到根目录
|
|
38
|
-
fs.copyFileSync(path.join(karinDir, 'pnpm-workspace.yaml'), './pnpm-workspace.yaml')
|
|
39
|
-
// 检查是否安装 pnpm 并安装依赖
|
|
40
|
-
console.log('检查是否安装 pnpm...')
|
|
41
|
-
try {
|
|
42
|
-
const isPnpm = execSync('pnpm -v', { env: process.env }).toString().trim()
|
|
43
|
-
if (isPnpm) {
|
|
44
|
-
console.log('检查到 pnpm,开始安装依赖...')
|
|
45
|
-
} else {
|
|
46
|
-
installPnpm()
|
|
47
|
-
}
|
|
48
|
-
} catch {
|
|
49
|
-
installPnpm()
|
|
50
|
-
}
|
|
51
|
-
// 安装依赖
|
|
52
|
-
try {
|
|
53
|
-
execSync('pnpm install -P', { env: process.env, stdio: 'inherit' })
|
|
54
|
-
console.log('依赖安装完成~')
|
|
55
|
-
} catch {
|
|
56
|
-
console.log('安装依赖失败,请手动安装依赖!')
|
|
57
|
-
console.log('可尝试手动执行 【 pnpm install -P 】 安装依赖~')
|
|
58
|
-
console.log('如中国大陆用户安装失败,请尝试执行换源 【 npm config set registry https://registry.npmmirror.com 】后再安装依赖~')
|
|
59
|
-
}
|
|
60
|
-
console.log('初始化完成~,请通过 【 node . 】 启动程序~')
|
|
61
|
-
function installPnpm () {
|
|
62
|
-
console.log('未检查到 pnpm,开始安装 pnpm...')
|
|
63
|
-
execSync('npm --registry=https://registry.npmmirror.com install pnpm -g', { env: process.env, stdio: 'inherit' })
|
|
64
|
-
console.log('pnpm 安装完成,开始安装依赖...')
|
|
65
|
-
}
|
|
File without changes
|