@@ -14,6 +14,8 @@ import { ChildProcess, SpawnOptions, fork, spawn } from 'node:child_process';
1414import path from 'node:path' ;
1515
1616import { CodifyTestUtils } from './test-utils.js' ;
17+ import fs from 'node:fs/promises' ;
18+ import * as os from 'node:os' ;
1719
1820const ajv = new Ajv . default ( {
1921 strict : true
@@ -52,7 +54,7 @@ export class PluginProcess {
5254 this . childProcess . stderr ?. pipe ( process . stderr ) ;
5355 this . childProcess . stdout ?. pipe ( process . stdout ) ;
5456
55- this . handleSudoRequests ( this . childProcess ) ;
57+ this . handleIncomingRequests ( this . childProcess ) ;
5658 }
5759
5860 async initialize ( ) : Promise < InitializeResponseData > {
@@ -99,7 +101,7 @@ export class PluginProcess {
99101 this . childProcess . kill ( ) ;
100102 }
101103
102- private handleSudoRequests ( process : ChildProcess ) {
104+ private handleIncomingRequests ( process : ChildProcess ) {
103105 // Listen for incoming sudo incoming sudo requests
104106 process . on ( 'message' , async ( message ) => {
105107 if ( ! ipcMessageValidator ( message ) ) {
@@ -121,6 +123,39 @@ export class PluginProcess {
121123 requestId,
122124 } )
123125 }
126+
127+ if ( message . cmd === MessageCmd . PRESS_KEY_TO_CONTINUE_REQUEST ) {
128+ const { data, requestId } = message ;
129+ if ( ! sudoRequestValidator ( data ) ) {
130+ throw new Error ( `Invalid sudo request from plugin. ${ JSON . stringify ( sudoRequestValidator . errors , null , 2 ) } ` ) ;
131+ }
132+
133+ process . send ( < IpcMessageV2 > {
134+ cmd : MessageCmd . PRESS_KEY_TO_CONTINUE_REQUEST + '_Response' ,
135+ data : { } ,
136+ requestId,
137+ } )
138+ }
139+
140+ if ( message . cmd === MessageCmd . CODIFY_CREDENTIALS_REQUEST ) {
141+ const { requestId } = message ;
142+
143+ const loginJson = await fs . readFile ( path . join ( os . homedir ( ) , '.codify' , 'credentials.json' ) , 'utf8' ) ;
144+ if ( ! loginJson ) {
145+ throw new Error ( 'Unable to get login credentials' )
146+ }
147+
148+ const login = JSON . parse ( loginJson ) ;
149+ if ( ! login ) {
150+ throw new Error ( 'Unable to parse login credentials' )
151+ }
152+
153+ process . send ( < IpcMessageV2 > {
154+ cmd : MessageCmd . CODIFY_CREDENTIALS_REQUEST + '_Response' ,
155+ data : login . accessToken ,
156+ requestId,
157+ } )
158+ }
124159 } )
125160 }
126161
0 commit comments