@@ -3,8 +3,8 @@ import path from 'path'
3
3
import yaml from 'yaml'
4
4
import axios from 'axios'
5
5
import { fileURLToPath } from 'url'
6
- import { exec as execCmd , spawn , ChildProcess } from 'child_process '
7
- import { KarinCfgInit } from '../core/init/config '
6
+ import { getRegistry } from './pkg '
7
+ import { exec as execCmd , spawn , ChildProcess , ExecOptions } from 'child_process '
8
8
9
9
export const enum Runner {
10
10
Node = 'node' ,
@@ -38,11 +38,12 @@ export class KarinCli {
38
38
/** 入口文件(注意后缀) */
39
39
this . file = path . join ( path . dirname ( this . filename ) , '../index.js' )
40
40
this . child = null as unknown as ChildProcess
41
+ process . env . karin_app_pkg = getRegistry ( )
41
42
process . env . karin_app_version = this . pkg ( true ) . version
42
43
}
43
44
44
45
/**
45
- * 获取pkg
46
+ * 获取pkg配置
46
47
* @param isNpm - 是否是npm包
47
48
*/
48
49
pkg ( isNpm : boolean ) {
@@ -226,10 +227,9 @@ export class KarinCli {
226
227
const list = Object . keys ( this . pkg ( false ) . dependencies ) . filter ( key => ! pkgdependencies . includes ( key ) )
227
228
228
229
/** 获取包管理器 */
229
- const pkg = new KarinCfgInit ( ) . getRegistry ( )
230
+ const pkg = getRegistry ( )
230
231
const cmd = pkg === 'yarn' ? 'yarn upgrade' : `${ pkg } update`
231
232
232
- /** 异步并发更新依赖 */
233
233
await Promise . all ( list . map ( async item => {
234
234
try {
235
235
/** 检查是否已经是最新版本 */
@@ -246,12 +246,26 @@ export class KarinCli {
246
246
console . log ( `[依赖更新] ${ item } 更新完成~` )
247
247
} catch ( error : any ) {
248
248
console . error ( `[依赖更新] ${ item } 更新失败:` )
249
- console . error ( `error.stack: ${ error . stack } ` )
250
- console . error ( `error.message: ${ error . message } ` )
249
+ console . error ( error . stack || error . message || error )
251
250
}
252
251
} ) )
253
252
254
- console . log ( '所有依赖已更新完成~' )
253
+ console . log ( '[依赖更新] 所有npm依赖已更新完成~' )
254
+ console . log ( '[依赖更新] 开始更新git插件...' )
255
+ const gitList = this . getGitPlugins ( )
256
+ if ( ! gitList . length ) return console . log ( '[依赖更新] 没有git插件需要更新~' )
257
+
258
+ await Promise . all ( gitList . map ( async item => {
259
+ const dir = path . resolve ( process . cwd ( ) , 'plugins' , item )
260
+ try {
261
+ await this . exec ( 'git pull' , { cwd : dir } )
262
+ console . log ( `[依赖更新] ${ item } 更新完成~` )
263
+ } catch ( error : any ) {
264
+ console . error ( `[依赖更新] ${ item } 更新失败` )
265
+ console . error ( error . stack || error . message || error )
266
+ }
267
+ } ) )
268
+ console . log ( '[依赖更新] 所有git插件已更新完成~' )
255
269
}
256
270
257
271
/**
@@ -294,14 +308,28 @@ export class KarinCli {
294
308
return text . trim ( )
295
309
}
296
310
311
+ /**
312
+ * 获取git插件列表
313
+ */
314
+ getGitPlugins ( ) : Array < string > {
315
+ const dir = path . resolve ( process . cwd ( ) , 'plugins' )
316
+ let list = fs . readdirSync ( dir , { withFileTypes : true } )
317
+ /** 忽略非文件夹、非 karin-plugin-开头的文件夹 */
318
+ list = list . filter ( v => v . isDirectory ( ) && v . name . startsWith ( 'karin-plugin-' ) )
319
+ list = list . filter ( v => fs . existsSync ( `${ dir } /${ v . name } /package.json` ) )
320
+ const arr : string [ ] = [ ]
321
+ list . map ( v => arr . push ( v . name ) )
322
+ return arr
323
+ }
324
+
297
325
/**
298
326
* 封装exec
299
327
* @param cmd - 命令
300
328
*/
301
- exec ( cmd : string ) : Promise < string > {
329
+ exec ( cmd : string , options ?: ExecOptions ) : Promise < string > {
302
330
return new Promise ( ( resolve , reject ) => {
303
- execCmd ( cmd , ( error , stdout , stderr ) => {
304
- if ( stdout ) return resolve ( stdout . trim ( ) )
331
+ execCmd ( cmd , options , ( error , stdout , stderr ) => {
332
+ if ( stdout ) return resolve ( stdout . toString ( ) . trim ( ) )
305
333
if ( error ) return reject ( error )
306
334
return reject ( stderr )
307
335
} )
0 commit comments