From 0f86df22db6af890ed86dace8f51aba72465a2d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Huan=20=28=E6=9D=8E=E5=8D=93=E6=A1=93=29?= Date: Mon, 1 Nov 2021 22:23:38 +0800 Subject: [PATCH] fix linting style --- bin/main.ts | 7 ++++++- package.json | 3 ++- src/chatops.ts | 10 +++++----- src/deliver-card.ts | 4 ++-- src/get-bot.ts | 2 +- src/get-memory.ts | 2 +- src/handlers-probot/pull-request.opened.ts | 2 +- src/handlers-wechaty/on-message.ts | 12 +++++------- src/handlers-wechaty/on-room-invite.ts | 2 +- src/pure-functions/match-org-repo.spec.ts | 18 +++++++++--------- src/pure-functions/match-org-repo.ts | 2 +- src/routers.ts | 11 +++++++---- src/setup-finis.ts | 4 +--- src/wtmp.ts | 2 +- 14 files changed, 43 insertions(+), 38 deletions(-) diff --git a/bin/main.ts b/bin/main.ts index d3a1a99..b65c380 100644 --- a/bin/main.ts +++ b/bin/main.ts @@ -7,6 +7,7 @@ import type { // https://probot.github.io/docs/development/#run-probot-programmatically import { run } from 'probot' +import { wrapAsyncError } from 'gerror' // import { Command } from 'commander' @@ -74,7 +75,11 @@ async function main () { // } try { - await run(probotApp) + await run( + wrapAsyncError(console.error)( + probotApp, + ), + ) } catch (e) { console.error(e) } diff --git a/package.json b/package.json index 1a87368..27d20fe 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "ducks": "^0.11.10", "express": "^4.17.1", "finis": "^0.4.5", + "gerror": "^1.0.2", "ha-wechaty": "^0.9.12", "micromatch": "^4.0.4", "moment": "^2.29.1", @@ -51,10 +52,10 @@ "rx-queue": "^0.12.6", "smee-client": "^1.2.2", "wechaty": "^0.68.1", + "wechaty-chatopera": "^0.4.2", "wechaty-ducks-contrib": "^0.3.6", "wechaty-plugin-contrib": "^0.14.23", "wechaty-puppet-padlocal": "^0.4.2", - "wechaty-chatopera": "^0.4.2", "wechaty-redux": "^0.5.7" }, "devDependencies": { diff --git a/src/chatops.ts b/src/chatops.ts index f48ef8b..100cf2c 100644 --- a/src/chatops.ts +++ b/src/chatops.ts @@ -16,7 +16,7 @@ import { export class Chatops { - private static singleton: Chatops + private static singleton?: Chatops public static instance ( haBot?: HAWechaty, @@ -108,7 +108,7 @@ export class Chatops { { const image = await info.toFileBox() await Promise.all( - roomList.map(room => room.say(image)) + roomList.map(room => room.say(image)), ) } break @@ -116,7 +116,7 @@ export class Chatops { { const urlLink = await info.toUrlLink() await Promise.all( - roomList.map(room => room.say(urlLink)) + roomList.map(room => room.say(urlLink)), ) } break @@ -124,14 +124,14 @@ export class Chatops { { const typeName = Message.Type[info.type()] await Promise.all( - roomList.map(room => room.say(`message type: ${typeName}`)) + roomList.map(room => room.say(`message type: ${typeName}`)), ) } break } } else if (info instanceof UrlLink) { await Promise.all( - roomList.map(room => room.say(info)) + roomList.map(room => room.say(info)), ) } diff --git a/src/deliver-card.ts b/src/deliver-card.ts index beb6e22..c06b89e 100644 --- a/src/deliver-card.ts +++ b/src/deliver-card.ts @@ -121,8 +121,8 @@ async function getRoomList ( if (Array.isArray(idOrList)) { const roomList = await Promise.all( idOrList.map( - id => getBot().Room.load(id) - ) + id => getBot().Room.load(id), + ), ) return roomList.filter(r => !!r) as Room[] } else { diff --git a/src/get-bot.ts b/src/get-bot.ts index e94c8e5..f591e31 100644 --- a/src/get-bot.ts +++ b/src/get-bot.ts @@ -25,7 +25,7 @@ const duckery = { type Duckery = typeof duckery -let haWechaty: HAWechaty +let haWechaty: undefined | HAWechaty export function getBot () { log.verbose('getWechaty', 'getHAWechaty()') diff --git a/src/get-memory.ts b/src/get-memory.ts index 8589ed6..0a8e2be 100644 --- a/src/get-memory.ts +++ b/src/get-memory.ts @@ -6,7 +6,7 @@ import { log, } from './config.js' -export let memory: MemoryCard +export let memory: undefined | MemoryCard export function getMemory (name: string): MemoryCard { log.verbose('getMemory', 'getMemory(%s)', name) diff --git a/src/handlers-probot/pull-request.opened.ts b/src/handlers-probot/pull-request.opened.ts index b8b13f5..5280f86 100644 --- a/src/handlers-probot/pull-request.opened.ts +++ b/src/handlers-probot/pull-request.opened.ts @@ -18,7 +18,7 @@ const pullRequestOpenedPlugin = (app: Probot) => app.on('pull_request.opened', a const title = [ `#${pullRequestNumber}`, - pullRequestTitle?.slice(0, Math.max(pullRequestTitle.length, 30)), + pullRequestTitle.slice(0, Math.max(pullRequestTitle.length, 30)), fullName, ].join(' ') const url = htmlUrl diff --git a/src/handlers-wechaty/on-message.ts b/src/handlers-wechaty/on-message.ts index 703bad3..9c4567b 100644 --- a/src/handlers-wechaty/on-message.ts +++ b/src/handlers-wechaty/on-message.ts @@ -31,21 +31,19 @@ export default async function onMessage ( // } const text = message.text() - const contact = message.talker() - if (!contact) { - return - } + const talker = message.talker() + if (text.toLowerCase() === 'oss') { // To Be Fix: Change "OSSChat ChatOps" Group Name to actual group name log.info('on-message', 'Begin to find the OSSChat ChatOps room') - const room = this.Room.load(CHATOPS_ROOM_ID) + const room = await this.Room.find({ id: CHATOPS_ROOM_ID }) if (room) { - await room.add(contact) + await room.add(talker) // To Be Fix: Change a formal welcome message - await room.say('Welcome to join OSSChat ChatOps Group', contact) + await room.say('Welcome to join OSSChat ChatOps Group', talker) } } log.info('on-message', 'onMessage(%s)', message) diff --git a/src/handlers-wechaty/on-room-invite.ts b/src/handlers-wechaty/on-room-invite.ts index c06143b..39c55e7 100644 --- a/src/handlers-wechaty/on-room-invite.ts +++ b/src/handlers-wechaty/on-room-invite.ts @@ -8,7 +8,7 @@ import { Chatops } from '../chatops.js' export default async function onRoomInvite ( this : Wechaty, - roomInvitation : RoomInvitation + roomInvitation : RoomInvitation, ): Promise { log.info('on-room-invite', 'onRoomInvite(%s)', roomInvitation) diff --git a/src/pure-functions/match-org-repo.spec.ts b/src/pure-functions/match-org-repo.spec.ts index b5ae2b0..32d6fa6 100755 --- a/src/pure-functions/match-org-repo.spec.ts +++ b/src/pure-functions/match-org-repo.spec.ts @@ -23,7 +23,7 @@ test('matchRepo', async t => { 'docs', 'wechaty/docs', ), - 'should match repo in plan text' + 'should match repo in plan text', ) t.notOk( @@ -31,7 +31,7 @@ test('matchRepo', async t => { 'docs', 'wechaty/matrix', ), - 'should not match repo in plan text' + 'should not match repo in plan text', ) t.ok( @@ -39,7 +39,7 @@ test('matchRepo', async t => { 'docs', 'wechaty/*cs', ), - 'should match repo in wildcat text' + 'should match repo in wildcat text', ) t.notOk( @@ -47,7 +47,7 @@ test('matchRepo', async t => { 'docs', 'wechaty/*trix', ), - 'should not match repo in wildcat text' + 'should not match repo in wildcat text', ) }) @@ -57,7 +57,7 @@ test('matchOwner between uppercase with lowercase', async t => { 'weCHATY', 'WEchaty/Docs', ), - 'should match owner in plan text for different cases' + 'should match owner in plan text for different cases', ) }) @@ -67,7 +67,7 @@ test('matchRepo between uppercase with lowercase', async t => { 'docs', 'wechaty/Docs', ), - 'should match repo in plan text for different cases' + 'should match repo in plan text for different cases', ) }) @@ -77,7 +77,7 @@ test('matchRepo with empty wildcat', async t => { 'docs', 'wechaty/*Docs*', ), - 'should match repo with empty wildcat for different cases' + 'should match repo with empty wildcat for different cases', ) }) @@ -94,7 +94,7 @@ test('matchRepo with complex wildcat', async t => { repo, fullName, ), - 'should match repo with complex wildcat for ' + repo + 'should match repo with complex wildcat for ' + repo, ) } }) @@ -113,7 +113,7 @@ test('matchRepo with group wildcat', async t => { repo, fullName, ), - 'should match repo with group match for ' + repo + 'should match repo with group match for ' + repo, ) } }) diff --git a/src/pure-functions/match-org-repo.ts b/src/pure-functions/match-org-repo.ts index 29641c6..05cb7f2 100644 --- a/src/pure-functions/match-org-repo.ts +++ b/src/pure-functions/match-org-repo.ts @@ -6,7 +6,7 @@ export function matchOwnerFullname ( ): boolean { return fullName.toLowerCase() .startsWith( - owner.toLowerCase() + '/' + owner.toLowerCase() + '/', ) } diff --git a/src/routers.ts b/src/routers.ts index dc2b416..c959581 100644 --- a/src/routers.ts +++ b/src/routers.ts @@ -8,6 +8,7 @@ import type { Room, Wechaty, } from 'wechaty' +import { wrapAsyncError } from 'gerror' import { log, @@ -22,6 +23,8 @@ import { getBot } from './get-bot.js' // counterSelectors, // } from './ducks/.js' +const wrapAsync = wrapAsyncError(console.error) + const haBot = getBot() const FORM_HTML = ` @@ -33,9 +36,9 @@ const FORM_HTML = ` ` function configureRoutes (router: Router) { - router.get('/dashboard/', rootHandler) - router.get('/chatops/', chatopsHandler) - router.get('/logout/', logoutHandler) + router.get('/dashboard/', wrapAsync(rootHandler)) + router.get('/chatops/', wrapAsync(chatopsHandler)) + router.get('/logout/', wrapAsync(logoutHandler)) } async function logoutHandler ( @@ -145,7 +148,7 @@ async function rootHandler ( html, htmlCounter, htmlFoot, - ].join('\n') + ].join('\n'), ) } diff --git a/src/setup-finis.ts b/src/setup-finis.ts index cb426dd..d976ac5 100644 --- a/src/setup-finis.ts +++ b/src/setup-finis.ts @@ -81,9 +81,7 @@ finis(async (code, signal) => { try { log.info('RestartReporter', 'finis() setTimeout() going to exit with %d', code) - if (bot) { - await bot.stop() - } + await bot.stop() } catch (e) { log.error('RestartReporter', 'finis() setTimeout() exception: %s', e) } finally { diff --git a/src/wtmp.ts b/src/wtmp.ts index 2b15196..6493e92 100644 --- a/src/wtmp.ts +++ b/src/wtmp.ts @@ -12,7 +12,7 @@ interface WtmpEntry { class Wtmp { - private static singleton: Wtmp + private static singleton?: Wtmp private bornTime: number private wtmpList: WtmpEntry[] = []