node-karin 0.11.13 → 0.11.15

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.
Files changed (61) hide show
  1. package/config/defSet/config.yaml +3 -3
  2. package/config/view/config.yaml +5 -5
  3. package/lib/adapter/index.js +2 -2
  4. package/lib/cli/index.d.ts +7 -3
  5. package/lib/cli/index.js +258 -233
  6. package/lib/cli/init.js +15 -14
  7. package/lib/cli/karin.js +15 -15
  8. package/lib/cli/pkg.d.ts +4 -0
  9. package/lib/cli/pkg.js +14 -0
  10. package/lib/cli/start.js +8 -0
  11. package/lib/core/index.js +10 -10
  12. package/lib/core/init/config.d.ts +0 -4
  13. package/lib/core/init/config.js +0 -13
  14. package/lib/core/init/init.js +1 -0
  15. package/lib/core/listener/listener.js +1 -1
  16. package/lib/core/process/process.js +2 -6
  17. package/lib/core/server/server.js +13 -1
  18. package/lib/db/index.js +2 -2
  19. package/lib/db/level/level.js +0 -1
  20. package/lib/db/redis/redis_level.d.ts +2 -0
  21. package/lib/db/redis/redis_level.js +12 -11
  22. package/lib/event/index.js +5 -5
  23. package/lib/modules/art-template.js +1 -1
  24. package/lib/modules/axios.js +2 -2
  25. package/lib/modules/chalk.js +2 -2
  26. package/lib/modules/chokidar.js +2 -2
  27. package/lib/modules/commander.js +2 -2
  28. package/lib/modules/express.js +3 -3
  29. package/lib/modules/level.js +2 -2
  30. package/lib/modules/lodash.js +1 -1
  31. package/lib/modules/log4js.js +2 -2
  32. package/lib/modules/moment.js +1 -1
  33. package/lib/modules/node-schedule.js +2 -2
  34. package/lib/modules/redis.js +2 -2
  35. package/lib/modules/ws.js +3 -3
  36. package/lib/modules/yaml.js +2 -2
  37. package/lib/render/app.js +82 -81
  38. package/lib/render/base.js +54 -54
  39. package/lib/render/client.js +144 -142
  40. package/lib/render/client_even.js +140 -137
  41. package/lib/render/http.js +40 -41
  42. package/lib/render/index.js +6 -6
  43. package/lib/render/server.js +93 -88
  44. package/lib/render/wormhole.js +153 -151
  45. package/lib/types/config/config.d.ts +2 -2
  46. package/lib/types/index.js +13 -13
  47. package/lib/types/type/global.d.ts +2 -0
  48. package/lib/utils/index.d.ts +2 -0
  49. package/lib/utils/index.js +13 -11
  50. package/lib/utils/tools/exec.d.ts +5 -17
  51. package/lib/utils/tools/exec.js +28 -26
  52. package/lib/utils/tools/ffmpeg.d.ts +2 -2
  53. package/lib/utils/tools/restart.d.ts +15 -0
  54. package/lib/utils/tools/restart.js +39 -0
  55. package/lib/utils/tools/stop.d.ts +7 -0
  56. package/lib/utils/tools/stop.js +13 -0
  57. package/lib/utils/tools/update.d.ts +26 -84
  58. package/lib/utils/tools/update.js +40 -28
  59. package/package.json +6 -3
  60. package/lib/cli/dev.js +0 -3
  61. /package/lib/cli/{dev.d.ts → start.d.ts} +0 -0
