log.ts 1.88 KB
Newer Older
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
1 2 3 4 5
/**
 * Logs 模块
 * 使用 LogsModule 包裹后,为某个安装流程增加日志处理,并且对外还是返回 Promise 对象
 */

王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
6
import { Socket } from "socket.io";
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
7

王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
8 9 10 11 12 13 14
let websocket: Map<string, Socket> = new Map()
export function setWebsocketIo (username: string, io: Socket) {
  websocket.set(username, io)
}
export function getWebsocketIo (username: string) {
  return websocket.get(username) as Socket
}
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
15

16 17 18 19 20 21
export function sendProjectStatus (platform: 'v1' | 'v2', username: string, status: string) {
  const eventName = platform === 'v1' ? 'ChangeV1Status' : 'ChangeV2Status'
  const socket = getWebsocketIo(username)
  socket.emit(eventName, status)
}

王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
22 23 24
export default class LogUtil {
  // 根据 log 判断用户是否已经执行过
  static async run (username: string, log: string, cb: () => Promise<void>) {
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
25 26
    let socket = getWebsocketIo(username)
    socket.emit('Log', '[progress] [1m[Loading][m ' + log + '中...\n')
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
27
    await cb()
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
28
    socket.emit('Log', '[progress] [1m[Info][m ' + log + '完成\n')
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
29 30
  }
  static async print(username: string, log: string) {
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
31 32
    let socket = getWebsocketIo(username)
    socket.emit('Log', removeUnreadCharacter(log))
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
33 34
  }
  static async printInfo(username: string, log: string) {
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
35 36
    let socket = getWebsocketIo(username)
    socket.emit('Log', removeUnreadCharacter(`[progress] [[1;34mInfo[m] ${log} \n`))
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
37 38
  }
  static async printError(username: string, log: string) {
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
39 40
    let socket = getWebsocketIo(username)
    socket.emit('Log', removeUnreadCharacter(`[error] [[1;31mError[m] ${log} \n`))
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
41 42
  }
  static async printSuccess(username: string, log: string) {
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
43 44
    let socket = getWebsocketIo(username)
    socket.emit('Log', removeUnreadCharacter(`[progress] [[1;31mSuccess[m] ${log} \n`))
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
45 46 47 48
  }
  static async printWarning (username: string, log: string) {

  }
49 50 51 52
  static clear (username: string) {
    let socket = getWebsocketIo(username)
    socket.emit('ClearLog')
  }
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
53
}