Skip to content

Commit

Permalink
fix: improved logging (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
bonyuta0204 authored Dec 9, 2023
1 parent 5b798c8 commit eea33dd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/exporter/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {} from 'csv-writer'
import { createObjectCsvStringifier } from 'csv-writer'
import { nonNullable } from '../utils'
import { writeFile } from 'fs/promises'
import { logger } from '../logger'

export type PullRequestInfo = {
id: string
Expand Down Expand Up @@ -53,5 +54,5 @@ export async function exportPullRequests(
} else {
process.stdout.write(csvString)
}
console.log('CSV file was written successfully')
logger.log('CSV file was written successfully')
}
8 changes: 5 additions & 3 deletions src/github/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { logger } from '../logger'

type PagingFunction<TItem> = (
items: TItem[],
hasNextPage: boolean,
Expand All @@ -11,7 +13,7 @@ type PagingFunction<TItem> = (
hasNextPage: boolean
}>

type ProgressCallBack = (param: {
export type ProgressCallBack = (param: {
totalCount?: number
currentCount: number
}) => void
Expand Down Expand Up @@ -95,9 +97,9 @@ export const buildPagingFunc: BuildPagingFunc = (fn) => {

export const logProgress: ProgressCallBack = ({ totalCount, currentCount }) => {
if (totalCount) {
process.stdout.write(`Progress: ${currentCount}/${totalCount}\n`)
logger.log(`Progress: ${currentCount}/${totalCount}\n`)
} else {
console.log(currentCount)
process.stdout.write(`Progress: ${currentCount}\n`)
logger.log(`Progress: ${currentCount}\n`)
}
}
7 changes: 4 additions & 3 deletions src/github/pulls.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ApolloClient, type NormalizedCacheObject } from '@apollo/client'

import { gql } from '../types/__generated__/gql'
import { buildPagingFunc, logProgress } from './pagination'
import { buildPagingFunc, ProgressCallBack } from './pagination'

const GET_PULL_REQUESTS = gql(/* GraphQL */ `
query GetPullRequestStats(
Expand Down Expand Up @@ -44,7 +44,8 @@ export async function getPullRequestStats(
client: ApolloClient<NormalizedCacheObject>,
repoOwner: string,
repoName: string,
limit?: number
limit?: number,
progressCallBack?: ProgressCallBack
) {
const pagingFetch = buildPagingFunc(async (limit, cursor) => {
const response = await client.query({
Expand Down Expand Up @@ -72,6 +73,6 @@ export async function getPullRequestStats(
}
})

const pulls = await pagingFetch([], true, undefined, limit, logProgress)
const pulls = await pagingFetch([], true, undefined, limit, progressCallBack)
return pulls.items
}
21 changes: 21 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Writable } from 'stream'

interface Logger {
log: (message: string) => void
error: (message: string) => void
}

export const logger: Logger = (() => {
const output: Writable = process.stdout.isTTY
? process.stdout
: process.stderr

return {
log: (message: string) => {
output.write(message)
},
error: (message: string) => {
process.stderr.write(message)
}
}
})()
4 changes: 3 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { exportPullRequests } from './exporter/csv'
import { getClient } from './github/client'
import { logProgress } from './github/pagination'

import { getPullRequestStats } from './github/pulls'
import { nonNullable } from './utils'
Expand All @@ -19,7 +20,8 @@ export async function run(options: RunOptions) {
client,
options.owner,
options.repo,
options.limit
options.limit,
logProgress
)

if (!pulls) return
Expand Down

0 comments on commit eea33dd

Please sign in to comment.