@@ -1,75 +1,22 @@
1
- import fs from 'fs';
2
- export declare const update: {
1
+ export declare class Updates {
3
2
  dir: string;
3
+ constructor();
4
4
  /**
5
- * 更新框架或插件
6
- * @param path - 插件相对路径
7
- * @param cmd - 更新命令 默认git pull
8
- * @param time - 超时时间 默认120s
9
- */
10
- update(path: string, cmd?: string, time?: number): Promise<{
11
- status: string;
12
- data: string;
13
- }>;
14
- /**
15
- * 获取指定仓库最后一次提交时间日期
16
- * @param path - 插件相对路径
17
- */
18
- getTime(path: string): Promise<string>;
19
- /**
20
- * 获取指定仓库最后一次提交哈希值
21
- * @param {string} path - 插件相对路径
22
- * @param {boolean} [short] - 是否获取短哈希 默认true
23
- * @returns {Promise<string>}
24
- */
25
- getHash(path: string, short?: boolean): Promise<string>;
26
- /**
27
- * 获取指定仓库的提交记录
28
- * @param {{
29
- * path: string,
30
- * count?: number,
31
- * hash?: string
32
- * }} options - 参数
33
- * @param {string} options.path - 插件相对路径
34
- * @param {number} [options.count] - 获取日志条数 默认1条
35
- * @param {string} [options.hash] - 指定HEAD
36
- * @returns {Promise<string>}
5
+ * 更新npm包
6
+ * @param name - 包名
7
+ * @param timeout - 超时时间 默认120s
37
8
  */
38
- getCommit(options: {
39
- path: any;
40
- count?: any;
41
- hash?: any;
42
- branch?: any;
43
- }): Promise<string>;
44
- /**
45
- * 检查插件是否有更新
46
- * @param {string} path - 插件相对路径
47
- * @param {number} [time] - 超时时间 默认120s
48
- * @returns {Promise<{status: 'ok'|'failed', data: string|boolean}>}
49
- */
50
- checkUpdate(path: fs.PathLike, time?: number): Promise<{
51
- status: string;
52
- data: string | undefined;
53
- count?: undefined;
54
- } | {
55
- status: string;
56
- data: boolean;
57
- count?: undefined;
58
- } | {
59
- status: string;
9
+ updatePkg(name: string, timeout?: number): Promise<{
10
+ status: boolean;
60
11
  data: string;
61
- count: string | number;
62
12
  }>;
63
- };
64
- export declare const Update: {
65
- dir: string;
66
13
  /**
67
14
  * 更新框架或插件
68
15
  * @param path - 插件相对路径
69
16
  * @param cmd - 更新命令 默认git pull
70
- * @param time - 超时时间 默认120s
17
+ * @param timeout - 超时时间 默认120s
71
18
  */
72
- update(path: string, cmd?: string, time?: number): Promise<{
19
+ update(path: string, cmd?: string, timeout?: number): Promise<{
73
20
  status: string;
74
21
  data: string;
75
22
  }>;
@@ -80,36 +27,29 @@ export declare const Update: {
80
27
  getTime(path: string): Promise<string>;
81
28
  /**
82
29
  * 获取指定仓库最后一次提交哈希值
83
- * @param {string} path - 插件相对路径
84
- * @param {boolean} [short] - 是否获取短哈希 默认true
85
- * @returns {Promise<string>}
30
+ * @param path - 插件相对路径
31
+ * @param short - 是否获取短哈希 默认true
86
32
  */
87
33
  getHash(path: string, short?: boolean): Promise<string>;
88
34
  /**
89
35
  * 获取指定仓库的提交记录
90
- * @param {{
91
- * path: string,
92
- * count?: number,
93
- * hash?: string
94
- * }} options - 参数
95
- * @param {string} options.path - 插件相对路径
96
- * @param {number} [options.count] - 获取日志条数 默认1条
97
- * @param {string} [options.hash] - 指定HEAD
98
- * @returns {Promise<string>}
99
36
  */
100
37
  getCommit(options: {
101
- path: any;
102
- count?: any;
103
- hash?: any;
104
- branch?: any;
38
+ /** 指令命令路径 */
39
+ path: string;
40
+ /** 获取几次提交 默认1次 */
41
+ count?: number;
42
+ /** 指定哈希 */
43
+ hash?: string;
44
+ /** 指定分支 */
45
+ branch?: string;
105
46
  }): Promise<string>;
106
47
  /**
107
48
  * 检查插件是否有更新
108
- * @param {string} path - 插件相对路径
109
- * @param {number} [time] - 超时时间 默认120s
110
- * @returns {Promise<{status: 'ok'|'failed', data: string|boolean}>}
49
+ * @param path - 插件相对路径
50
+ * @param time - 超时时间 默认120s
111
51
  */
112
- checkUpdate(path: fs.PathLike, time?: number): Promise<{
52
+ checkUpdate(path: string, time?: number): Promise<{
113
53
  status: string;
114
54
  data: string | undefined;
115
55
  count?: undefined;
@@ -120,6 +60,8 @@ export declare const Update: {
120
60
  } | {
121
61
  status: string;
122
62
  data: string;
123
- count: string | number;
63
+ count: number;
124
64
  }>;
125
- };
65
+ }
66
+ export declare const update: Updates;
67
+ export declare const Update: Updates;
@@ -1,17 +1,39 @@
1
1
  import fs from 'fs';
2
2
  import exec from './exec.js';
3
- export const update = new (class Update {
3
+ import { KarinCli } from '../../cli/index.js';
4
+ import { getRegistry } from '../../cli/pkg.js';
5
+ export class Updates {
4
6
  dir;
5
7
  constructor() {
6
8
  this.dir = './plugins';
7
9
  }
10
+ /**
11
+ * 更新npm包
12
+ * @param name - 包名
13
+ * @param timeout - 超时时间 默认120s
14
+ */
15
+ async updatePkg(name, timeout = 120) {
16
+ const pkg = getRegistry();
17
+ const cmd = (pkg === 'yarn' ? 'yarn upgrade' : `${pkg} update`) + `${name}@latest`;
18
+ /** 检查是否已经是最新版本 */
19
+ const tools = new KarinCli();
20
+ const local = await tools.getLocalVersion(name, pkg);
21
+ const remote = await tools.getRemoteVersion(name, pkg);
22
+ logger.info(`[依赖更新] ${name} 当前版本: ${local} 最新版本: ${remote}`);
23
+ if (local === remote)
24
+ return { status: true, data: `${name} 已经是最新版本~` };
25
+ const npm = await exec(cmd, true, { cwd: process.cwd(), timeout: timeout * 1000 });
26
+ if (npm.status === 'ok')
27
+ return { status: true, data: `${name} 更新完成~` };
28
+ return { status: false, data: `${name} 更新失败: ${npm.error?.message}` };
29
+ }
8
30
  /**
9
31
  * 更新框架或插件
10
32
  * @param path - 插件相对路径
11
33
  * @param cmd - 更新命令 默认git pull
12
- * @param time - 超时时间 默认120s
34
+ * @param timeout - 超时时间 默认120s
13
35
  */
14
- async update(path, cmd = 'git pull', time = 120) {
36
+ async update(path, cmd = 'git pull', timeout = 120) {
15
37
  /** 检查一下路径是否存在 */
16
38
  if (!fs.existsSync(path))
17
39
  return { status: 'failed', data: '路径不存在' };
@@ -21,7 +43,7 @@ export const update = new (class Update {
21
43
  /** 设置超时时间 */
22
44
  const timer = setTimeout(() => {
23
45
  return { status: 'failed', data: '执行超时' };
24
- }, time * 1000);
46
+ }, timeout * 1000);
25
47
  const options = { env: process.env, cwd: path, encoding: 'utf-8' };
26
48
  /** 记录当前短哈希 */
27
49
  const hash = await this.getHash(path);
@@ -36,17 +58,17 @@ export const update = new (class Update {
36
58
  const time = await this.getTime(path);
37
59
  return {
38
60
  status: 'ok',
39
- data: ['\n当前版本已是最新版本', `Hash: ${hash}`, `最后更新:${await this.getCommit({ path, count: 1 })}`, `最后提交时间:${time}`].join('\n'),
61
+ data: `\n当前版本已是最新版本\n最后更新时间:${time}\n更新详情:${await this.getCommit({ path, count: 1 })}`,
40
62
  };
41
63
  }
42
64
  const Commit = await this.getCommit({ path, hash });
43
65
  return {
44
66
  status: 'ok',
45
- data: ['\n更新成功', `当前Hash: ${newHash}`, `更新日志:\n${Commit}`].join('\n'),
67
+ data: `\n更新成功\n更新日志:\n${Commit}`,
46
68
  };
47
69
  }
48
- const msg = ['\n更新失败', `当前Hash: ${hash}`, `错误信息:${error?.message?.toString() || error?.stack?.toString() || error?.toString()}`, '请解决错误后重试或执行【#强制更新】'];
49
- return { status: 'failed', data: msg.join('\n') };
70
+ const data = `\n更新失败\n错误信息:${error?.stack?.toString() || error?.message?.toString() || error?.toString() || error}\n请解决错误后重试或执行【#强制更新】`;
71
+ return { status: 'failed', data };
50
72
  }
51
73
  /**
52
74
  * 获取指定仓库最后一次提交时间日期
@@ -54,20 +76,19 @@ export const update = new (class Update {
54
76
  */
55
77
  async getTime(path) {
56
78
  const cmd = 'git log -1 --format=%cd --date=format:"%Y-%m-%d %H:%M:%S"';
57
- const data = await exec(cmd, false, { cwd: path, encoding: 'utf-8' });
79
+ const data = await exec(cmd, false, { cwd: path });
58
80
  if (data.status === 'failed')
59
81
  return '获取时间失败,请重试或更新Git~';
60
82
  return data.stdout.trim();
61
83
  }
62
84
  /**
63
85
  * 获取指定仓库最后一次提交哈希值
64
- * @param {string} path - 插件相对路径
65
- * @param {boolean} [short] - 是否获取短哈希 默认true
66
- * @returns {Promise<string>}
86
+ * @param path - 插件相对路径
87
+ * @param short - 是否获取短哈希 默认true
67
88
  */
68
89
  async getHash(path, short = true) {
69
90
  const cmd = short ? 'git rev-parse --short HEAD' : 'git rev-parse HEAD';
70
- const data = await exec(cmd, false, { cwd: path, encoding: 'utf-8' });
91
+ const data = await exec(cmd, false, { cwd: path });
71
92
  if (data.status === 'failed') {
72
93
  const text = data.error;
73
94
  throw new Error(text);
@@ -76,15 +97,6 @@ export const update = new (class Update {
76
97
  }
77
98
  /**
78
99
  * 获取指定仓库的提交记录
79
- * @param {{
80
- * path: string,
81
- * count?: number,
82
- * hash?: string
83
- * }} options - 参数
84
- * @param {string} options.path - 插件相对路径
85
- * @param {number} [options.count] - 获取日志条数 默认1条
86
- * @param {string} [options.hash] - 指定HEAD
87
- * @returns {Promise<string>}
88
100
  */
89
101
  async getCommit(options) {
90
102
  const { path, count = 1, hash, branch } = options;
@@ -95,7 +107,7 @@ export const update = new (class Update {
95
107
  /** 指定分支 */
96
108
  if (branch)
97
109
  cmd = `git log -${count} ${branch} --format="[%ad] %s %n" --date="format:%m-%d %H:%M"`;
98
- const data = await exec(cmd, false, { cwd: path, encoding: 'utf-8' });
110
+ const data = await exec(cmd, false, { cwd: path });
99
111
  if (data.status === 'failed') {
100
112
  const text = data.error;
101
113
  throw new Error(text);
@@ -104,9 +116,8 @@ export const update = new (class Update {
104
116
  }
105
117
  /**
106
118
  * 检查插件是否有更新
107
- * @param {string} path - 插件相对路径
108
- * @param {number} [time] - 超时时间 默认120s
109
- * @returns {Promise<{status: 'ok'|'failed', data: string|boolean}>}
119
+ * @param path - 插件相对路径
120
+ * @param time - 超时时间 默认120s
110
121
  */
111
122
  async checkUpdate(path, time = 120) {
112
123
  /** 检查一下路径是否存在 */
@@ -131,9 +142,10 @@ export const update = new (class Update {
131
142
  if (stdout.includes('Your branch is up to date with'))
132
143
  return { status: 'ok', data: false };
133
144
  /** 获取落后几次更新 */
134
- const count = stdout.match(/Your branch is behind '.*' by (\d+) commits/)?.[1] || 1;
145
+ const count = Number(stdout.match(/Your branch is behind '.*' by (\d+) commits/)?.[1]) || 1;
135
146
  const data = await this.getCommit({ path, count, branch: 'origin' });
136
147
  return { status: 'ok', data, count };
137
148
  }
138
- })();
149
+ }
150
+ export const update = new Updates();
139
151
  export const Update = update;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "0.11.13",
3
+ "version": "0.11.15",
4
4
  "private": false,
5
5
  "description": "基于 Kritor 进行开发的nodejs机器人框架",
6
6
  "homepage": "https://github.com/KarinJS/Karin",
@@ -132,12 +132,15 @@
132
132
  "plugins/**"
133
133
  ],
134
134
  "scripts": {
135
- "build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json && npm run fix",
135
+ ".": "node lib/cli/start.js .",
136
+ "build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
137
+ "build:npm": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
138
+ "dev": "node lib/cli/start.js dev",
136
139
  "fix": "eslint lib/**/*.js --fix",
137
140
  "fix:all": "eslint lib/**/*.js --fix && eslint lib/**/*.d.ts --fix",
138
141
  "pub": "npm publish --access public",
139
142
  "sort": "npx sort-package-json && sort-json tsconfig.json",
140
- "dev": "node lib/cli/dev.js"
143
+ "start": "node lib/cli/start.js start"
141
144
  },
142
145
  "dependencies": {
143
146
  "art-template": "4.13.2",
package/lib/cli/dev.js DELETED
@@ -1,3 +0,0 @@
1
- import { KarinCli } from './index.js'
2
- const karin = new KarinCli()
3
- karin.start('dev' /* Mode.Dev */, 'js' /* Lang.Js */, 'node' /* Runner.Node */)
File without changes