1- import consola from "consola"
21import { execa } from "execa"
32
4- import { PATHS } from "~/lib/paths"
5-
6- import type { GetTokenResponse } from "./types"
7-
8- const TEN_MINUTES = 10 * 60 * 1000
9-
103// @ts -expect-error TypeScript can't analyze timeout
11- export async function getToken ( ) : Promise < GetTokenResponse > {
12- try {
13- const cachedToken = await readCachedToken ( )
14-
15- if ( Date . now ( ) - cachedToken . expires_at > ONE_DAY ) {
16- return cachedToken
17- }
18- } catch ( e ) {
19- if ( ! ( e instanceof Error ) ) throw e
20- if ( e . message === "No such file or directory" )
21- consola . info ( `No cached token found in ${ PATHS . PATH_TOKEN_CACHE } ` )
22- }
23-
4+ export async function getGitHubToken ( ) : Promise < string > {
245 // Kill any existing vscode processes
256 // otherwise, no token call will be made
267 await killVSCodeProcesses ( )
@@ -34,20 +15,24 @@ export async function getToken(): Promise<GetTokenResponse> {
3415
3516 for await ( const line of mitmdump . stdout ) {
3617 if ( typeof line !== "string" ) continue
37- if ( ! line . includes ( "tid=" ) ) continue
38-
39- consola . debug ( `Found token output line: ${ line } ` )
18+ if ( ! line . includes ( "authorization: token" ) ) continue
19+
20+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
21+ const token = line
22+ . split ( "\n" )
23+ . map ( ( line ) => line . trim ( ) )
24+ . filter ( Boolean )
25+ . find ( ( line ) => line . includes ( "authorization: token" ) ) !
26+ . split ( "authorization: token " )
27+ . at ( 1 ) !
28+ . trim ( )
4029
4130 clearTimeout ( timeout )
4231
4332 await killVSCodeProcesses ( )
4433 mitmdump . kill ( )
4534
46- const parsed = JSON . parse ( line ) as GetTokenResponse
47- parsed . expires_at = Date . now ( ) + t
48-
49- await writeCachedToken ( line )
50- return JSON . parse ( line ) as GetTokenResponse
35+ return token
5136 }
5237}
5338
@@ -73,11 +58,3 @@ const createVSCodeProcess = () =>
7358 ] )
7459
7560const killVSCodeProcesses = ( ) => execa ( { reject : false } ) ( "pkill" , [ "code" ] )
76-
77- const readCachedToken = async ( ) => {
78- const content = await Bun . file ( PATHS . PATH_TOKEN_CACHE ) . text ( )
79- return JSON . parse ( content ) as GetTokenResponse
80- }
81-
82- const writeCachedToken = async ( token : string ) =>
83- Bun . write ( PATHS . PATH_TOKEN_CACHE , token )
0 commit comments