Skip to content

Commit

Permalink
fix: improve logging format
Browse files Browse the repository at this point in the history
  • Loading branch information
matzkoh committed Jul 17, 2024
1 parent e341aa1 commit 12163dc
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 28 deletions.
71 changes: 59 additions & 12 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@
"dependencies": {
"@slack/socket-mode": "2.0.0",
"@slack/web-api": "7.3.1",
"chalk": "5.3.0",
"cli-truncate": "4.0.0",
"commander": "12.1.0",
"emoji-datasource": "15.1.2",
"slack-message-parser": "3.0.2",
"terminal-link": "3.0.0",
"ws": "8.18.0"
},
"devDependencies": {
Expand Down
41 changes: 37 additions & 4 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
@typescript-eslint/no-unsafe-member-access,
*/

import chalk from 'chalk'
import truncate from 'cli-truncate'
import link from 'terminal-link'

import type { CliOptions } from '../bin/index.js'

import { parse } from './parser.js'
Expand Down Expand Up @@ -53,11 +57,32 @@ export async function main(options: CliOptions) {

send(options.showUsername ? `${userName}<br />${comment}` : comment)

if (options.json) {
console.log(JSON.stringify({ ts: Math.trunc(event.ts), channel: channelName, user: userName, url, comment }))
} else if (options.logging) {
console.log(`#${channelName} @${userName} ${url} ${comment.replace(/\s+/g, '').slice(0, 40)}`)
if (!options.logging) {
return
}

console.log(
options.json
? JSON.stringify({
ts: Math.trunc(event.ts * 1000),
channel: channelName,
user: userName,
url,
comment,
})
: truncate(
[
chalk.yellow(link(intl.format(event.ts * 1000), url)),
chalk.magenta(channelName),
chalk.green(userName),
slack.renderTerminal(node),
]
.join(' ')
.replaceAll(/\s+/g, ' ')
.trim(),
process.stdout.columns || 80,
),
)
})

await slack.prepare()
Expand All @@ -67,3 +92,11 @@ export async function main(options: CliOptions) {
process.stderr.write(JSON.stringify(slack.getStats()) + '\n')
}
}

const intl = new Intl.DateTimeFormat('ja-JP', {
month: 'numeric',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
66 changes: 56 additions & 10 deletions src/lib/render.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import chalk from 'chalk'
import { NodeType } from 'slack-message-parser'
import link from 'terminal-link'

import type { EmojiFixedNode } from './parser-emoji.js'

export function render(
node: EmojiFixedNode,
channelMap: Map<string, string>,
userMap: Map<string, string>,
emojiMap: Map<string, string>,
): string {
return toHtml(node, channelMap, userMap, emojiMap).trim()
}

function toHtml(
export function toHtml(
node: EmojiFixedNode,
channelMap: Map<string, string>,
userMap: Map<string, string>,
Expand Down Expand Up @@ -57,3 +50,56 @@ function toHtml(
return node.children.map(n => toHtml(n, channelMap, userMap, emojiMap)).join('')
}
}

export function toTerminal(
node: EmojiFixedNode,
channelMap: Map<string, string>,
userMap: Map<string, string>,
emojiMap: Map<string, string>,
): string {
switch (node.type) {
case NodeType.Text:
case NodeType.PreText:
case NodeType.Code:
return node.text

case NodeType.ChannelLink:
return chalk.blue(
node.label
? `#${node.label.map(n => toTerminal(n, channelMap, userMap, emojiMap)).join('')}`
: `#${channelMap.get(node.channelID) ?? node.channelID}`,
)

case NodeType.UserLink:
return chalk.blue(
node.label
? `@${node.label.map(n => toTerminal(n, channelMap, userMap, emojiMap)).join('')}`
: `@${userMap.get(node.userID) ?? node.userID}`,
)

case NodeType.URL:
return chalk.blue(
link(
node.label
? node.label.map(n => toTerminal(n, channelMap, userMap, emojiMap)).join('')
: new URL(node.url).hostname,
node.url,
),
)

case NodeType.Command:
return node.label
? node.label.map(n => toTerminal(n, channelMap, userMap, emojiMap)).join('')
: `!${[node.name, ...node.arguments].join(' ')}`

case NodeType.Emoji:
return `:${node.name}:`

case NodeType.Italic:
case NodeType.Bold:
case NodeType.Strike:
case NodeType.Quote:
case NodeType.Root:
return node.children.map(n => toTerminal(n, channelMap, userMap, emojiMap)).join('')
}
}
8 changes: 6 additions & 2 deletions src/lib/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ConversationsListArguments } from '@slack/web-api'
import { WebClient } from '@slack/web-api'

import type { EmojiFixedNode } from './parser-emoji.js'
import { render } from './render.js'
import { toHtml, toTerminal } from './render.js'

interface Resource {
id: string
Expand Down Expand Up @@ -120,7 +120,11 @@ export class SlackClient {
}

renderComment(node: EmojiFixedNode) {
return render(node, this.channels, this.users, this.emojis)
return toHtml(node, this.channels, this.users, this.emojis)
}

renderTerminal(node: EmojiFixedNode) {
return toTerminal(node, this.channels, this.users, this.emojis)
}
}

Expand Down

0 comments on commit 12163dc

Please sign in to comment.