node-karin 0.11.6 → 0.11.9
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/cli/dev.d.ts +1 -0
- package/lib/cli/dev.js +3 -0
- package/lib/cli/index.d.ts +88 -0
- package/lib/cli/index.js +282 -0
- package/lib/cli/karin.d.ts +1 -13
- package/lib/cli/karin.js +1 -282
- package/lib/types/type/global.d.ts +1 -1
- package/package.json +4 -17
package/lib/cli/dev.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/cli/dev.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ChildProcess } from 'child_process';
|
|
2
|
+
export declare const enum Runner {
|
|
3
|
+
Node = "node",
|
|
4
|
+
Tsx = "tsx",
|
|
5
|
+
Pm2 = "pm2"
|
|
6
|
+
}
|
|
7
|
+
export declare const enum Mode {
|
|
8
|
+
Dev = "dev",
|
|
9
|
+
Prod = "prod"
|
|
10
|
+
}
|
|
11
|
+
export declare const enum Lang {
|
|
12
|
+
Js = "js",
|
|
13
|
+
Ts = "ts"
|
|
14
|
+
}
|
|
15
|
+
export declare class KarinCli {
|
|
16
|
+
child: ChildProcess;
|
|
17
|
+
filename: string;
|
|
18
|
+
karinDir: string;
|
|
19
|
+
file: string;
|
|
20
|
+
constructor();
|
|
21
|
+
/**
|
|
22
|
+
* 获取pkg
|
|
23
|
+
* @param isNpm - 是否是npm包
|
|
24
|
+
*/
|
|
25
|
+
pkg(isNpm: boolean): any;
|
|
26
|
+
/**
|
|
27
|
+
* 获取配置文件路径
|
|
28
|
+
* @param name - 配置文件名
|
|
29
|
+
*/
|
|
30
|
+
getConfigPath(name: 'pm2' | 'server'): string;
|
|
31
|
+
/**
|
|
32
|
+
* 获取pm2配置
|
|
33
|
+
* @param name - 配置文件名
|
|
34
|
+
*/
|
|
35
|
+
getConfigData(name: 'pm2' | 'server'): any;
|
|
36
|
+
/**
|
|
37
|
+
* 启动
|
|
38
|
+
* @param mode - 模式
|
|
39
|
+
* @param lang - 语言环境
|
|
40
|
+
* @param runner - 运行器
|
|
41
|
+
*/
|
|
42
|
+
start(mode: Mode, lang: Lang, runner: Runner): void;
|
|
43
|
+
/**
|
|
44
|
+
* pm2重启
|
|
45
|
+
*/
|
|
46
|
+
restart(): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* pm2启动
|
|
49
|
+
*/
|
|
50
|
+
pm2(): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* pm2结束进程
|
|
53
|
+
*/
|
|
54
|
+
stop(): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* pm2查看日志
|
|
57
|
+
*/
|
|
58
|
+
log(): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* 更新依赖
|
|
61
|
+
*/
|
|
62
|
+
update(): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* 获取指定包的本地版本
|
|
65
|
+
* @param name - 包名
|
|
66
|
+
* @param pkg - 包管理器
|
|
67
|
+
* @returns - 版本号
|
|
68
|
+
*/
|
|
69
|
+
getLocalVersion(name: string, pkg: 'pnpm' | 'cnpm' | 'yarn' | 'npm'): Promise<string>;
|
|
70
|
+
/**
|
|
71
|
+
* 获取指定包的最新版本
|
|
72
|
+
* @param name - 包名
|
|
73
|
+
* @param pkg - 包管理器
|
|
74
|
+
*/
|
|
75
|
+
getRemoteVersion(name: string, pkg: 'pnpm' | 'cnpm' | 'yarn' | 'npm'): Promise<string>;
|
|
76
|
+
/**
|
|
77
|
+
* 封装exec
|
|
78
|
+
* @param cmd - 命令
|
|
79
|
+
*/
|
|
80
|
+
exec(cmd: string): Promise<string>;
|
|
81
|
+
/**
|
|
82
|
+
* 封装axios 超时返回false
|
|
83
|
+
* @param url - 请求地址
|
|
84
|
+
* @param timeout - 超时时间
|
|
85
|
+
* @returns - 请求结果
|
|
86
|
+
*/
|
|
87
|
+
Axios(url: string, timeout: number): Promise<any>;
|
|
88
|
+
}
|
package/lib/cli/index.js
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import yaml from 'yaml'
|
|
4
|
+
import axios from 'axios'
|
|
5
|
+
import { fileURLToPath } from 'url'
|
|
6
|
+
import { exec as execCmd, spawn } from 'child_process'
|
|
7
|
+
import { KarinCfgInit } from '../core/init/config.js'
|
|
8
|
+
export class KarinCli {
|
|
9
|
+
child
|
|
10
|
+
filename
|
|
11
|
+
karinDir
|
|
12
|
+
file
|
|
13
|
+
constructor () {
|
|
14
|
+
process.env.karin_app_start_count = '0'
|
|
15
|
+
process.env.karin_app_watch = 'no'
|
|
16
|
+
/** 当前文件绝对路径 */
|
|
17
|
+
this.filename = fileURLToPath(import.meta.url)
|
|
18
|
+
/** karin目录 */
|
|
19
|
+
this.karinDir = path.join(path.dirname(this.filename), '../..')
|
|
20
|
+
/** 入口文件(注意后缀) */
|
|
21
|
+
this.file = path.join(path.dirname(this.filename), '../index.js')
|
|
22
|
+
this.child = null
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 获取pkg
|
|
27
|
+
* @param isNpm - 是否是npm包
|
|
28
|
+
*/
|
|
29
|
+
pkg (isNpm) {
|
|
30
|
+
const filePath = isNpm ? path.join(this.karinDir, 'package.json') : path.join(process.cwd(), 'package.json')
|
|
31
|
+
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
|
|
32
|
+
return data
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 获取配置文件路径
|
|
37
|
+
* @param name - 配置文件名
|
|
38
|
+
*/
|
|
39
|
+
getConfigPath (name) {
|
|
40
|
+
const filePath = `./config/config/${name}.yaml`
|
|
41
|
+
if (!fs.existsSync(filePath)) { return `${this.karinDir}/config/config/${name}.yaml` }
|
|
42
|
+
return filePath
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 获取pm2配置
|
|
47
|
+
* @param name - 配置文件名
|
|
48
|
+
*/
|
|
49
|
+
getConfigData (name) {
|
|
50
|
+
const _path = this.getConfigPath(name)
|
|
51
|
+
const data = yaml.parse(fs.readFileSync(_path, 'utf-8'))
|
|
52
|
+
return data
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 启动
|
|
57
|
+
* @param mode - 模式
|
|
58
|
+
* @param lang - 语言环境
|
|
59
|
+
* @param runner - 运行器
|
|
60
|
+
*/
|
|
61
|
+
start (mode, lang, runner) {
|
|
62
|
+
process.env.karin_app_mode = mode
|
|
63
|
+
process.env.karin_app_lang = lang
|
|
64
|
+
process.env.karin_app_runner = runner
|
|
65
|
+
let cmd
|
|
66
|
+
switch (runner) {
|
|
67
|
+
case 'node' /* Runner.Node */:
|
|
68
|
+
cmd = [this.file]
|
|
69
|
+
break
|
|
70
|
+
case 'tsx' /* Runner.Tsx */:
|
|
71
|
+
cmd = [this.file]
|
|
72
|
+
break
|
|
73
|
+
case 'pm2' /* Runner.Pm2 */: {
|
|
74
|
+
this.pm2()
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** 启动 */
|
|
79
|
+
this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env, shell: runner === 'tsx' /* Runner.Tsx */ })
|
|
80
|
+
/** 监听退出 */
|
|
81
|
+
this.child.once('exit', (code) => process.exit(code))
|
|
82
|
+
/** 监听子进程消息 */
|
|
83
|
+
this.child.on('message', (data) => {
|
|
84
|
+
/** pm2重启 */
|
|
85
|
+
if (data.env.pm_id) { return this.restart() }
|
|
86
|
+
try {
|
|
87
|
+
/** 先结束进程 */
|
|
88
|
+
this.child.kill('SIGINT')
|
|
89
|
+
/** 停止监听 */
|
|
90
|
+
this.child.removeAllListeners()
|
|
91
|
+
/** 重启次数+1 */
|
|
92
|
+
const count = Number(process.env.karin_app_start_count) || 0
|
|
93
|
+
process.env.karin_app_start_count = String(count + 1)
|
|
94
|
+
} catch { }
|
|
95
|
+
return this.start(mode, lang, runner)
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* pm2重启
|
|
101
|
+
*/
|
|
102
|
+
async restart () {
|
|
103
|
+
const pm2Cfg = this.getConfigData('pm2')
|
|
104
|
+
const serverCfg = this.getConfigData('server')
|
|
105
|
+
/** 尝试获取pm2的进程id */
|
|
106
|
+
const port = serverCfg?.http?.port || 7000
|
|
107
|
+
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000)
|
|
108
|
+
if (res) {
|
|
109
|
+
await this.exec(`pm2 restart ${res.pm2_id}`)
|
|
110
|
+
} else {
|
|
111
|
+
await this.exec(`pm2 restart ${pm2Cfg?.apps[0]?.name || 'Karin'}`)
|
|
112
|
+
}
|
|
113
|
+
console.log('pm2服务已重启')
|
|
114
|
+
process.exit(0)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* pm2启动
|
|
119
|
+
*/
|
|
120
|
+
async pm2 () {
|
|
121
|
+
console.log('pm2启动中...')
|
|
122
|
+
const filePath = this.getConfigPath('pm2')
|
|
123
|
+
const data = this.getConfigData('pm2')
|
|
124
|
+
/** 修正入口文件路径 兼容0.6.28以前的版本 */
|
|
125
|
+
if (!fs.existsSync('./src') && filePath === './config/config/pm2.yaml') {
|
|
126
|
+
const script = './node_modules/node-karin/lib/index.js'
|
|
127
|
+
if (data.apps[0].script !== script) {
|
|
128
|
+
data.apps[0].script = script
|
|
129
|
+
fs.writeFileSync(filePath, yaml.stringify(data))
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const cmd = `pm2 start ${filePath} --env ${JSON.stringify(process.env)}`
|
|
133
|
+
await this.exec(cmd)
|
|
134
|
+
console.log('pm2服务已启动 可执行 【npx karin log】 查看日志')
|
|
135
|
+
process.exit(0)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* pm2结束进程
|
|
140
|
+
*/
|
|
141
|
+
async stop () {
|
|
142
|
+
console.log('pm2服务停止中...')
|
|
143
|
+
const pm2Cfg = this.getConfigData('pm2')
|
|
144
|
+
const serverCfg = this.getConfigData('server')
|
|
145
|
+
const port = serverCfg?.http?.port || 7000
|
|
146
|
+
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000)
|
|
147
|
+
if (res) {
|
|
148
|
+
await this.exec(`pm2 delete ${res.pm2_id}`)
|
|
149
|
+
} else {
|
|
150
|
+
await this.exec(`pm2 delete ${pm2Cfg?.apps[0]?.name || 'Karin'}`)
|
|
151
|
+
}
|
|
152
|
+
console.log('pm2服务已停止')
|
|
153
|
+
process.exit(0)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* pm2查看日志
|
|
158
|
+
*/
|
|
159
|
+
async log () {
|
|
160
|
+
const pm2Cfg = this.getConfigData('pm2')
|
|
161
|
+
const serverCfg = this.getConfigData('server')
|
|
162
|
+
const port = serverCfg?.http?.port || 7000
|
|
163
|
+
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000)
|
|
164
|
+
const lines = pm2Cfg?.lines || 1000
|
|
165
|
+
const cmd = process.platform === 'win32' ? 'pm2.cmd' : 'pm2'
|
|
166
|
+
const type = res ? res.pm_id : pm2Cfg?.apps[0]?.name || 'Karin'
|
|
167
|
+
spawn(cmd, ['logs', type, '--lines', lines], { stdio: 'inherit', shell: true, cwd: process.cwd() })
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 更新依赖
|
|
172
|
+
*/
|
|
173
|
+
async update () {
|
|
174
|
+
/** 屏蔽的依赖包列表 */
|
|
175
|
+
const pkgdependencies = [
|
|
176
|
+
'art-template',
|
|
177
|
+
'axios',
|
|
178
|
+
'chalk',
|
|
179
|
+
'chokidar',
|
|
180
|
+
'commander',
|
|
181
|
+
'express',
|
|
182
|
+
'level',
|
|
183
|
+
'lodash',
|
|
184
|
+
'log4js',
|
|
185
|
+
'moment',
|
|
186
|
+
'node-schedule',
|
|
187
|
+
'redis',
|
|
188
|
+
'ws',
|
|
189
|
+
'yaml',
|
|
190
|
+
]
|
|
191
|
+
const list = Object.keys(this.pkg(false).dependencies).filter(key => !pkgdependencies.includes(key))
|
|
192
|
+
/** 获取包管理器 */
|
|
193
|
+
const pkg = new KarinCfgInit().getRegistry()
|
|
194
|
+
const cmd = pkg === 'yarn' ? 'yarn upgrade' : `${pkg} update`
|
|
195
|
+
/** 异步并发更新依赖 */
|
|
196
|
+
await Promise.all(list.map(async (item) => {
|
|
197
|
+
try {
|
|
198
|
+
/** 检查是否已经是最新版本 */
|
|
199
|
+
const local = await this.getLocalVersion(item, pkg)
|
|
200
|
+
const remote = await this.getRemoteVersion(item, pkg)
|
|
201
|
+
if (local === remote) {
|
|
202
|
+
console.log(`[依赖更新] ${item} 已经是最新~`)
|
|
203
|
+
return
|
|
204
|
+
}
|
|
205
|
+
console.log(`[依赖更新] ${item} 当前版本: ${local} 最新版本: ${remote}`)
|
|
206
|
+
await this.exec(`${cmd} ${item}@latest`)
|
|
207
|
+
console.log(`[依赖更新] ${item} 更新完成~`)
|
|
208
|
+
} catch (error) {
|
|
209
|
+
console.error(`[依赖更新] ${item} 更新失败:`)
|
|
210
|
+
console.error(`error.stack: ${error.stack}`)
|
|
211
|
+
console.error(`error.message: ${error.message}`)
|
|
212
|
+
}
|
|
213
|
+
}))
|
|
214
|
+
console.log('所有依赖已更新完成~')
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 获取指定包的本地版本
|
|
219
|
+
* @param name - 包名
|
|
220
|
+
* @param pkg - 包管理器
|
|
221
|
+
* @returns - 版本号
|
|
222
|
+
*/
|
|
223
|
+
async getLocalVersion (name, pkg) {
|
|
224
|
+
const cmd = pkg === 'yarn' ? `yarn list --pattern ${name}` : `${pkg} list ${name} --depth=0`
|
|
225
|
+
const text = await this.exec(cmd)
|
|
226
|
+
/** pnpm特殊处理 */
|
|
227
|
+
if (pkg === 'pnpm') {
|
|
228
|
+
const reg = new RegExp(`${name}\\s+([\\d.]+)`, 'gm')
|
|
229
|
+
const res = reg.exec(text)
|
|
230
|
+
return res?.[1] || '0.0.0'
|
|
231
|
+
}
|
|
232
|
+
const reg = new RegExp(`${name}@(\\d+\\.\\d+\\.\\d+)`, 'gm')
|
|
233
|
+
const res = reg.exec(text)
|
|
234
|
+
return res?.[1] || '0.0.0'
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* 获取指定包的最新版本
|
|
239
|
+
* @param name - 包名
|
|
240
|
+
* @param pkg - 包管理器
|
|
241
|
+
*/
|
|
242
|
+
async getRemoteVersion (name, pkg) {
|
|
243
|
+
const cmd = `${pkg} info ${name} version`
|
|
244
|
+
const text = await this.exec(cmd)
|
|
245
|
+
/** yarn特殊处理 */
|
|
246
|
+
if (pkg === 'yarn') {
|
|
247
|
+
const lines = text.split('\n').map(line => line.trim())
|
|
248
|
+
const ver = lines.find(line => /^\d+\.\d+\.\d+$/.test(line))
|
|
249
|
+
return ver || ''
|
|
250
|
+
}
|
|
251
|
+
return text.trim()
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* 封装exec
|
|
256
|
+
* @param cmd - 命令
|
|
257
|
+
*/
|
|
258
|
+
exec (cmd) {
|
|
259
|
+
return new Promise((resolve, reject) => {
|
|
260
|
+
execCmd(cmd, (error, stdout, stderr) => {
|
|
261
|
+
if (stdout) { return resolve(stdout.trim()) }
|
|
262
|
+
if (error) { return reject(error) }
|
|
263
|
+
return reject(stderr)
|
|
264
|
+
})
|
|
265
|
+
})
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 封装axios 超时返回false
|
|
270
|
+
* @param url - 请求地址
|
|
271
|
+
* @param timeout - 超时时间
|
|
272
|
+
* @returns - 请求结果
|
|
273
|
+
*/
|
|
274
|
+
async Axios (url, timeout) {
|
|
275
|
+
try {
|
|
276
|
+
const res = await axios.get(url, { timeout })
|
|
277
|
+
return res.data
|
|
278
|
+
} catch {
|
|
279
|
+
return false
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
package/lib/cli/karin.d.ts
CHANGED
|
@@ -1,14 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
export
|
|
3
|
-
Node = "node",
|
|
4
|
-
Tsx = "tsx",
|
|
5
|
-
Pm2 = "pm2"
|
|
6
|
-
}
|
|
7
|
-
export declare const enum Mode {
|
|
8
|
-
Dev = "dev",
|
|
9
|
-
Prod = "prod"
|
|
10
|
-
}
|
|
11
|
-
export declare const enum Lang {
|
|
12
|
-
Js = "js",
|
|
13
|
-
Ts = "ts"
|
|
14
|
-
}
|
|
2
|
+
export {};
|
package/lib/cli/karin.js
CHANGED
|
@@ -1,287 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import fs from 'fs'
|
|
3
|
-
import path from 'path'
|
|
4
|
-
import yaml from 'yaml'
|
|
5
|
-
import axios from 'axios'
|
|
6
|
-
import { fileURLToPath } from 'url'
|
|
7
2
|
import { program } from 'commander'
|
|
8
|
-
import {
|
|
9
|
-
import { KarinCfgInit } from '../core/init/config.js'
|
|
10
|
-
class KarinCli {
|
|
11
|
-
child
|
|
12
|
-
filename
|
|
13
|
-
karinDir
|
|
14
|
-
file
|
|
15
|
-
constructor () {
|
|
16
|
-
process.env.karin_app_start_count = '0'
|
|
17
|
-
process.env.karin_app_watch = 'no'
|
|
18
|
-
/** 当前文件绝对路径 */
|
|
19
|
-
this.filename = fileURLToPath(import.meta.url)
|
|
20
|
-
/** karin目录 */
|
|
21
|
-
this.karinDir = path.join(path.dirname(this.filename), '../..')
|
|
22
|
-
/** 入口文件(注意后缀) */
|
|
23
|
-
this.file = path.join(path.dirname(this.filename), '../index.js')
|
|
24
|
-
this.child = null
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 获取pkg
|
|
29
|
-
* @param isNpm - 是否是npm包
|
|
30
|
-
*/
|
|
31
|
-
pkg (isNpm) {
|
|
32
|
-
const filePath = isNpm ? path.join(this.karinDir, 'package.json') : path.join(process.cwd(), 'package.json')
|
|
33
|
-
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
|
|
34
|
-
return data
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 获取配置文件路径
|
|
39
|
-
* @param name - 配置文件名
|
|
40
|
-
*/
|
|
41
|
-
getConfigPath (name) {
|
|
42
|
-
const filePath = `./config/config/${name}.yaml`
|
|
43
|
-
if (!fs.existsSync(filePath)) { return `${this.karinDir}/config/config/${name}.yaml` }
|
|
44
|
-
return filePath
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 获取pm2配置
|
|
49
|
-
* @param name - 配置文件名
|
|
50
|
-
*/
|
|
51
|
-
getConfigData (name) {
|
|
52
|
-
const _path = this.getConfigPath(name)
|
|
53
|
-
const data = yaml.parse(fs.readFileSync(_path, 'utf-8'))
|
|
54
|
-
return data
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 启动
|
|
59
|
-
* @param mode - 模式
|
|
60
|
-
* @param lang - 语言环境
|
|
61
|
-
* @param runner - 运行器
|
|
62
|
-
*/
|
|
63
|
-
start (mode, lang, runner) {
|
|
64
|
-
process.env.karin_app_mode = mode
|
|
65
|
-
process.env.karin_app_lang = lang
|
|
66
|
-
process.env.karin_app_runner = runner
|
|
67
|
-
let cmd
|
|
68
|
-
switch (runner) {
|
|
69
|
-
case 'node' /* Runner.Node */:
|
|
70
|
-
cmd = [this.file]
|
|
71
|
-
break
|
|
72
|
-
case 'tsx' /* Runner.Tsx */:
|
|
73
|
-
cmd = [this.file]
|
|
74
|
-
break
|
|
75
|
-
case 'pm2' /* Runner.Pm2 */: {
|
|
76
|
-
this.pm2()
|
|
77
|
-
return
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
/** 启动 */
|
|
81
|
-
this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env, shell: runner === 'tsx' /* Runner.Tsx */ })
|
|
82
|
-
/** 监听退出 */
|
|
83
|
-
this.child.once('exit', (code) => process.exit(code))
|
|
84
|
-
/** 监听子进程消息 */
|
|
85
|
-
this.child.on('message', (data) => {
|
|
86
|
-
/** pm2重启 */
|
|
87
|
-
if (data.env.pm_id) { return this.restart() }
|
|
88
|
-
try {
|
|
89
|
-
/** 先结束进程 */
|
|
90
|
-
this.child.kill('SIGINT')
|
|
91
|
-
/** 停止监听 */
|
|
92
|
-
this.child.removeAllListeners()
|
|
93
|
-
/** 重启次数+1 */
|
|
94
|
-
const count = Number(process.env.karin_app_start_count) || 0
|
|
95
|
-
process.env.karin_app_start_count = String(count + 1)
|
|
96
|
-
} catch { }
|
|
97
|
-
return this.start(mode, lang, runner)
|
|
98
|
-
})
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* pm2重启
|
|
103
|
-
*/
|
|
104
|
-
async restart () {
|
|
105
|
-
const pm2Cfg = this.getConfigData('pm2')
|
|
106
|
-
const serverCfg = this.getConfigData('server')
|
|
107
|
-
/** 尝试获取pm2的进程id */
|
|
108
|
-
const port = serverCfg?.http?.port || 7000
|
|
109
|
-
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000)
|
|
110
|
-
if (res) {
|
|
111
|
-
await this.exec(`pm2 restart ${res.pm2_id}`)
|
|
112
|
-
} else {
|
|
113
|
-
await this.exec(`pm2 restart ${pm2Cfg?.apps[0]?.name || 'Karin'}`)
|
|
114
|
-
}
|
|
115
|
-
console.log('pm2服务已重启')
|
|
116
|
-
process.exit(0)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* pm2启动
|
|
121
|
-
*/
|
|
122
|
-
async pm2 () {
|
|
123
|
-
console.log('pm2启动中...')
|
|
124
|
-
const filePath = this.getConfigPath('pm2')
|
|
125
|
-
const data = this.getConfigData('pm2')
|
|
126
|
-
/** 修正入口文件路径 兼容0.6.28以前的版本 */
|
|
127
|
-
if (!fs.existsSync('./src') && filePath === './config/config/pm2.yaml') {
|
|
128
|
-
const script = './node_modules/node-karin/lib/index.js'
|
|
129
|
-
if (data.apps[0].script !== script) {
|
|
130
|
-
data.apps[0].script = script
|
|
131
|
-
fs.writeFileSync(filePath, yaml.stringify(data))
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
const cmd = `pm2 start ${filePath} --env ${JSON.stringify(process.env)}`
|
|
135
|
-
await this.exec(cmd)
|
|
136
|
-
console.log('pm2服务已启动 可执行 【npx karin log】 查看日志')
|
|
137
|
-
process.exit(0)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* pm2结束进程
|
|
142
|
-
*/
|
|
143
|
-
async stop () {
|
|
144
|
-
console.log('pm2服务停止中...')
|
|
145
|
-
const pm2Cfg = this.getConfigData('pm2')
|
|
146
|
-
const serverCfg = this.getConfigData('server')
|
|
147
|
-
const port = serverCfg?.http?.port || 7000
|
|
148
|
-
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000)
|
|
149
|
-
if (res) {
|
|
150
|
-
await this.exec(`pm2 delete ${res.pm2_id}`)
|
|
151
|
-
} else {
|
|
152
|
-
await this.exec(`pm2 delete ${pm2Cfg?.apps[0]?.name || 'Karin'}`)
|
|
153
|
-
}
|
|
154
|
-
console.log('pm2服务已停止')
|
|
155
|
-
process.exit(0)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* pm2查看日志
|
|
160
|
-
*/
|
|
161
|
-
async log () {
|
|
162
|
-
const pm2Cfg = this.getConfigData('pm2')
|
|
163
|
-
const serverCfg = this.getConfigData('server')
|
|
164
|
-
const port = serverCfg?.http?.port || 7000
|
|
165
|
-
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000)
|
|
166
|
-
const lines = pm2Cfg?.lines || 1000
|
|
167
|
-
const cmd = process.platform === 'win32' ? 'pm2.cmd' : 'pm2'
|
|
168
|
-
const type = res ? res.pm_id : pm2Cfg?.apps[0]?.name || 'Karin'
|
|
169
|
-
spawn(cmd, ['logs', type, '--lines', lines], { stdio: 'inherit', shell: true, cwd: process.cwd() })
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* 更新依赖
|
|
174
|
-
*/
|
|
175
|
-
async update () {
|
|
176
|
-
/** 屏蔽的依赖包列表 */
|
|
177
|
-
const pkgdependencies = [
|
|
178
|
-
'art-template',
|
|
179
|
-
'axios',
|
|
180
|
-
'chalk',
|
|
181
|
-
'chokidar',
|
|
182
|
-
'commander',
|
|
183
|
-
'express',
|
|
184
|
-
'level',
|
|
185
|
-
'lodash',
|
|
186
|
-
'log4js',
|
|
187
|
-
'moment',
|
|
188
|
-
'node-schedule',
|
|
189
|
-
'redis',
|
|
190
|
-
'ws',
|
|
191
|
-
'yaml',
|
|
192
|
-
]
|
|
193
|
-
const list = Object.keys(this.pkg(false).dependencies).filter(key => !pkgdependencies.includes(key))
|
|
194
|
-
/** 获取包管理器 */
|
|
195
|
-
const pkg = new KarinCfgInit().getRegistry()
|
|
196
|
-
const cmd = pkg === 'yarn' ? 'yarn upgrade' : `${pkg} update`
|
|
197
|
-
/** 异步并发更新依赖 */
|
|
198
|
-
await Promise.all(list.map(async (item) => {
|
|
199
|
-
try {
|
|
200
|
-
/** 检查是否已经是最新版本 */
|
|
201
|
-
const local = await this.getLocalVersion(item, pkg)
|
|
202
|
-
const remote = await this.getRemoteVersion(item, pkg)
|
|
203
|
-
if (local === remote) {
|
|
204
|
-
console.log(`[依赖更新] ${item} 已经是最新~`)
|
|
205
|
-
return
|
|
206
|
-
}
|
|
207
|
-
console.log(`[依赖更新] ${item} 当前版本: ${local} 最新版本: ${remote}`)
|
|
208
|
-
await this.exec(`${cmd} ${item}@latest`)
|
|
209
|
-
console.log(`[依赖更新] ${item} 更新完成~`)
|
|
210
|
-
} catch (error) {
|
|
211
|
-
console.error(`[依赖更新] ${item} 更新失败:`)
|
|
212
|
-
console.error(`error.stack: ${error.stack}`)
|
|
213
|
-
console.error(`error.message: ${error.message}`)
|
|
214
|
-
}
|
|
215
|
-
}))
|
|
216
|
-
console.log('所有依赖已更新完成~')
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* 获取指定包的本地版本
|
|
221
|
-
* @param name - 包名
|
|
222
|
-
* @param pkg - 包管理器
|
|
223
|
-
* @returns - 版本号
|
|
224
|
-
*/
|
|
225
|
-
async getLocalVersion (name, pkg) {
|
|
226
|
-
const cmd = pkg === 'yarn' ? `yarn list --pattern ${name}` : `${pkg} list ${name} --depth=0`
|
|
227
|
-
const text = await this.exec(cmd)
|
|
228
|
-
/** pnpm特殊处理 */
|
|
229
|
-
if (pkg === 'pnpm') {
|
|
230
|
-
const reg = new RegExp(`${name}\\s+([\\d.]+)`, 'gm')
|
|
231
|
-
const res = reg.exec(text)
|
|
232
|
-
return res?.[1] || '0.0.0'
|
|
233
|
-
}
|
|
234
|
-
const reg = new RegExp(`${name}@(\\d+\\.\\d+\\.\\d+)`, 'gm')
|
|
235
|
-
const res = reg.exec(text)
|
|
236
|
-
return res?.[1] || '0.0.0'
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* 获取指定包的最新版本
|
|
241
|
-
* @param name - 包名
|
|
242
|
-
* @param pkg - 包管理器
|
|
243
|
-
*/
|
|
244
|
-
async getRemoteVersion (name, pkg) {
|
|
245
|
-
const cmd = `${pkg} info ${name} version`
|
|
246
|
-
const text = await this.exec(cmd)
|
|
247
|
-
/** yarn特殊处理 */
|
|
248
|
-
if (pkg === 'yarn') {
|
|
249
|
-
const lines = text.split('\n').map(line => line.trim())
|
|
250
|
-
const ver = lines.find(line => /^\d+\.\d+\.\d+$/.test(line))
|
|
251
|
-
return ver || ''
|
|
252
|
-
}
|
|
253
|
-
return text.trim()
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* 封装exec
|
|
258
|
-
* @param cmd - 命令
|
|
259
|
-
*/
|
|
260
|
-
exec (cmd) {
|
|
261
|
-
return new Promise((resolve, reject) => {
|
|
262
|
-
execCmd(cmd, (error, stdout, stderr) => {
|
|
263
|
-
if (stdout) { return resolve(stdout.trim()) }
|
|
264
|
-
if (error) { return reject(error) }
|
|
265
|
-
return reject(stderr)
|
|
266
|
-
})
|
|
267
|
-
})
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* 封装axios 超时返回false
|
|
272
|
-
* @param url - 请求地址
|
|
273
|
-
* @param timeout - 超时时间
|
|
274
|
-
* @returns - 请求结果
|
|
275
|
-
*/
|
|
276
|
-
async Axios (url, timeout) {
|
|
277
|
-
try {
|
|
278
|
-
const res = await axios.get(url, { timeout })
|
|
279
|
-
return res.data
|
|
280
|
-
} catch {
|
|
281
|
-
return false
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
3
|
+
import { KarinCli } from './index.js'
|
|
285
4
|
const cli = new KarinCli()
|
|
286
5
|
program.version(cli.pkg(true).version, '-v, --version', '显示版本号')
|
|
287
6
|
program.command('.').description('启动karin').action(() => cli.start('prod' /* Mode.Prod */, 'js' /* Lang.Js */, 'node' /* Runner.Node */))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Logger as LoggerType } from 'log4js';
|
|
2
2
|
import { Logger } from '../../types/index.js';
|
|
3
|
-
import { Lang, Mode, Runner } from '../../cli/
|
|
3
|
+
import { Lang, Mode, Runner } from '../../cli/index.js';
|
|
4
4
|
declare global {
|
|
5
5
|
namespace NodeJS {
|
|
6
6
|
interface ProcessEnv {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-karin",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.9",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "基于 Kritor 进行开发的nodejs机器人框架",
|
|
6
6
|
"homepage": "https://github.com/KarinJS/Karin",
|
|
@@ -136,7 +136,8 @@
|
|
|
136
136
|
"fix": "eslint lib/**/*.js --fix",
|
|
137
137
|
"fix:all": "eslint lib/**/*.js --fix && eslint lib/**/*.d.ts --fix",
|
|
138
138
|
"pub": "npm publish --access public",
|
|
139
|
-
"sort": "npx sort-package-json && sort-json tsconfig.json"
|
|
139
|
+
"sort": "npx sort-package-json && sort-json tsconfig.json",
|
|
140
|
+
"dev": "node lib/cli/dev.js"
|
|
140
141
|
},
|
|
141
142
|
"dependencies": {
|
|
142
143
|
"art-template": "4.13.2",
|
|
@@ -154,20 +155,6 @@
|
|
|
154
155
|
"ws": "8.18.0",
|
|
155
156
|
"yaml": "2.5.0"
|
|
156
157
|
},
|
|
157
|
-
"devDependencies": {
|
|
158
|
-
"@types/express": "^4.17.21",
|
|
159
|
-
"@types/lodash": "^4.17.7",
|
|
160
|
-
"@types/node": "^20.14.11",
|
|
161
|
-
"@types/node-schedule": "^2.1.7",
|
|
162
|
-
"@types/ws": "^8.5.11",
|
|
163
|
-
"eslint": "^9.7.0",
|
|
164
|
-
"neostandard": "^0.11.1",
|
|
165
|
-
"sort-json": "^2.0.1",
|
|
166
|
-
"sort-package-json": "^2.10.0",
|
|
167
|
-
"tsc-alias": "^1.8.10",
|
|
168
|
-
"tsx": "^4.16.2",
|
|
169
|
-
"typescript": "^5.5.3"
|
|
170
|
-
},
|
|
171
158
|
"engines": {
|
|
172
159
|
"node": ">=18"
|
|
173
160
|
},
|
|
@@ -175,4 +162,4 @@
|
|
|
175
162
|
"access": "public",
|
|
176
163
|
"registry": "https://registry.npmjs.org"
|
|
177
164
|
}
|
|
178
|
-
}
|
|
165
|
+
}
|