Skip to content

Commit

Permalink
feat: implement notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Nov 8, 2021
1 parent bb7a369 commit 2c9a5d7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 48 deletions.
18 changes: 0 additions & 18 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion core/src/types/conf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { CookieRawObject } from './cookie'

export type UsersConf = { notifier?: Array<string>; users: Array<UserConfOpts> }
export type UsersConf = {
notifier?: [`${number}`, string]
users: Array<UserConfOpts>
}
export type UserConfOpts = {
username: string
password: string
Expand Down
5 changes: 5 additions & 0 deletions core/src/types/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Signale } from 'signale'

export interface LogRouter extends Signale {
notify: (msg: string) => Promise<void>
}
39 changes: 22 additions & 17 deletions core/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
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'
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<void> },
const logProxiedRouter = new Proxy(
log as typeof log & {
notify: () => Promise<void>
object: (obj: { [K: string]: string }) => void
},
{
get(target, prop, receiver) {
if (prop === 'error' || prop === 'success' || prop === 'warn') {
Expand All @@ -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
32 changes: 24 additions & 8 deletions core/src/utils/notifier.ts
Original file line number Diff line number Diff line change
@@ -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<string> = []

const saveNotifications = (args: Array<any>) => {
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(`<br>`)}<br>${
addtionalMessage.replace(/\n/g, '<br>').replace(/\s/g, '&nbsp')
}`
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 }
8 changes: 4 additions & 4 deletions plugins/check-in/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GlobalLogInfo> {
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)
Expand All @@ -269,6 +269,6 @@ async function signIn(users: UsersConf['users']): Promise<GlobalLogInfo> {
}
}),
)
log.notify()
log.notify(`签到结果 => \n${JSON.stringify(logs, null, ' ')}`)
return logs
}

0 comments on commit 2c9a5d7

Please sign in to comment.