Skip to content

Commit

Permalink
refactor: migrate to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Feb 25, 2024
1 parent ffb2c02 commit 68134c5
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 30 deletions.
21 changes: 16 additions & 5 deletions demo/index.ts
@@ -1,20 +1,31 @@
import fs from 'node:fs'
import path from 'node:path'
import path, { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { Mirai } from 'mirai-ts'
import type { MiraiApiHttpSetting } from 'mirai-ts'
import yaml from 'js-yaml'
import consola from 'consola'

const __dirname = dirname(fileURLToPath(import.meta.url))

const qq = 712727946
// setting 可直接读取 setting.yml 或参考 `src/types/setting.ts`
const setting = yaml.load(
fs.readFileSync(
let settingYml = ''
try {
settingYml = fs.readFileSync(
path.resolve(
__dirname,
'../mcl/config/net.mamoe.mirai-api-http/setting.yml',
),
'utf8',
),
) as MiraiApiHttpSetting
)
}
catch (e) {
consola.error('读取 setting.yml 失败')
console.error(e)
}

const setting = yaml.load(settingYml) as MiraiApiHttpSetting

const mirai = new Mirai(setting)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -53,9 +53,9 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@yunyoujun/logger": "0.2.3",
"axios": "^1.6.7",
"chalk": "4",
"consola": "^3.2.3",
"form-data": "^4.0.0",
"ws": "^8.16.0"
},
Expand Down
13 changes: 3 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/index.ts
Expand Up @@ -14,8 +14,6 @@ export * from './mirai-api-http'

// 工具
export * from './utils'
// https://www.npmjs.com/package/@yunyoujun/logger
export { Logger } from '@yunyoujun/logger'

// 类型
export * from './types'
Expand Down
8 changes: 4 additions & 4 deletions src/mirai-api-http/index.ts
Expand Up @@ -9,8 +9,7 @@ import process from 'node:process'
import type { AxiosResponse, AxiosStatic } from 'axios'
import FormData from 'form-data'
import WebSocket from 'ws'
import { Logger } from '@yunyoujun/logger'
import chalk from 'chalk'
import { createConsola } from 'consola'
import { isChatMessage } from '../utils/check'
import type {
Api,
Expand Down Expand Up @@ -91,7 +90,8 @@ export class MiraiApiHttp {
*/
resp: Resp

public logger = new Logger({ prefix: chalk.cyan('[mirai-api-http]') })
// public logger = new Logger({ prefix: chalk.cyan('[mirai-api-http]') })
public logger = createConsola().withTag('mirai-api-http')

constructor(public mirai: Mirai, public axios: AxiosStatic) {
this.setting = this.mirai.mahSetting
Expand Down Expand Up @@ -128,7 +128,7 @@ export class MiraiApiHttp {
this.logger.error(`Code ${statusCode}: ${message}`)

if (statusCode === 3 || statusCode === 4) {
this.logger.warning('正在尝试重新建立连接...')
this.logger.warn('正在尝试重新建立连接...')
await this.verify()
await this.bind(this.qq)
}
Expand Down
5 changes: 2 additions & 3 deletions src/mirai.ts
Expand Up @@ -4,9 +4,8 @@
*/

import events from 'node:events'
import chalk from 'chalk'
import type { AxiosStatic } from 'axios'
import { Logger } from '@yunyoujun/logger'
import { createConsola } from 'consola'
import pkg from '../package.json'
import * as axios from './axios'
import { MiraiApiHttp } from './mirai-api-http'
Expand Down Expand Up @@ -61,7 +60,7 @@ export class Mirai {
/**
* 日志模块
*/
logger = new Logger({ prefix: chalk.cyan('[mirai-ts]') })
logger = createConsola().withTag('mirai-ts')
/**
* 请求工具
*/
Expand Down
4 changes: 2 additions & 2 deletions test/logger.ts
@@ -1,3 +1,3 @@
import { Logger } from '@yunyoujun/logger'
import { createConsola } from 'consola'

export const logger = new Logger({ prefix: 'test' })
export const logger = createConsola().withTag('test')
9 changes: 6 additions & 3 deletions tsconfig.json
Expand Up @@ -2,10 +2,12 @@
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
"target": "ES2016",
"target": "ESNext",
"lib": ["ESNext"],
"experimentalDecorators": true,
"baseUrl": ".",
"moduleResolution": "node",
"module": "ESNext",
"moduleResolution": "Bundler",
"paths": {
"mirai-ts": ["./src/index.ts"]
},
Expand All @@ -15,7 +17,8 @@
"declaration": true,
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true
},
"exclude": ["dist", "node_modules"]
}

0 comments on commit 68134c5

Please sign in to comment.