@@ -4,26 +4,15 @@ import relativeTime from "dayjs/plugin/relativeTime.js";
44import customParseFormat from "dayjs/plugin/customParseFormat.js" ;
55import * as cp from "child_process" ;
66import type { GitHubRepoInfo } from "./types.js" ;
7+ import path from "node:path" ;
8+ import { fileURLToPath } from "node:url" ;
79
810dayjs . extend ( relativeTime ) ;
911dayjs . extend ( customParseFormat ) ;
1012
1113// Git 로그 포맷 (vscode와 동일)
1214const GIT_LOG_FORMAT =
13- "%n%n" +
14- [
15- "%H" ,
16- "%P" ,
17- "%D" ,
18- "%an" ,
19- "%ae" ,
20- "%ad" ,
21- "%cn" ,
22- "%ce" ,
23- "%cd" ,
24- "%w(0,0,4)%s" ,
25- "%b" ,
26- ] . join ( "%n" ) ;
15+ "%n%n" + [ "%H" , "%P" , "%D" , "%an" , "%ae" , "%ad" , "%cn" , "%ce" , "%cd" , "%w(0,0,4)%s" , "%b" ] . join ( "%n" ) ;
2716
2817function resolveSpawnOutput ( cmd : cp . ChildProcess ) {
2918 return Promise . all ( [
@@ -62,26 +51,30 @@ export const GitHubUtils = {
6251 . replace ( / ^ h t t p s ? : \/ \/ g i t h u b \. c o m \/ / , "" )
6352 . replace ( / \. g i t $ / , "" )
6453 . replace ( / \/ + $ / , "" ) ;
65-
54+
6655 const [ owner , repo ] = cleaned . split ( "/" ) ;
67-
56+
6857 if ( ! owner || ! repo ) {
69- throw new Error ( `Invalid repository path: ${ repoUrlOrPath } . Expected format: "owner/repo" or "https://github.com/owner/repo"` ) ;
58+ throw new Error (
59+ `Invalid repository path: ${ repoUrlOrPath } . Expected format: "owner/repo" or "https://github.com/owner/repo"`
60+ ) ;
7061 }
71-
62+
7263 return { owner, repo } ;
7364 } ,
7465
7566 parseTimeRange ( since ?: string , until ?: string ) : { since : string ; until : string } {
7667 const now = dayjs ( ) ;
7768
78- let sinceDate = now . subtract ( 90 , ' day' ) ;
69+ let sinceDate = now . subtract ( 90 , " day" ) ;
7970 if ( since ) {
8071 const parsedSince = this . parseFlexibleDate ( since ) ;
8172 if ( parsedSince ) {
8273 sinceDate = parsedSince ;
8374 } else {
84- throw new Error ( `Invalid date format: ${ since } . Try formats like "30 days ago", "2024-01-01", "last month", "yesterday", or "30d"` ) ;
75+ throw new Error (
76+ `Invalid date format: ${ since } . Try formats like "30 days ago", "2024-01-01", "last month", "yesterday", or "30d"`
77+ ) ;
8578 }
8679 }
8780
@@ -91,7 +84,9 @@ export const GitHubUtils = {
9184 if ( parsedUntil ) {
9285 untilDate = parsedUntil ;
9386 } else {
94- throw new Error ( `Invalid date format: ${ until } . Try formats like "yesterday", "2024-01-01", "last week", or "today"` ) ;
87+ throw new Error (
88+ `Invalid date format: ${ until } . Try formats like "yesterday", "2024-01-01", "last week", or "today"`
89+ ) ;
9590 }
9691 }
9792
@@ -106,14 +101,14 @@ export const GitHubUtils = {
106101 const now = dayjs ( ) ;
107102
108103 const patterns = [
109- { regex : / ^ ( \d + ) \s * d a y s ? \s * a g o $ / i, unit : ' day' } ,
110- { regex : / ^ ( \d + ) \s * w e e k s ? \s * a g o $ / i, unit : ' week' } ,
111- { regex : / ^ ( \d + ) \s * m o n t h s ? \s * a g o $ / i, unit : ' month' } ,
112- { regex : / ^ ( \d + ) \s * y e a r s ? \s * a g o $ / i, unit : ' year' } ,
113- { regex : / ^ ( \d + ) d $ / i, unit : ' day' } ,
114- { regex : / ^ ( \d + ) w $ / i, unit : ' week' } ,
115- { regex : / ^ ( \d + ) m $ / i, unit : ' month' } ,
116- { regex : / ^ ( \d + ) y $ / i, unit : ' year' } ,
104+ { regex : / ^ ( \d + ) \s * d a y s ? \s * a g o $ / i, unit : " day" } ,
105+ { regex : / ^ ( \d + ) \s * w e e k s ? \s * a g o $ / i, unit : " week" } ,
106+ { regex : / ^ ( \d + ) \s * m o n t h s ? \s * a g o $ / i, unit : " month" } ,
107+ { regex : / ^ ( \d + ) \s * y e a r s ? \s * a g o $ / i, unit : " year" } ,
108+ { regex : / ^ ( \d + ) d $ / i, unit : " day" } ,
109+ { regex : / ^ ( \d + ) w $ / i, unit : " week" } ,
110+ { regex : / ^ ( \d + ) m $ / i, unit : " month" } ,
111+ { regex : / ^ ( \d + ) y $ / i, unit : " year" } ,
117112 ] ;
118113
119114 for ( const { regex, unit } of patterns ) {
@@ -127,11 +122,11 @@ export const GitHubUtils = {
127122 }
128123
129124 const quickFormats = {
130- ' yesterday' : ( ) => now . subtract ( 1 , ' day' ) ,
131- ' today' : ( ) => now ,
132- ' last week' : ( ) => now . subtract ( 1 , ' week' ) ,
133- ' last month' : ( ) => now . subtract ( 1 , ' month' ) ,
134- ' last year' : ( ) => now . subtract ( 1 , ' year' ) ,
125+ yesterday : ( ) => now . subtract ( 1 , " day" ) ,
126+ today : ( ) => now ,
127+ " last week" : ( ) => now . subtract ( 1 , " week" ) ,
128+ " last month" : ( ) => now . subtract ( 1 , " month" ) ,
129+ " last year" : ( ) => now . subtract ( 1 , " year" ) ,
135130 } ;
136131
137132 if ( quickFormats [ str as keyof typeof quickFormats ] ) {
@@ -241,7 +236,7 @@ export const GitHubUtils = {
241236
242237 async cloneRepository ( githubToken : string , owner : string , repo : string , targetPath : string ) : Promise < void > {
243238 const repoUrl = `https://${ githubToken } @github.com/${ owner } /${ repo } .git` ;
244-
239+
245240 const [ status , stdout , stderr ] = await resolveSpawnOutput (
246241 cp . spawn ( "git" , [ "clone" , repoUrl , targetPath ] , {
247242 env : Object . assign ( { } , process . env ) ,
@@ -255,14 +250,14 @@ export const GitHubUtils = {
255250
256251 async getDefaultBranch ( githubToken : string , owner : string , repo : string ) : Promise < string > {
257252 const octokit = this . createGitHubAPIClient ( githubToken ) ;
258-
253+
259254 try {
260255 const repoInfo = await octokit . repos . get ( { owner, repo } ) ;
261256 return repoInfo . data . default_branch ;
262257 } catch ( error : any ) {
263258 throw new Error ( `Failed to get default branch: ${ error . message } ` ) ;
264259 }
265- }
260+ } ,
266261} ;
267262
268263export const CommonUtils = {
@@ -280,9 +275,26 @@ export const CommonUtils = {
280275 return chunks ;
281276 } ,
282277 safeParseInt ( value : string | number ) : number {
283- if ( typeof value === ' number' ) return value ;
278+ if ( typeof value === " number" ) return value ;
284279 const parsed = parseInt ( value ) ;
285280 return isNaN ( parsed ) ? 0 : parsed ;
286- }
281+ } ,
287282} ;
288283
284+ export function getFilename ( ) : string {
285+ if ( typeof __filename !== "undefined" ) return __filename ;
286+ try {
287+ const metaUrl = ( 0 , eval ) ( "import.meta.url" ) ;
288+ if ( metaUrl ) return fileURLToPath ( metaUrl ) ;
289+ } catch { }
290+ return path . join ( process . cwd ( ) , "index.js" ) ;
291+ }
292+
293+ export function getDirname ( ) : string {
294+ if ( typeof __dirname !== "undefined" ) return __dirname ;
295+ try {
296+ const metaUrl = ( 0 , eval ) ( "import.meta.url" ) ;
297+ if ( metaUrl ) return path . dirname ( fileURLToPath ( metaUrl ) ) ;
298+ } catch { }
299+ return process . cwd ( ) ;
300+ }
0 commit comments