From 2c9a5d73e6620acfef91f19bd37dc709297a3ae3 Mon Sep 17 00:00:00 2001 From: beetcb <63141491+beetcb@users.noreply.github.com> Date: Mon, 8 Nov 2021 17:48:59 +0800 Subject: [PATCH] feat: implement notifier --- core/src/index.ts | 18 ---------------- core/src/types/conf.ts | 5 ++++- core/src/types/logger.ts | 5 +++++ core/src/utils/logger.ts | 39 ++++++++++++++++++++--------------- core/src/utils/notifier.ts | 32 +++++++++++++++++++++------- plugins/check-in/src/index.ts | 8 +++---- 6 files changed, 59 insertions(+), 48 deletions(-) create mode 100644 core/src/types/logger.ts diff --git a/core/src/index.ts b/core/src/index.ts index ca10e09..b3cb7d0 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -49,24 +49,6 @@ export async function handleCookie(options?: HandleCookieOptions) { throw error } } - /* 未加载用户执行sign 会报错 - - file:///root/cea/core/lib/src/index.js:11 - await Promise.all(sstore.get("users").map(async (i) => { - ^ - -TypeError: Cannot read properties of undefined (reading 'map') - at handleCookie (file:///root/cea/core/lib/src/index.js:11:40) - at checkIn (file:///root/cea/plugins/check-in/lib/src/index.js:162:9) - at file:///root/cea/internal/lib/src/cli.js:26:13 - at file:///root/cea/internal/lib/src/cli.js:47:3 - at ModuleJob.run (node:internal/modules/esm/module_job:185:25) - at async Promise.all (index 0) - at async ESMLoader.import (node:internal/modules/esm/loader:281:24) - at async loadESM (node:internal/process/esm_loader:88:5) - at async handleMainPromise (node:internal/modules/run_main:65:12) - - */ } async function handleLogin( diff --git a/core/src/types/conf.ts b/core/src/types/conf.ts index a69fafa..b241a57 100644 --- a/core/src/types/conf.ts +++ b/core/src/types/conf.ts @@ -1,6 +1,9 @@ import { CookieRawObject } from './cookie' -export type UsersConf = { notifier?: Array; users: Array } +export type UsersConf = { + notifier?: [`${number}`, string] + users: Array +} export type UserConfOpts = { username: string password: string diff --git a/core/src/types/logger.ts b/core/src/types/logger.ts new file mode 100644 index 0000000..acced8f --- /dev/null +++ b/core/src/types/logger.ts @@ -0,0 +1,5 @@ +import type { Signale } from 'signale' + +export interface LogRouter extends Signale { + notify: (msg: string) => Promise +} diff --git a/core/src/utils/logger.ts b/core/src/utils/logger.ts index 96f1693..200c499 100644 --- a/core/src/utils/logger.ts +++ b/core/src/utils/logger.ts @@ -1,5 +1,6 @@ import sstore from '@beetcb/sstore' import type { SignaleOptions } from 'signale' +import type { LogRouter } from '../types/logger' import signale from 'signale' import { notify, saveNotifications } from './notifier.js' @@ -7,22 +8,28 @@ const isNotificationEnabled = sstore.get('notifier')?.length const { Signale } = signale // Will be cached by node -const log = new Signale({ - types: { - error: { - label: '失败', +const log: LogRouter = { + ...new Signale({ + types: { + error: { + label: '失败', + }, + success: { + label: '成功', + }, + warn: { + label: '警示', + }, }, - success: { - label: '成功', - }, - warn: { - label: '警示', - }, - }, -} as SignaleOptions) + } as SignaleOptions), + notify, +} -const logWithNotifier = new Proxy( - log as typeof log & { notify: () => Promise }, +const logProxiedRouter = new Proxy( + log as typeof log & { + notify: () => Promise + object: (obj: { [K: string]: string }) => void + }, { get(target, prop, receiver) { if (prop === 'error' || prop === 'success' || prop === 'warn') { @@ -32,12 +39,10 @@ const logWithNotifier = new Proxy( target[prop].apply(target, args) } } - } else if (prop === 'notify') { - return notify } return Reflect.get(target, prop, receiver) }, }, ) -export default logWithNotifier +export default logProxiedRouter diff --git a/core/src/utils/notifier.ts b/core/src/utils/notifier.ts index 8b4082b..33f12d8 100644 --- a/core/src/utils/notifier.ts +++ b/core/src/utils/notifier.ts @@ -1,17 +1,33 @@ -import { EOL } from 'node:os' - +import sstore from '@beetcb/sstore' +import fetch from 'node-fetch' +const pushEndpoints = ['http://pushplus.hxtrip.com/send'] const notifications: Array = [] const saveNotifications = (args: Array) => { const message = args[0] - const log = message?.message ?? message - notifications.push(log) + const [log, at] = [message?.message ?? message, message?.suffix ?? ''] + notifications.push(`${log} ${at ?? ''}`) } -const notify = async function() { - const content = notifications.join(EOL) - // return pushAPI() - throw 'Not implemented' +const notify = async function(addtionalMessage: string) { + const [pushPlatform, pushToken] = sstore.get('notifier') + const content = `${notifications.join(`
`)}
${ + addtionalMessage.replace(/\n/g, '
').replace(/\s/g, ' ') + }` + const url = pushEndpoints[Number(pushPlatform)] + if (pushToken && url) { + await fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + content, + token: pushToken, + title: 'Cea.js 消息推送服务', + }), + }) + } } export { notify, saveNotifications } diff --git a/plugins/check-in/src/index.ts b/plugins/check-in/src/index.ts index 4dbb78b..3de4330 100644 --- a/plugins/check-in/src/index.ts +++ b/plugins/check-in/src/index.ts @@ -240,14 +240,14 @@ export async function checkIn() { // Log in and save cookie to cea const users = sstore.get('users') // Sign in - let logs = await signIn(users) - // Log out sign in result + const logs = await signIn(users) + // Log out results console.table(logs) } async function signIn(users: UsersConf['users']): Promise { const logs: GlobalLogInfo = {} - // sign in asynchronizedly with promise all and diff instance of signApp class + // Sign in asynchronizedly with promise all and diff instance of signApp class await Promise.all( users.map(async (i) => { const instance: CheckIn = new CheckIn(i) @@ -269,6 +269,6 @@ async function signIn(users: UsersConf['users']): Promise { } }), ) - log.notify() + log.notify(`签到结果 => \n${JSON.stringify(logs, null, ' ')}`) return logs }